Oscar Andersson
Oscar Andersson

Reputation: 5

Multiple markers on google maps using PHP arrays with data from Mysql database

So my first post on stack overflow, I have 2 PHP arrays that contains latitude and longitude. The values that these variables store depends on what the user searches for.

I have a simple map that i got from the google maps api tutorial, How can i add multiple markers to the map using only latitude and longitude from php arrays?

I am very grateful for any kind response to this

 <head>
<link type="text/css" rel="stylesheet" href="searchnext.css"/>
<script
src="http://maps.googleapis.com/maps/api/js?key=AIzaSyDY0kkJiTPVd2U7aTOAwhc9ySH6oHxOIYM&sensor=false">
</script>

<script>
function initialize()
{
var mapProp = {
  center:new google.maps.LatLng(57.708742,11.820850),
  zoom:10,
  mapTypeId:google.maps.MapTypeId.ROADMAP
  };
var map=new google.maps.Map(document.getElementById("googleMap"),mapProp);
}


google.maps.event.addDomListener(window, 'load', initialize);
</script>
</head>
  <body>



<div id="googleMap" style="width:500px;height:380px;"></div>

 <?php

include_once'config/connect.php';
$search= $_POST['search'];
// To protect MySQL injection (more detail about MySQL injection)
$search = stripslashes($search);

$search = mysql_real_escape_string($search);

$sql= "select * from venue where vID in (select vID from sv where sID in (select sID from sports where sN = '$search'));";
$result=mysql_query($sql);

$latitude = array();
$longitude = array();

while( $row1 = mysql_fetch_array($result)){
    array_push($latitude, $row1['latitude']);

}
while( $row2 = mysql_fetch_array($result)){
    array_push($longitude, $row2['longitude']);

}

echo $latitude[1];
echo $latitude[2];
echo $latitude[3];
?>
</body>

Upvotes: 0

Views: 10039

Answers (1)

Ian Jamieson
Ian Jamieson

Reputation: 4836

I would suggest using an XML document of markers which is generated by PHP.

You can have a look at the format of the XML document here:

http://gmaps-samples-v3.googlecode.com/svn/trunk/xmlparsing/

So all you will need to do is create an XML file using your PHP data like this one:

http://gmaps-samples-v3.googlecode.com/svn/trunk/xmlparsing/data.xml

You can read about how to do that here:

How to generate XML file dynamically using PHP?

And then use this code to display the markers:

<script type="text/javascript">
function initialize() {
  var myLatlng = new google.maps.LatLng(37.4419, -122.1419);
  var myOptions = {
    zoom: 13,
    center: myLatlng,
    mapTypeId: google.maps.MapTypeId.ROADMAP
  }
  var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
  downloadUrl("data.xml", function(data) {
    var markers = data.documentElement.getElementsByTagName("marker");
    for (var i = 0; i < markers.length; i++) {
      var latlng = new google.maps.LatLng(parseFloat(markers[i].getAttribute("lat")),
                                  parseFloat(markers[i].getAttribute("lng")));
      var marker = new google.maps.Marker({position: latlng, map: map});
     }
   });
}
</script>

Upvotes: 1

Related Questions