affichage marqueur google avi

Eléphanteau du PHP | 33 Messages

21 févr. 2017, 17:00

Bonjour
j'ai réalisé premièrement un code qui me permettait d'afficher plusieurs marqueur sur une map, cependant je codais la Long et Lat à " la main" :

Code : Tout sélectionner

var myLatLng = [ {lat: <?php echo $infos_geo[0][0];?>, lng: <?php echo $infos_geo[0][1];?>}, {lat: <?php echo $infos_geo[8][0];?>, lng: <?php echo $infos_geo[8][1];?>}]; var map = new google.maps.Map(document.getElementById('map'), { zoom: 4, center: myLatLng[0], }); for (var i=0 ; i<myLatLng.length ; i++){ var marker = new google.maps.Marker({ position: myLatLng[i], map: map, title: 'Adresse n°'+i, }); }
J'ai essayé de créer une boucle afin de pouvoir retiré a la fois toutes les longitudes et latitude d'une de mes fonctions mais en vain et après plusieurs heures passé dessus j'ai pas du tout put avancer, voici le code :

Code : Tout sélectionner

var myLatLng = [<?php for ($j=0 ; $j< $longueur; $j++){ ?> { lat: <?php echo $infos_geo[$j][0];?> , lng: <?php echo $infos_geo[$j][1];?> } <?php } ?> ]; var map = new google.maps.Map(document.getElementById('map'), { zoom: 4, center: myLatLng[0], }); for (var i=0 ; i<myLatLng.length ; i++){ var marker = new google.maps.Marker({ position: myLatLng[i], map: map, title: 'Adresse n°'+i, }); }
sachant que $longueur=count($info_geo)
merci d'avance pour vos réponses ;)

Avatar du membre
Modérateur PHPfrance
Modérateur PHPfrance | 8758 Messages

21 févr. 2017, 17:22

salut,

tu gagnerais peux être du temps à utiliser json_encode et en utilisant une liste d'objet.

il y a quoi dans le JS généré ?

tu peux écrire cela de façon plus claire.
<?php
for ($j=0 ; $j< $longueur; $j++){
    echo '{ lat: ',$infos_geo[$j][0],' , lng:', $infos_geo[$j][1],', }';
}
?>
ceci le problème c'est surtout la virgule qui manque après l'accolade fermante ;)

du coup cela peux donner ceci
<?php
// tableau de test
$infos_geo = [
  [0=>1, 1=> 2],
  [0=>3, 1=> 4],
  [0=>5, 1=> 6]
];

$longueur = count($infos_geo);
 ?>
[
<?php
$lastIndex = count($infos_geo)-1;
foreach($infos_geo as $key => $value){
    echo '{ lat: ', $value[0] ,' , lng:', $value[1] ,'}';
    if($key < $lastIndex){
      echo ',';
    }
}
 ?>
]
résultat : [ { lat: 1 , lng:2},{ lat: 3 , lng:4},{ lat: 5 , lng:6}]


@+
Il en faut peu pour être heureux ......

Eléphanteau du PHP | 33 Messages

21 févr. 2017, 17:28

J'ai bien utilisé votre fonction :

Code : Tout sélectionner

var myLatLng = [<?php for ($j=0 ; $j< $longueur; $j++){ echo '{ lat: ',$infos_geo[$j][0],' , lng:', $infos_geo[$j][1],', }'; } ?> ]; var map = new google.maps.Map(document.getElementById('map'), { zoom: 4, center: myLatLng[0], }); for (var i=0 ; i<myLatLng.length ; i++){ var marker = new google.maps.Marker({ position: myLatLng[i], map: map, title: 'Adresse n°'+i, }); }
Cependant cela ne change rien, la map ne s'affiche pas et du coup les marqueurs non plus

Eléphanteau du PHP | 33 Messages

21 févr. 2017, 17:29

Et de plus la variable $infos_geo est généré par ceci :

Code : Tout sélectionner

<?php function get_value_in_dbb($nombre_point_geolocalisation){ try { $bdd = new PDO('mysql:host=localhost;dbname=riadh;charset=utf8', 'root', ''); } catch(Exception $e) { die('Erreur : '.$e->getMessage()); } if ($nombre_point_geolocalisation==1){ $donner=$bdd->query('(SELECT `ID`, `Device_ID`, `Latitude`, `Longitude`, `Battery`, `seq_number`, `Recorded_at` FROM hidnseek_mangements LIMIT 0,1) ORDER BY `Recorded_at` DESC'); $donner -> execute(); $coordonnee=$donner->fetchAll(); ?> <html> <table> <thead> <tr> <td>ID</td> <td>Device_ID</td> <td>Latitude</td> <td>Longitude</td> <td>Battery</td> <td>Seq_number</td> </tr> </thead> <tbody> <tr><?php foreach($coordonnee as $element){ ?> <td><?php echo $element['ID'] ?></td> <td><?php echo $element['Device_ID'] ?></td> <td><?php echo $element['Latitude'] ?></td> <td><?php echo $element['Longitude'] ?></td> <td><?php echo $element['Battery'] ?></td> <td><?php echo $element['seq_number'] ?></td> </tr> <?php } ?> </tbody> </table> <?php } elseif($nombre_point_geolocalisation==10){ $donner=$bdd->query('(SELECT `ID`, `Device_ID`, `Latitude`, `Longitude`, `Battery`, `seq_number`, `Recorded_at` FROM hidnseek_mangements LIMIT 0,10) ORDER BY `Recorded_at` DESC'); $donner -> execute(); $coordonnee=$donner->fetchAll(); ?> <table> <thead> <tr> <td>ID</td> <td>Device_ID</td> <td>Latitude</td> <td>Longitude</td> <td>Battery</td> <td>Seq_number</td> </tr> </thead> <tbody> <tr><?php foreach($coordonnee as $element){ ?> <td><?php echo $element['ID'] ?></td> <td><?php echo $element['Device_ID'] ?></td> <td><?php echo $element['Latitude'] ?></td> <td><?php echo $element['Longitude'] ?></td> <td><?php echo $element['Battery'] ?></td> <td><?php echo $element['seq_number'] ?></td> </tr> <?php } ?> </tbody> </table> <?php } elseif($nombre_point_geolocalisation==30){ $donner=$bdd->query('(SELECT `ID`, `Device_ID`, `Latitude`, `Longitude`, `Battery`, `seq_number`, `Recorded_at` FROM hidnseek_mangements LIMIT 0,30) ORDER BY `Recorded_at` DESC'); $donner -> execute(); $coordonnee=$donner->fetchAll(); ?> <table> <thead> <tr> <td>ID</td> <td>Device_ID</td> <td>Latitude</td> <td>Longitude</td> <td>Battery</td> <td>Seq_number</td> </tr> </thead> <tbody> <tr><?php foreach($coordonnee as $element){ ?> <td><?php echo $element['ID'] ?></td> <td><?php echo $element['Device_ID'] ?></td> <td><?php echo $element['Latitude'] ?></td> <td><?php echo $element['Longitude'] ?></td> <td><?php echo $element['Battery'] ?></td> <td><?php echo $element['seq_number'] ?></td> </tr> <?php } ?> </tbody> </table> <?php } elseif($nombre_point_geolocalisation==50){ $donner=$bdd->query('(SELECT `ID`, `Device_ID`, `Latitude`, `Longitude`, `Battery`, `seq_number`, `Recorded_at` FROM hidnseek_mangements LIMIT 0,50) ORDER BY `Recorded_at` DESC'); $donner -> execute(); $coordonnee=$donner->fetchAll(); ?> <table> <thead> <tr> <td>ID</td> <td>Device_ID</td> <td>Latitude</td> <td>Longitude</td> <td>Battery</td> <td>Seq_number</td> </tr> </thead> <tbody> <tr><?php foreach($coordonnee as $element){ ?> <td><?php echo $element['ID'] ?></td> <td><?php echo $element['Device_ID'] ?></td> <td><?php echo $element['Latitude'] ?></td> <td><?php echo $element['Longitude'] ?></td> <td><?php echo $element['Battery'] ?></td> <td><?php echo $element['seq_number'] ?></td> </tr> <?php } ?> </tbody> </table> <?php } elseif($nombre_point_geolocalisation==100){ $donner=$bdd->query('(SELECT `ID`, `Device_ID`, `Latitude`, `Longitude`, `Battery`, `seq_number`, `Recorded_at` FROM hidnseek_mangements LIMIT 0,100) ORDER BY `Recorded_at` DESC'); $donner -> execute(); $coordonnee=$donner->fetchAll(); ?> <table> <thead> <tr> <td>ID</td> <td>Device_ID</td> <td>Latitude</td> <td>Longitude</td> <td>Battery</td> <td>Seq_number</td> </tr> </thead> <tbody> <tr><?php foreach($coordonnee as $element){ ?> <td><?php echo $element['ID'] ?></td> <td><?php echo $element['Device_ID'] ?></td> <td><?php echo $element['Latitude'] ?></td> <td><?php echo $element['Longitude'] ?></td> <td><?php echo $element['Battery'] ?></td> <td><?php echo $element['seq_number'] ?></td> </tr> <?php } ?> </tbody> </table> <?php } else{ echo'Veuillez sélectionner une valeur dans le champ proposé.'; } for ($i=0;$i<=$nombre_point_geolocalisation-1;$i++){ $listedespoints[$i][0]=$coordonnee[$i]['Latitude']; $listedespoints[$i][1]=$coordonnee[$i]['Longitude']; //echo'Latitude= '.$listedespoints[$i][0].' Longitude= '.$listedespoints[$i][1].'<br/>'; } return $listedespoints ; } $infos_geo=get_value_in_dbb(10); $longueur=count($infos_geo); ?>

Avatar du membre
Modérateur PHPfrance
Modérateur PHPfrance | 8758 Messages

21 févr. 2017, 17:48

premier point réduit la taille de la fonction, tu construis dynamiquement la requête en fonction du paramètre le reste du traitement c'est exactement la même chose que tu ais 1, 10, 50 ou 100 lignes ;)

au final tu peux avoir un truc dans le genre
<?php
function get_value_in_dbb($nombre_point_geolocalisation){
   try
   {
      $bdd = new PDO('mysql:host=localhost;dbname=riadh;charset=utf8', 'root', '');
   }
   catch(Exception $e)
   {
      die('Erreur : '.$e->getMessage());
   }
// vérifie quand même que $nombre_point_geolocalisation est bien un entier dans [1,10,50,100]
    $donnees = $bdd->query('SELECT `ID`, `Device_ID`, `Latitude`, `Longitude`, `Battery`, `seq_number`, `Recorded_at` FROM hidnseek_mangements LIMIT 0,'.$nombre_point_geolocalisation.' ORDER BY `Recorded_at` DESC');
   if($donnees === false){
     // erreur retour d'un tableau vide
     return [];
   }
   $coordonnee = $donnees->fetchAll(PDO::FETCH_ASSOC);
   $donnees->closeCursor();
   ?>
   <html>
   <table>
   <thead>
   <tr>
      <td>ID</td>
      <td>Device_ID</td>
      <td>Latitude</td>
      <td>Longitude</td>
      <td>Battery</td>
      <td>Seq_number</td>
   </tr>
   </thead>
   <tbody>
      <tr><?php
      foreach($coordonnee as $element){
      echo <<<html
      <td>{$element['ID']}</td>
      <td>{$element['Device_ID']}</td>
      <td>{$element['Latitude']}</td>
      <td>{$element['Longitude']}</td>
      <td>{$element['Battery']}</td>
      <td>{$element['seq_number']}</td>
   </tr>
html;
    $listedespoints[] = [ 0 => $element['Latitude'], 1 => $element['Longitude']];
    }
?>
   </tbody>
   </table>
   <?php
  return $listedespoints ;
}

$infos_geo=get_value_in_dbb(10);
$js = '[';
$lastIndex = count($infos_geo)-1;
foreach($infos_geo as $key => $value){
    $js += '{ lat: ' . $value[0] . ' , lng:' . $value[1] . '}';
    if($key < $lastIndex){
      $js += ',';
    }
}
echo $js;
(75% de code en moins ;) )


@+
Il en faut peu pour être heureux ......

Eléphanteau du PHP | 33 Messages

21 févr. 2017, 17:54

Par contre petite précision^^ ton <<<html je l'ai pas comprit, ça ouvre un script en html ??
Et avec le code raccourci que vous m'avez donné, il y a aucune valeur qui en ressort comparé a celui d'avant ou même si on affichait pas le tableau il y avait bien qql chose
Et le script js , il faut le mettre dans var myLatLng ?

Eléphanteau du PHP | 33 Messages

21 févr. 2017, 18:09

Et si je réutilise juste ton code pour le $js avec mon "supergrandcode" ^^ La valeur de $js renvoyé est 0

Avatar du membre
Modérateur PHPfrance
Modérateur PHPfrance | 8758 Messages

21 févr. 2017, 18:10

syntaxe heredoc : http://php.net/manual/fr/language.types ... ax.heredoc

pour la variable JS oui, faut adapter un peu je ne fait pas tout ;)

echo 'var myLatLng = ',$js;


@+
Il en faut peu pour être heureux ......

Eléphanteau du PHP | 33 Messages

22 févr. 2017, 10:52

Bonjour, du coup oui avec votre informations maintenant je peux afficher tous les points présent, déjà un grand merci car j'ai bien galéré a essayer de le faire ^^

J'ai réussit à tracer une ligne entre chaque point mais j'avais déja initialement un rectange qui s'affichait sur la map, cependant en traçant les lignes le rectangle ne s'affiche plus

Code : Tout sélectionner

//Les variables ci-dessous sont globales, pour être accessibles dans la fonction init_autocomplete, ou autre<br>//on passe les points de PHP à javascript via la fonction json_encode var myLatLng = <?php echo json_encode($infos_geo,JSON_NUMERIC_CHECK)?>; //un tableau pour enregistrer les markers var markers = new Array(); //Vérifier tout de même qu'on a quelque chose dans le tableau console.log(myLatLng); for (var i=0 ; i<myLatLng.length ; i++){ var marker = new google.maps.Marker({ position: new google.maps.LatLng(myLatLng[i][0], myLatLng[i][1]), map: map, title: 'Adresse n°'+i, }); markers.push(marker); var flightPlanCoordinates = [ {lat: myLatLng[i][0], lng: myLatLng[i][1]}, {lat: myLatLng[i+1][0], lng: myLatLng[i+1][1]} ]; var flightPath = new google.maps.Polyline({ path: flightPlanCoordinates, geodesic: true, strokeColor: '#FF0000', strokeOpacity: 1.0, strokeWeight: 2 }); flightPath.setMap(map); } var bounds = { north: 47.79839667295524, // On définit les valeurs initiales du rectangle avec quatre informations south: 46.99524110694597, // La coordonné de son segment Nord, de son segment Sud, de son segment Est et de son segmen West east: 5.042724609375, west: 3.548583984375 }; // On définie le rectangle et ses différentes propriétés rectangle = new google.maps.Rectangle({ bounds: bounds, editable: true, // peremt de modifier la forme du rectangle draggable: true // permet de déplacer le rectangle }); rectangle.setMap(map); // on met le rectangle sur la carte rectangle.addListener('bounds_changed', showNewRect); infoWindow = new google.maps.InfoWindow();
Merci d'avance pour vos réponses ;)