directionsService.route (request,
function (result, status)
开发者_如何学JAVA {
alert(status);
if (status == google.maps.DirectionsStatus.OK)
{
directionsDisplay.setDirections (result);
pointsArray = result.routes[0].overview_path;
The problem is that the *result.routes[0].overview_path;* returns an array of type 'google.maps.LatLng'. i.e. Each element is an "object" of the class 'google.maps.LatLng', in that array.
I want to write all those returned latitudes and longitudes on a text file.
How to extract the individual "latitudes and longitudes" values from the object of the class "google.maps.LatLng"
http://code.google.com/apis/maps/documentation/javascript/reference.html#LatLng
The above link contains two methods: lat()
and lng()
which do solve the issue.
The code for the same is as follows:
function displayRoute ()
{
var start = arrayMarkers [0];
var end = arrayMarkers [1];
var request = {
origin:start,
destination:end,
travelMode:google.maps.TravelMode.DRIVING
};
directionsService.route (request, function (result, status)
{
if (status == google.maps.DirectionsStatus.OK)
{
directionsDisplay.setDirections (result);
pointsArray = result.routes[0].overview_path;
var i = 0;
var j = 0;
for (j = 0; j < pointsArray.length; j++)
{
arrayToBeReturned [i] = pointsArray[j].lat ();
i++;
arrayToBeReturned [i] = pointsArray[j].lng ();
i++;
var point1 = new google.maps.Marker ({
position:pointsArray [j],
draggable:false,
map:map,
flat:true
});
}
}
});
}
精彩评论