2017-01-18 45 views
0

允許用戶在地圖上繪製一個圓圈,然後您可以添加標記 - 如果標記在圓圈外,那麼應該放大圓圈以將標記放入內部。Google Maps JS API - 延長圈子

我找不到文檔中任何事情:

有沒有一種可能,如:UserCircle.extend(event.overlay.location); 雖然事件疊加的標記。

我也嘗試過自己製作這個功能 - 但是我被困在半徑上。 哪個單位?我怎樣才能將它轉換成度數?

任何想法?

+0

閱讀:http://stackoverflow.com/questions/37997093/trying-to-get-google-map-with-user-set-radius –

回答

0

多數民衆贊成我現在...

function extendRegion(marker, drawing){ 
    marker_lat = marker.getPosition().lat(); 
    marker_lng = marker.getPosition().lng(); 
    if(drawing.type == google.maps.drawing.OverlayType.CIRCLE){ 
     var path = [marker.getPosition(), drawing.overlay.getCenter()]; 
     var length = google.maps.geometry.spherical.computeDistanceBetween(path[0], path[1]); 

     if(length>drawing.overlay.getRadius()){ 
      consloe.log('The Marker was outside the drawing, it is extended now.'); 
      drawing.overlay.setRadius(length); 
     } 
    } 
    else if(drawing.type == google.maps.drawing.OverlayType.RECTANGLE){ 
     ne_lat = drawing.overlay.getBounds().getNorthEast().lat(); 
     ne_lng = drawing.overlay.getBounds().getNorthEast().lng(); 
     sw_lat = drawing.overlay.getBounds().getSouthWest().lat(); 
     sw_lng = drawing.overlay.getBounds().getSouthWest().lng(); 

     var error=0; 
     if(marker_lat<sw_lat){ 
      sw_lat = marker_lat; 
      error=1; 
     } 
     if(marker_lat>ne_lat){ 
      ne_lat = marker_lat; 
      error=1; 
     } 
     if(marker_lng<sw_lng){ 
      sw_lng = marker_lng; 
      error=1; 
     } 
     if(marker_lng>ne_lng){ 
      ne_lng = marker_lng; 
      error=1; 
     } 

     if(error) { 
      console.log('The Marker was outside the drawing, it is extended now.'); 
      drawing.overlay.setBounds({north: ne_lat, south: sw_lat, east: ne_lng, west: sw_lng}); 
     } 
    } 
}