Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
43
rated 0 times [  47] [ 4]  / answers: 1 / hits: 9943  / 11 Years ago, tue, january 7, 2014, 12:00:00

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>

More From » php

 Answers
21

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>

[#48919] Monday, January 6, 2014, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
donaldcristianl

Total Points: 114
Total Questions: 95
Total Answers: 110

Location: Bonaire
Member since Sat, May 27, 2023
1 Year ago
;