DirectionsService.Route - 尝试访问lat,lng无效

时间:2015-10-16 14:50:35

标签: google-maps google-maps-api-3

从DestinationsService.Route响应中,我想访问latlng值,如下所示:

var latitude0 = routes[0].legs[0].steps[0].start_location.lat;
var longitude0 = routes[0].legs[0].steps[0].start_location.lng;
console.log("var ["+latitude0+","+longitude0+"]");

使用Firefox调试器查看DestinationsService.Route,json响应如下所示:

"routes" : [
  {
     "bounds" : {...}
     },
     "copyrights" : "Map data ©2015 Google",
     "legs" : [
        {
           "distance" : {...},
           "duration" : {...},
           "end_address" : "something",
           "end_location" : {...},
           "start_address" : "another thing",
           "start_location" : {...},
           "steps" : [
              {
                 "distance" : {...},
                 "duration" : {
                    "text" : "2 mins",
                    "value" : 104
                 },
                 "end_location" : {
                    "lat" : 14.64548629999999,
                    "lng" : 21.0152624
                 },
                 "html_instructions" : "...",
                 "polyline" : {
                    "points" : "..."
                 },
                 "start_location" : {
                    "lat" : 14.64537809999999,
                    "lng" : 17.9754639
                 },
                 "travel_mode" : "DRIVING"

我可以访问steps[0]中的所有其他字段和值,例如

routes[0].legs[0].steps[0].travel_mode

给了我“DRIVING”或

routes[0].legs[0].steps[0].duration.text

给了我“2分钟”。

但是当按照start_locationend_location的开头lat和lng中的说明进行访问时,它会给我(这是控制台打印输出):

var [function (){"use strict"; return a},function (){"use strict";return b}]

我做错了什么? 在此先感谢您的帮助。

1 个答案:

答案 0 :(得分:2)

google.maps.LatLng对象具有lat / lng方法,您需要调用它们以获取适当的值。而不是:

var latitude0 = routes[0].legs[0].steps[0].start_location.lat;
var longitude0 = routes[0].legs[0].steps[0].start_location.lng;

执行:

var latitude0 = routes[0].legs[0].steps[0].start_location.lat();
var longitude0 = routes[0].legs[0].steps[0].start_location.lng();

working fiddle