简体   繁体   中英

Get Latitude and Longitude from result of google map v3 direction api?

I'm using Google map v3 direction api in my project.

I give cities name as a start point and end point for getting Distance and Time Taken for that distance.

I have another requirement to get longitude and latitude of start point which is city name.

Can I get it from direction api which is used by me to get distance and time taken in result of direction api?

If we can get latitude and longitude from result of direction api, then please help me by giving proper answer.

thanks in advance.

The response does not contain a lat/lng string. You have to get it using a function.

Using jquery this works for me.

$.each(result.routes[0].legs, function(index, route) {
alert(route.start_location.lat());
});

This will alert each starting latitude for each leg of the journey you requested.

From a quick look at the responses section of the directions API documentation , it looks like you can get the start_location -> lat and start_location -> lng the same way you are already getting the distance and time.

I guess that would be:

routes.legs[0].start_location.lat
routes.legs[0].start_location.lng

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM