Google使用坐标映射路线

时间:2015-09-29 19:42:33

标签: google-maps

请帮助我,我需要在谷歌地图上添加不同颜色路线的路线,并在可能的情况下为它们制作动画。

1 个答案:

答案 0 :(得分:0)

您可以使用以下javascript代码: 请注意,lat_lng是纬度和经度数组。

        //Initialize the Path Array.  
    var path = new google.maps.MVCArray();

    //Getting the Direction Service.  
    var service = new google.maps.DirectionsService();

    //Set the Path Stroke Color.  
    var poly = new google.maps.Polyline({
        map: map, strokeColor: '#4986E7', strokeOpacity: 0.8, strokeWeight: 2
    });

    //Loop and Draw Path Route between the Points on MAP.  
    for (var i = 0; i < lat_lng.length; i++) {
        if ((i + 1) < lat_lng.length) {
            var src = lat_lng[i];
            var des = lat_lng[i + 1];
            path.push(src);
            poly.setPath(path);
            service.route({
                origin: src,
                destination: des,
                travelMode: google.maps.DirectionsTravelMode.DRIVING
            }, function (result, status) {
                if (status == google.maps.DirectionsStatus.OK) {
                    for (var i = 0, len = result.routes[0].overview_path.length; i < len; i++) {
                        path.push(result.routes[0].overview_path[i]);
                    }
                }
                else {
                    alert("Invalid location.");
                }
            });
        }
    }