Google Maps v3 API获取路线持续时间失败 [英] Google Maps v3 API getting route duration fails

查看:97
本文介绍了Google Maps v3 API获取路线持续时间失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您能否向我解释为什么我在这段代码中没有得到 google.maps.DirectionsStatus.OK

  function measureDistance(m1,m2){
var wp = new Array();
var lstart = new google.maps.LatLng(markers [m1] .position);
var lend = new google.maps.LatLng(markers [m2] .position);
var request = {
origin:lstart,
destination:lend,
waypoints:[],
optimizeWaypoints:true,
travelMode:google.maps .DirectionsTravelMode.DRIVING
};
directionsService.route(request,function(response,status){
if(status == google.maps.DirectionsStatus.OK){
response.routes [0] .legs [0] .duration.text;
} else {
document.getElementById('locationD')。value =drawRouteBad;
}

});
}

标记是一个全球字典与来自地图的标记。 directionsService 是全局定义的(并且在另一个函数中相同的 directionsService 适用于我)。问题是,出于某种原因,我无法得到正确的答复(我在我的网站上的一个字段中有drawRouteBad)。

div>

问题是,我不需要创建新的LatLng对象。


$ b

  var lstart = markers [m1] .position; 
var lend = markers [m2] .position;


Could you please explain to me why I don't get google.maps.DirectionsStatus.OK in this piece of code?

function measureDistance(m1, m2) {
    var wp = new Array();
    var lstart = new google.maps.LatLng(markers[m1].position);
    var lend = new google.maps.LatLng(markers[m2].position);
    var request = {
        origin : lstart,
        destination : lend,
        waypoints : [],
        optimizeWaypoints: true,
        travelMode : google.maps.DirectionsTravelMode.DRIVING
    };
    directionsService.route(request, function(response, status) {
        if (status== google.maps.DirectionsStatus.OK) {
            response.routes[0].legs[0].duration.text;
        } else {
            document.getElementById('locationD').value = "drawRouteBad";
        }

    });
}

Markers is a global dictionary with markers from the map. directionsService is defined globally (and the same directionsService works for me in another function). The problem is, that for some reason I can't get correct response (I've got drawRouteBad in one of fields on my website).

解决方案

The problem was, that I don't need to create new LatLng objects. What is inside markers[].position is enough for start and end.

 var lstart = markers[m1].position;
 var lend = markers[m2].position;

这篇关于Google Maps v3 API获取路线持续时间失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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