【问题标题】:Google maps drawing path works till some point谷歌地图绘制路径一直有效
【发布时间】:2013-03-25 12:51:48
【问题描述】:

我有一个带有 latlang 值的数组,我想绘制一个遵循这个值的路径。这就是我所做的,但它只画到某个点。当我 console.log '结果'时,期待绘制的对象,它会打印

Uncaught Error: Error in property <routes>: (Cannot read property 'routes' of null)

function renderDirections(result) {

  var directionDisplay = new google.maps.DirectionsRenderer();
  directionDisplay.setMap(map);
  directionDisplay.setDirections(result);
  directionDisplay.setOptions({suppressMarkers: true});
}

for(var i=0; i < array.length; i++){

                    var marker = new google.maps.Marker({
                    position: new google.maps.LatLng(array[i].lat, array[i].lng),
                    map: map                                 
                    });

                    var directionsService = new google.maps.DirectionsService();
                    directionsService.route({
                    origin:  new google.maps.LatLng(array[i].lat, array[i].lng),
                    destination:  new google.maps.LatLng(array[i+1].lat, array[i+1].lng),
                    unitSystem: google.maps.UnitSystem.IMPERIAL,
                    travelMode: google.maps.DirectionsTravelMode.DRIVING
                    },
                    function(result){
                      renderDirections(result);
                    });
                }                   

【问题讨论】:

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


    【解决方案1】:

    检查DirectionsService 的“状态”。 DirectionsService 受配额和费率限制。

                    directionsService.route({
                      origin:  new google.maps.LatLng(array[i].lat, array[i].lng),
                      destination:  new google.maps.LatLng(array[i+1].lat, array[i+1].lng),
                      unitSystem: google.maps.UnitSystem.IMPERIAL,
                      travelMode: google.maps.DirectionsTravelMode.DRIVING
                    },
                    function(result, status){
                      if (status == google.maps.DirectionsStatus.OK)
                        renderDirections(result);
                      else alert ("Directions request failed:"+status);
                    });
    

    “Uncaught TypeError: Cannot read property 'lat' of undefined”的问题是由于你的逻辑。当您收到最后一个路线请求时,array[i+1] 不存在。试试这个:

    for(var i=0; i < array.length-1; i++){
    

    example using function closure

    This example 将多个请求串在一起也可能对您有所帮助。

    【讨论】:

    • 现在它没有绘制任何路径,并给出了“未捕获的类型错误:无法读取未定义的属性 'lat'”错误以及您希望我写的警报。但它给出了一个空状态它只是打印“路线请求失败:”。
    • 听起来您所做的不仅仅是添加状态检查。我建议的检查中有错字,但无法解释您看到的症状。
    • 我刚刚添加了你写的两行。当我评论它们时,它仍然像我在问题中所问的那样画...
    • "Cannot read property 'lat' of undefined" 实际上是无关紧要的,它打印它是因为它在那个时候完成了循环,所以这不是问题。添加状态检查后,它停止渲染路径并且只警告“方向请求失败:”没有状态
    • 错过了。您的“i”超出了数组的末尾。这是一个重复的问题,您可以使用函数闭包来保留数组索引的值,以调用路线服务。但是,一旦你解决了这个问题,你就会得到 OVER_QUERY_LIMIT(至少我是这样)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多