Drag (move) a polygon using Google Maps v3

前端 未结 5 899
清歌不尽
清歌不尽 2021-01-02 15:49

The Google Maps API for a Polygon does not offer a drag method.

What would be an efficient way of implementing such a feature (i.e., sufficiently optimised so that i

5条回答
  •  执念已碎
    2021-01-02 16:23

    Here's how I do it. Find the approximate center of the polygon, and add a marker, then add a drag listener to the marker. On lat/lng change, subtract the difference from the original marker lat/lng, subtract the difference to each of the paths, then, set the original position to the new position. Make sure that in your javascript api call that you have library=geometry,drawing

    google.maps.event.addListener(draw, 'overlaycomplete', function(shape) {
    // POLYGON
          if (shape.type == 'polygon') {
            var bounds = new google.maps.LatLngBounds(); var i;  
            var path = shape.overlay.getPath();
            for (i = 0; i < path.length; i++) { bounds.extend(path.getAt(i)); }
            shape.latLng = bounds.getCenter();
            marker = getMarker(map,shape);
            shape.overlay.marker = marker;
            markers.push(marker); 
          }
          google.maps.event.addListener(marker, 'drag', function(event) {
             shape.overlay.move(event.latLng, shape, path);
          });
    
              google.maps.event.addListener(shape.overlay, 'rightclick', function() {
                this.setMap(null);
                this.marker.setMap(null);
                draw.setDrawingMode('polygon');
              });
    
      });
    }
    google.maps.Polygon.prototype.move = function(latLng, shape, p) {
        var lat = latLng.lat();
        var lng = latLng.lng();
    
        latDiff = shape.latLng.lat()-lat;
        lngDiff = shape.latLng.lng()-lng;
    
       for (i = 0; i < p.length; i++) {
        pLat = p.getAt(i).lat();
        pLng = p.getAt(i).lng();
        p.setAt(i,new google.maps.LatLng(pLat-latDiff,pLng-lngDiff));
       }
       shape.latLng = latLng; 
    }
    function getMarker(map,shape){
      var infowindow = new google.maps.InfoWindow();
      if(shape.type=='polygon'){ latLng = shape.latLng; }
      marker = new google.maps.Marker({
                  position: latLng,
                  map:map,
                  draggable:true,
                  clickable: true,
                  animation: google.maps.Animation.DROP
                });
               shape.overlay.marker = marker;
               shape.overlay.bindTo('center',marker,'position');
               google.maps.event.addListener(marker, 'click', (function(marker) {
                return function() {
                  infowindow.setContent('polygon');
                  infowindow.open(map, marker);
                  toggleBounce(marker);
                }
              })(marker));
              google.maps.event.addListener(infowindow,'closeclick', (function(marker) {      
                return function() {
                marker.setAnimation(null);
                }
              })(marker));
     return marker;
    }
    

    If you have any questions, feel free to contact me.

提交回复
热议问题