2013-10-17 38 views
0

我有一個谷歌地圖應用程序,我在折線移動標記。我正在生成新的目標點(latlng座標)。我想將多段線擴展到這個新的目的地點。使Polyline在運行時間更長

有人能告訴我如何將折線的終點移動到地圖上的另一點。

我在這裏使用了polyline[index].getPath()我可以看到組成路線的所有座標對。如何添加到此路線使其更長或更短?

回答

2

簡單。如果你只是想補充一點添加點的現有陣列,這個工程:

var currentPath = polyline[index].getPath(); 
var newPoint = new google.maps.LatLng(40.781321,-73.965855); 
currentPath.push(newPoint); 
polyline[index].setPath(currentPath); 

但是,如果要改變其中當前最後一點是,試試這個來代替:

var currentPath = polyline[index].getPath(); 
var newPoint = new google.maps.LatLng(40.781321,-73.965855); 
currentPath.setAt(currentPath.getLength()-1, newPoint); 
polyline[index].setPath(currentPath); 
+0

這應該只更新終點?只是確認。非常感謝你知道這是怎麼回事。 – devdar

+1

啊,實際上這隻會增加一個額外的點,成爲最後一個。如果您只想將LAST端點更改爲其他地方,則代碼會稍有不同。我也會在這裏更新我的答案。 – duncan

+0

是的,請參閱我更新的答案,第二個版本只是改變陣列中最後一個點的位置是 – duncan