Google maps V3 directions service fails to find roads close to selected point resulting in wrong directions
Greetings,
Until now I have mostly been working with google maps v2. I am now making my way to v3, but I came across a problem I havent been able to find a solution for.
In V2, when using directions, I could add a point on the map with no actual road, and the maps api would automatically figure out the closest possible endpoint where a road exists and display directrions to that point. This seems not to be the case with V3 though. In allot of cases the route is wrong, because I haven't placed my point on an actual road and the service fails (?) to find the closest one. Here is a screen开发者_StackOverflowshot to display what I mean:
I am using the exact same coordinates for both maps. The red pins on V3 show where those coordinates actually point to, but as you can see the directions are wrong. Only if I use coordinates that are exactly on a road, then they show up correctly (not displayed here). I am using the exact same coordinates on V2 and V3, but only V2 displays the correct directions.
Here is a sample of the code I am using for getting the direcrtions:
var directionDisplay;
var directionsService = new google.maps.DirectionsService();
var map;
function initialize() {
//Works fine, so I ommit the code
}
function calcRoute(endpointCoords) // endpointCoords holds coordinates for end point
{
var startpoint = '<?=$startPoint[latitude];?>, <?=$startPoint[longitude];?>';
var endpoint = endpointCoords;
var request = {
origin:startpoint,
destination:endpoint,
travelMode: google.maps.DirectionsTravelMode.DRIVING
};
directionsService.route(request, function(response, status){
if (status == google.maps.DirectionsStatus.OK)
{
directionsDisplay.setDirections(response);
}
});
}
The documentation did not help me much further ( http://code.google.com/apis/maps/documentation/javascript/services.html#Directions )
Have you had similar experience with the v3 version of the api? Any ideas why this happens?
UPDATE: Here is a link that shows the code and problem in action: http://jsfiddle.net/spairus/gNpZ2/
From the graphic, it looks like your lng/lat might be in the wrong order. As suggested here, you might want to make sure you are using the longitude and latitude in the correct order.
精彩评论