gmaps:如何添加多个目的地

gmaps: how to add multiple destinations

我正在使用 gmaps 和 JQuery。我想在地图中显示从 A 点到 B 点,然后到 C 点的方向。我正在使用此代码,但它不起作用:

<script type="text/javascript">
    var map;
    $(document).ready(function(){
        map = new GMaps({
            div: '#map',
            lat: -12.043333,
            lng: -77.028333
        });
        map.drawRoute({
            origin: [-12.044012922866312, -77.02470665341184],
            destination: [-12.090814532191756, -77.02271108990476],
            destination: [-12.044012922866312, -77.02470665341184],
            travelMode: 'driving',
            strokeColor: '#131540',
            strokeOpacity: 0.6,
            strokeWeight: 6
        });
    });
</script>

您传递给 map.drawRoute() 函数的对象文字有 destination 属性 重复了两次。一个 JavaScript 对象只能有一个 属性 同名,所以第二个 destination 会覆盖第一个

documentation for .drawRoute() 表示您应该为中间 waypoints 使用 waypoints 数组。虽然我还没有测试过,但我怀疑你想要的代码看起来像这样:

        map.drawRoute({
            origin: [-12.044012922866312, -77.02470665341184],
            waypoints: [
                {
                    location: new google.maps.LatLng(
                        -12.090814532191756,
                        -77.02271108990476
                    ),
                    stopover: true
                }
            ],
            destination: [-12.044012922866312, -77.02470665341184],
            travelMode: 'driving',
            strokeColor: '#131540',
            strokeOpacity: 0.6,
            strokeWeight: 6
        });