0

我正在尝试从谷歌地图 v3 api 创建一条折线。我知道如何在点击时添加折线,但我想从纬度和经度值创建折线,这将在循环中。我怎样才能做到这一点 ?

    var network_url = decodeURIComponent(document.URL);
    //var network_url = " http://testNetwork.com?param[]=(12.983147333223707, 77.49687217431642)&param[]=(12.95035965289418, 77.54871391015627)";

    var location_array = split_url(network_url);
    for(var i = 0; i<location_array.length; i++)
    {

         //(12.960062579720846,%2077.48313926416017)
        var loc = location_array[i] ; //==>(12.77,  24.87)

        var split_arr1 = loc.split("(");
        var result_split_arr1 = split_arr1[1].split( ")" );

        var split_latLon = result_split_arr1[0];
        var result_split = split_latLon.split(",");

        var split_lat = result_split[0];
        var split_lon = result_split[1];

        marker = new google.maps.Marker({
        position: new google.maps.LatLng(split_lat, split_lon),
        map: map
        });



        ///////////POLYLINE DRAWing /////////////////////////////

        /////////////END OF POLYLINE ////////////////////


     }


}

谢谢你。

4

1 回答 1

1

查看折线的文档。它需要一个 LatLng 的数组。

循环遍历您的和点,将它们放入数组中,然后通过构造函数或 .setPath() 将它们传递给折线。

https://developers.google.com/maps/documentation/javascript/reference#Polyline

编辑:示例。(我没有运行它)。

    var location_array = split_url(network_url);
    var path = [];
    for(var i = 0; i<location_array.length; i++)
    {

         //(12.960062579720846,%2077.48313926416017)
        var loc = location_array[i] ; //==>(12.77,  24.87)

        var split_arr1 = loc.split("(");
        var result_split_arr1 = split_arr1[1].split( ")" );

        var split_latLon = result_split_arr1[0];
        var result_split = split_latLon.split(",");

        var split_lat = result_split[0];
        var split_lon = result_split[1];
        var latLng = new google.maps.LatLng(split_lat, split_lon);
        path.push(latLng)
        marker = new google.maps.Marker({
        position: latLng,
        map: map
        });

     }

    var flightPath = new google.maps.Polyline({
       path: path,
       strokeColor: "#FF0000",
       strokeOpacity: 1.0,
       strokeWeight: 2,
       map: map
    });

}
于 2012-06-27T11:14:56.070 回答