Google Map Drawing freehand

后端 未结 2 679
鱼传尺愫
鱼传尺愫 2021-01-31 06:21

\"enter

LATEST CODE - http://jsfiddle.net/YsQdh/88/ -

THIS VERSION USES gDouglasPe

相关标签:
2条回答
  • 2021-01-31 06:56

    function drawFreeHand()
    {
    
        //the polygon
        poly=new google.maps.Polyline({map:map,clickable:false});
        
        //move-listener
        var move=google.maps.event.addListener(map,'mousemove',function(e){
            poly.getPath().push(e.latLng);
        });
        
        //mouseup-listener
        google.maps.event.addListenerOnce(map,'mouseup',function(e){
            google.maps.event.removeListener(move);
            var path=poly.getPath();
            poly.setMap(null);
            poly=new google.maps.Polygon({map:map,path:path});
            
            
            google.maps.event.clearListeners(map.getDiv(), 'mousedown');
            
            enable()
        });
    }
    
    function disable(){
      map.setOptions({
        draggable: false, 
        zoomControl: false, 
        scrollwheel: false, 
        disableDoubleClickZoom: false
      });
    }
    
    function enable(){
      map.setOptions({
        draggable: true, 
        zoomControl: true, 
        scrollwheel: true, 
        disableDoubleClickZoom: true
      });
    }
    
    
    function initialize() 
    {
      var mapOptions = {
        zoom: 14,
        center: new google.maps.LatLng(52.5498783, 13.425209099999961),
        mapTypeId: google.maps.MapTypeId.ROADMAP
      };
      map = new google.maps.Map(document.getElementById('map_canvas'), mapOptions);
           
      //draw
      $("#drawpoly a").click(function(e) {
        e.preventDefault();
    
        disable()
    
        google.maps.event.addDomListener(map.getDiv(),'mousedown',function(e){
          drawFreeHand()
        });
    
      });
    
    }
    
    google.maps.event.addDomListener(window, 'load', initialize);
    html,body{height:100%;margin:0}
    #map_canvas{height:500px; width:100%;}
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    
    <script src="https://maps.googleapis.com/maps/api/js?sensor=false&libraries=geometry,places&ext=.js"></script>
    
    <div id="drawpoly"><a href="#">Click Here To Draw On Map</a></div>
    
    <div id="map_canvas"></div>

    0 讨论(0)
  • 2021-01-31 07:00

    The latest answer - http://jsfiddle.net/YsQdh/94/

    This contains the gDouglasPeuker algorithm

    var theArrayofLatLng = path.j;
    var ArrayforPolygontoUse= GDouglasPeucker(theArrayofLatLng,50); 
    console.log("ArrayforPolygontoUse", ArrayforPolygontoUse);        
    
    
    var polyOptions = {
        map: map,
        fillColor: '#0099FF',
        fillOpacity: 0.7,
        strokeColor: '#AA2143',
        strokeWeight: 2,
        clickable: false,
        zIndex: 1,
        path:ArrayforPolygontoUse,
        editable: false
    }
    
    0 讨论(0)
提交回复
热议问题