Skip to content Skip to sidebar Skip to footer

Array Multiple Points In Google Maps With Sql & Php?

I a newbie in PHP. The code above works fine, but only with one marker. If I use top 1 to search one point it's ok. When I change the top nubmer like 10 still shows one or last mar

Solution 1:

Only create the map once (currently you are creating a new map with one marker on it for each row in your database, when the query ends, the last one remains):

functioninitialize() {
  var bounds = new google.maps.LatLngBounds();
  // initialize the mapvar mapOptions = {
         center: new google.maps.LatLng(0,0),
         zoom: 13
  };
  var map = new google.maps.Map(document.getElementById("map-canvas"),
                        mapOptions);

<?php

... then add markers to it

  $result = mssql_query($query);

  while($row = mssql_fetch_assoc($result))
  {  
    echo'var myLatlng1 = new google.maps.LatLng('.$row['lat'].', '.$row['long'].');                                  
    var marker = new google.maps.Marker({ 
                            position: myLatlng1, 
                            map: map, 
                            animation: google.maps.Animation.DROP,
                            title:"'.$row['Cabno'].','.$row['Hora'].'"
                          });
     bounds.extend(marker.getPosition());';
   }
 ?>// center and zoom the map to show all the markers.
   map.fitBounds(bounds);
 }
 google.maps.event.addDomListener(window, 'load', initialize);

Solution 2:

  1. Are you sure your SQL request return several results?
  2. Put it before your loop:

    var mapOptions = { center: new google.maps.LatLng( '. $row['lat'].', '.$row['long'].'), zoom: 13 }; var map = new google.maps.Map(document.getElementById("map-canvas"), mapOptions); var myLatlng1 = new google.maps.LatLng('. $row['lat'].', '.$row['long'].');

There is perhaps some other mistakes. You could find it useful: https://developers.google.com/maps/documentation/javascript/examples/map-latlng-literal

Post a Comment for "Array Multiple Points In Google Maps With Sql & Php?"