如何循环访问Google Maps API的旅行模式 [英] How to loop through Google Maps API's Travel mode

查看:147
本文介绍了如何循环访问Google Maps API的旅行模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图通过循环访问google.maps.TravelMode属性来使用JavaScript来获取一组从Google Maps JavaScript API驾驶,行走和骑车的路线。我试图通过使用方括号和方法名称来实现此目的,但是当我这样做时,我收到以下错误消息:


Uncaught InvalidValueError:in property travelMode:undefined


以下是循环:

  var methods = new Array(DRIVING,WALKING,BICYCLING); 
$ .each(methods,function(method){
request.travelMode = google.maps.TravelMode [method];
directionsService.route(request,function(response){
directionsDisplay.setDirections(response);
console.log(directionsDisplay.getDirections());
});
});

我相信这是上面代码部分第三行的错误。



如果我将google.maps.TravelMode对象打印到控制台,它会显示以下内容:

 对象{
驾驶:驾驶,
步行:步行,
自行车:自行车,
过境: TRANSIT

有什么想法?

$。each 的回调函数有两个参数key和value,您必须使用第二个参数(当前为您尝试访问 google.maps.TravelMode.0 google.maps.TravelMode.1 google.maps.TravelMode.2 ):

  $。each(methods,function(key ,方法){/ ** /} 


I am attempting to use JavaScript to get a set of directions for driving, walking and bicycling from the Google Maps JavaScript API by looping through the google.maps.TravelMode properties. I am trying to do this by using a square bracket and the method name, however I get the following error message when I do it:

Uncaught InvalidValueError: in property travelMode: undefined

Here is the loop:

var methods = new Array("DRIVING", "WALKING", "BICYCLING");
            $.each(methods, function(method) {
                request.travelMode = google.maps.TravelMode[method];
                directionsService.route(request, function(response) {
                    directionsDisplay.setDirections(response);
                    console.log(directionsDisplay.getDirections());
                });
            });

I believe it to be an error with the third line in the code section above.

If I print out the google.maps.TravelMode object to the console, it comes up with the following:

Object {
    DRIVING: "DRIVING",
    WALKING: "WALKING",
    BICYCLING: "BICYCLING",
    TRANSIT: "TRANSIT"
} 

Any ideas?

解决方案

the callback of $.each has two arguments, key and value, you must use the 2nd argument(currently you try to access google.maps.TravelMode.0 , google.maps.TravelMode.1 , google.maps.TravelMode.2):

$.each(methods, function(key, method){/**/}

这篇关于如何循环访问Google Maps API的旅行模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆