javascript - 显示路线的谷歌地图

标签 javascript google-maps

根据谷歌地图,我可以规划一条跨越多个航路点的路线。这里有解释:http://code.google.com/intl/nl-NL/apis/maps/documentation/javascript/services.html#Routes

现在 api 要我像这样添加航路点:

location: waypoints

所以 waypoints 是一个数组,我必须将其分配给 location: 参数,但从我在演示中看到的情况来看,它们用位置字符串填充数组。我想知道是否可以传递纬度和经度而不是字符串?

更新:这是我尝试创建路线的部分。我现在已经在整个循环中将相同的值放在位置中,但是如果我既不使用变量值,id 也不起作用

function calcRoute() {

    var waypts = [];

    for (var i in owt.stores.spotStore.data.map) {
        waypts.push({
            location:  new google.maps.LatLng(12.3, -33.6),
            stopover:true
        });
        console.log(waypts);
    }
    var request = {
        origin: new google.maps.LatLng(50.82788, 3.26499),
        destination: new google.maps.LatLng(50.82788, 3.26499),
        waypoints: waypts,
        optimizeWaypoints: true,
        travelMode: google.maps.DirectionsTravelMode.DRIVING
    };

    directionsService.route(request, function(response, status) {
        if (status == google.maps.DirectionsStatus.OK) {
            directionsDisplay.setDirections(response);
        }
    });
}
;

最佳答案

根据API reference :

A DirectionsWaypoint represents a location between origin and destination through which the trip should be routed.

location LatLng|string Waypoint location. Can be an address string or LatLng. Optional

因此创建一个具有经纬度值的新 Waypoint 应该如下所示

return {
    location:new google.maps.LatLng(12.3, -33.6),
    stopover:true
};

关于javascript - 显示路线的谷歌地图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5425930/

相关文章:

javascript - 如何伪造 keycloak 调用以用于本地开发?

javascript - jQuery ui 选项卡选择所需的帮助。我可以取消该操作吗?

google-maps - 如何查找与纬度/经度对应的邮政编码

javascript - 谷歌地图倾斜不起作用

javascript - Angular : Uncaught ReferenceError: $cordovaSQLite is not defined

javascript - 带有 FormData 的 Ajax 回调对大文件失败

javascript - 如何使用 native JavaScript 在 HTML DOM 事件上调用 JSF 托管 bean?

css - 巨大的谷歌地图控件(可能是错误?)

php - 如果我正在执行 "find close to me"选项,我应该在数据库中存储经度/纬度吗?

javascript - 谷歌地图有两条路线重叠