Hi All,
I have done a few of these tutorials successfully, but I have yet to
come across a tutorial that combines these two features and I can't
seem to get there on my own. The method of populating a map with a
database full of markers that works best for me is covered in this
tutorial: //
http://code.google.com/apis/maps/articles/
phpsqlajax_v3.html
This is a two file method that uses XML (spit out with PHP) that is
read with JavaScript (thus making it AJAX) on the main page being
viewed.
XML Output:
http://hungryvt.com/map_tests/google-php/phpsqlajax_genxml5.php
Main Page:
http://hungryvt.com/map_tests/google-php/phpsqlajax_map_v3c.php
First of all, I'm not sure where to put the JS that locates the user.
I'm guessing that it should go on the main page instead of being
placed within the XML output of the business listings, since that's
server side info, and user location is client side. Correct me if I'm
wrong on that one. I put the little blue marker in there with static
info on the XML output page, thinking I could replace that with the
correct code once I figure that part out.
Second, assuming it does go on the main page, I'm not sure if the JS
code has to be placed within the "load" function that is loaded with
the body tag. My attempts at that have not worked. Placing the
geoloation code outside of the "load" function seem to have no effect
or break the map. Here is the code for the main page:
<?php
// IF NO CITY IS CHOSEN, CHOOSE BURLINGTON
if(isset($_GET['city'])){
$city_choice = $_GET['city'];
} else {
$city_choice = "1";
}
require("db_connection.php");
$db_selected = mysql_select_db($database, $conn);
if (!$db_selected) {
die ('Can\'t use db : ' . mysql_error());
}
$query_loc = "SELECT * FROM locations WHERE loc_id = '$city_choice'";
$result_loc = mysql_query($query_loc);
if (!$result_loc) {
die('Invalid query: ' . mysql_error());
}
$row_result_loc = mysql_fetch_assoc($result_loc);
?>
<!DOCTYPE html >
<!--//
http://code.google.com/apis/maps/articles/phpsqlajax_v3.html-->
<head>
<meta name="viewport" content="initial-scale=1.0, user-
scalable=no" />
<meta http-equiv="content-type" content="text/html; charset=UTF-8"/
>
<title>PHP/MySQL & Google Maps Example</title>
<script src="../../scripts/geo.js" type="text/javascript"
charset="utf-8"></script>
<script type="text/javascript" src="
http://maps.google.com/maps/
api/js?sensor=false"></script>
<script type="text/javascript">
//<![CDATA[
var customIcons = {
blue: {
icon: '
http://labs.google.com/ridefinder/images/
mm_20_blue.png',
shadow: '
http://labs.google.com/ridefinder/images/
mm_20_shadow.png'
},
bar: {
icon: '
http://labs.google.com/ridefinder/images/
mm_20_red.png',
shadow: '
http://labs.google.com/ridefinder/images/
mm_20_shadow.png'
}
};
function load() {
//geo_position_js.getCurrentPosition(show_position,function()
{document.getElementById('map').innerHTML="Couldn't get location"},
{enableHighAccuracy:true});
//var user =
navigator.geolocation.getCurrentPosition(function(position)
{(position.coords.latitude, position.coords.longitude)});
//var pos = new
google.maps.LatLng(p.coords.latitude,p.coords.longitude);
//THE THREE LINES ABOVE ARE PREVIOUS FAILED ATTEMPTS
var map = new google.maps.Map(document.getElementById("map"), {
center: new google.maps.LatLng(<?php echo
$row_result_loc['loc_lat'];?>, <?php echo $row_result_loc['loc_lng'];?
>),
zoom: 12,
mapTypeId: 'roadmap'
});
var infoWindow = new google.maps.InfoWindow;
// Change this depending on the name of your PHP file
downloadUrl("phpsqlajax_genxml5.php", function(data) {
var xml = data.responseXML;
var markers =
xml.documentElement.getElementsByTagName("marker");
for (var i = 0; i < markers.length; i++) {
var name = markers[i].getAttribute("name");
var address = markers[i].getAttribute("address");
var citystate = markers[i].getAttribute("citystate");
var phone = markers[i].getAttribute("phone");
var type = markers[i].getAttribute("type");
var point = new google.maps.LatLng(
parseFloat(markers[i].getAttribute("lat")),
parseFloat(markers[i].getAttribute("lng")));
var html = "<b>" + name + "</b> <br/>" + address + "<br/>"
+citystate + "<br/>" + phone;
var icon = customIcons[type] || {};
var marker = new google.maps.Marker({
map: map,
position: point,
icon: icon.icon,
shadow: icon.shadow
});
bindInfoWindow(marker, map, infoWindow, html);
}
});
}
function bindInfoWindow(marker, map, infoWindow, html) {
google.maps.event.addListener(marker, 'click', function() {
infoWindow.setContent(html);
infoWindow.open(map, marker);
});
}
function downloadUrl(url, callback) {
var request = window.ActiveXObject ?
new ActiveXObject('Microsoft.XMLHTTP') :
new XMLHttpRequest;
request.onreadystatechange = function() {
if (request.readyState == 4) {
request.onreadystatechange = doNothing;
callback(request, request.status);
}
};
request.open('GET', url, true);
request.send(null);
}
function doNothing() {}
//]]>
</script>
</head>
<body onload="load()">
<div id="map" style="width: 600px; height: 600px"></div>
</body>
</html>