我有一个坐标数组,我想在Maps API v3中为其创建PolyLines。到目前为止,我已经完成了以下工作。

    for (i = 0; i < locations.length; i++) {  //locations is an array of Lat, Long
        var a = new google.maps.LatLng(locations[i][0], locations[i][1]);
        plans.push(a)
        bounds.extend(a);
    }

    var roadPath = new google.maps.Polyline({
        path: plans,
        geodesic: true,
        strokeColor: '#FF0000',
        strokeOpacity: 1.0,
        strokeWeight: 2
    });
    roadPath.setMap(map);


现在的问题是,在创建折线时,最后一点和第一点也被连接起来,如图所示



其中最左边的点是数组locations中的第一个点,最右边的是最后一个坐标。如何使红线只贴在道路上,而不像乌鸦飞一样贴在路上?

最佳答案

而不是同时pushing all个点pass pair个点,您的问题将得到解决。尝试使用pair of points绘制路径。如:第一次send origin and first点,然后second pair is first point and second point以此类推。

例如:假设您的点数组为:origin,first,seond,third,......,last,destination。然后将origin,first发送为first pair,然后将first,second发送为second pairsecond and third发送为third pair,..,last,destination作为last pair到Google路线服务或折线。

如果源和目的地相同,请确保此处不相同,然后不要发送最后一对。

07-24 09:44
查看更多