2012-06-27 88 views
0

我想從谷歌地圖v3 api創建一個折線。我知道如何在點擊中添加多段線,但我想從緯度&經度值創建折線,這將在循環中。我怎樣才能做到這一點 ?谷歌地圖中的谷歌縱橫經度創建Polyine?

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 //////////////////// 


    } 


} 

謝謝。

+0

你到目前爲止試過了什麼?你的問題沒有顯示什麼是循環。顯示代碼。 –

+0

在這裏,我在split_lat&split_lon變量的循環中獲取緯度和經度值,我想用標記折線繪製標記,標記已經繪製。謝謝 –

+0

謝謝我編輯了這個問題。 –

回答

1

查看折線的文檔。它需要LatLng的數組。

循環遍歷你的和點,將它們放在一個數組中,並通過構造函數或.setPath()將它們傳遞給PolyLine。

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 
    }); 

} 
+0

謝謝里克你能給我簡單的例子! –