0

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>
1
  • If you used the Google Maps store locator tutorial that I think you did, then the tutorial itself shows you how to add markers to the map! Commented Jan 7, 2014 at 16:30

1 Answer 1

1

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>
Sign up to request clarification or add additional context in comments.

2 Comments

I have a problem with the maps code, i can't get maps to show up at all with the code u sent, where did you get the code from? thanks
Have a look at this page: gmaps-samples-v3.googlecode.com/svn/trunk/xmlparsing/…, if you grab the source from there you should be able to get it working.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.