簡體   English   中英

繼續在Google Maps v3中繪制折線

[英]Continue drawing a polyline in google maps v3

是否可以使用Google的DrawingManager為現有折線打開繪圖模式?

轉到自定義地圖編輯器(菜單中的“我的地圖”->創建地圖->繪制)時,可以在Google Maps網絡應用中使用。 然后創建一條折線,右鍵單擊最后一點,然后選擇延伸線(請參見下圖)。

但是我找不到與Google Maps API v3相同的方法。

Google Maps-我的地圖

將折線的editable -option設置為true ,並將點添加到折線路徑

例:

  • 單擊該行可使該行可編輯
  • 點擊地圖停止編輯
  • 在頂點上單擊鼠標右鍵,同時延長線會停止編輯

 function initMap() { var goo = google.maps, map = new goo.Map(document.getElementById('map'), {}), line = new goo.Polyline({ path: [ {lat: 37.772, lng: -122.214}, {lat: 21.291, lng: -157.821}, {lat: -18.142, lng: 178.431}, {lat: -27.467, lng: 153.027} ]}), bounds = new goo.LatLngBounds(); line.getPath().getArray().forEach(function(ll){ bounds.extend(ll); }); map.fitBounds(bounds); line.setMap(map); /** * click on the line makes the line editable * * click on the map stops editing * * rightclick on the vertex while extending the line stops editing * **/ goo.event.addListener(line,'click',function(e){ var line=this; //make the line editable this.setEditable(true); //stopediting on map-click goo.event.addListenerOnce(this.getMap(),'click',function(){ line.setEditable(false); }); //when a vertex has been clicked if(typeof e.vertex==='number'){ //when the first or last vertex has been clicked if(!e.vertex ||e.vertex===this.getPath().getLength()-1){ //when the first vertex has been clicked reverse the path to //be able do push a vertex if(e.vertex===0){ var p=this.getPath().getArray(); p.reverse(); this.setPath(p); } //push a vertex this.getPath().push(e.latLng) //observe the mousemove of the map to set the latLng of the last vertex var move=goo.event.addListener(this.getMap(),'mousemove',function(e){ line.getPath().setAt(line.getPath().getLength()-1,e.latLng) }); //stop editing on rightclick goo.event.addListenerOnce(this,'rightclick',function(){ goo.event.removeListener(move); this.setEditable(false); }); } } }); } 
 html, body { height: 100%; margin: 0; padding: 0; } #map { height: 100%; } 
 <script async defer src="https://maps.googleapis.com/maps/api/js?v=3&callback=initMap"></script> <div id="map"></div> 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM