Hello,
I have a question about getting directions with the Google Maps API. I
looked at the examples and put something together. It worked, it
showed the directions in the sidebar and updated the map, visualizing
the route. Now I wanted to merge that to another part of my project,
just a map which gets the users location. I managed to put both
together but now it only shows the direction in the sidebar but does
not show the route in the map. The map does not change. There is
something I am missing and it is driving me crazy. Can you help me?
Thanks in advance
Mickey
This is the map HTML part:
<h1>Map</h1>
<table border=1>
<tr>
<td>
<div id="map_canvas" style="width: 550px; height:
450px"></div>
</td>
<td width = 350 valign="top" >
<div id="side_bar" style="text-decoration:
underline; color:
#4444ff;"><a href="javascript:showdirections();">Show directions to
nearest center</a></div>
<div id="directions"
style="overflow:auto;height:390px"><hr /></
div>
</td>
</tr>
</table>
<!-- <div id="map_canvas" style="width: 750px; height:
600px;"></
div> -->
and the script I used (I know it is a mess):
<script type="text/javascript">
var initialLocation;
var stuttgart = new google.maps.LatLng(12, 105);
var browserSupportFlag = new Boolean();
var map;
var infowindow = new google.maps.InfoWindow();
var centerpositions = new Array();
centerpositions[0] = new google.maps.LatLng(60,105);
centerpositions[1] = new google.maps.LatLng(40, 105);
var gdir;
var directionDisplay;
function getnearestpoint(position)
{
var nearestpointnr = 0;
var nearestdistance = 99999;
var curdistance = 0;
for (var i = 0; i<centerpositions.length; i++)
{
curdistance = distHaversine(position, centerpositions[i]);
if (curdistance < nearestdistance)
{
nearestdistance = curdistance;
nearestpointnr = i;
}
}
return nearestpointnr;
}
rad = function(x) {return x*Math.PI/180;}
distHaversine = function(p1, p2) {
var R = 6371; // earth's mean radius in km
var dLat = rad(p2.lat() - p1.lat());
var dLong = rad(p2.lng() - p1.lng());
var a = Math.sin(dLat/2) * Math.sin(dLat/2) +
Math.cos(rad(p1.lat())) * Math.cos(rad(p2.lat())) *
Math.sin(dLong/2) * Math.sin(dLong/2);
var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
var d = R * c;
return d.toFixed(3);
}
function initialize() {
directionsDisplay = new google.maps.DirectionsRenderer();
GUnload();
shownextcenterdirectionsinit();
var myOptions = {
zoom: 6,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(document.getElementById("map_canvas"),
myOptions);
/*map = new GMap2(document.getElementById("map_canvas"), myOptions);
map.addControl(new GLargeMapControl());
map.addControl(new GMapTypeControl());
map.setCenter(new GLatLng(43.907787,-79.359741), 9);*/
// from getdirection
//map.setCenter(new GLatLng(43.907787,-79.359741), 9);
// ____
google.maps.event.trigger(map, 'resize');
// W3C
if(navigator.geolocation) {
browserSupportFlag = true;
navigator.geolocation.getCurrentPosition(function(position) {
initialLocation = new
google.maps.LatLng(position.coords.latitude,position.coords.longitude);
contentString = "Location found using W3C standard";
map.setCenter(initialLocation);
infowindow.setContent(contentString);
infowindow.setPosition(initialLocation);
infowindow.open(map);
}, function() {
handleNoGeolocation(browserSupportFlag);
});
} else {
// Browser doesn't support Geolocation
browserSupportFlag = false;
handleNoGeolocation(browserSupportFlag);
}
}
function handleNoGeolocation(errorFlag) {
if (errorFlag == true) {
initialLocation = stuttgart;
contentString = "Error: The Geolocation service failed.";
} else {
initialLocation = stuttgart;
contentString = "Error: Your browser does not support W3C
Geolocation. This is the Big Cat Rescue Center in Stuttgart";
}
map.setCenter(initialLocation);
infowindow.setContent(contentString);
infowindow.setPosition(initialLocation);
infowindow.open(map);
}
function shownextcenterdirectionsinit()
{
// from getdirection
// === create a GDirections Object ===
gdir=new GDirections(map,
document.getElementById("directions"));
}
function GetRouteBetweenPoints(startadress, endadress)
{
var options = {};
options.avoidHighways = false;
gdir.load("from: "+startadress+" to: "+endadress, options);
}
function showdirections()
{
// === Array for decoding the failure codes ===
var reasons=[];
reasons[G_GEO_SUCCESS] = "Success";
reasons[G_GEO_MISSING_ADDRESS] = "Missing Address: The
address was either missing or had no value.";
reasons[G_GEO_UNKNOWN_ADDRESS] = "Unknown Address: No
corresponding geographic location could be found for the specified
address.";
reasons[G_GEO_UNAVAILABLE_ADDRESS]= "Unavailable Address: The
geocode for the given address cannot be returned due to legal or
contractual reasons.";
reasons[G_GEO_BAD_KEY] = "Bad Key: The API key is
either invalid or does not match the domain for which it was given";
reasons[G_GEO_TOO_MANY_QUERIES] = "Too Many Queries: The daily
geocoding quota for this site has been exceeded.";
reasons[G_GEO_SERVER_ERROR] = "Server error: The geocoding
request could not be successfully processed.";
reasons[G_GEO_BAD_REQUEST] = "A directions request could
not be successfully parsed.";
reasons[G_GEO_MISSING_QUERY] = "No query was specified in
the input.";
reasons[G_GEO_UNKNOWN_DIRECTIONS] = "The GDirections object
could not compute directions between the points.";
// === catch Directions errors ===
GEvent.addListener(gdir, "error", function() {
var code = gdir.getStatus().code;
var reason="Code "+code;
if (reasons[code]) {
reason = reasons[code]
}
alert("Failed to obtain directions, "+reason);
});
// Read the data from example.xml
var request = GXmlHttp.create();
request.open("GET", "example.xml", true);
request.onreadystatechange = function() {
}
request.send(null);
GetRouteBetweenPoints("49.48240137826932,8.448486328125", "Mainz");
}
</script>
--
You received this message because you are subscribed to the Google Groups
"Google Maps API V2" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/google-maps-api?hl=en.