gmaps:如何添加多个目的地

时间:2015-05-13 02:05:45

标签: jquery google-maps google-maps-api-3 gmaps.js

我正在使用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>

1 个答案:

答案 0 :(得分:2)

您传递到map.drawRoute()函数的对象文字具有重复两次的destination属性。 JavaScript对象只能有一个具有相同名称的属性,因此第二个destination会覆盖第一个。

documentation for .drawRoute()表示您应该使用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
        });