Event handler for editing a Google Maps Polyline?

前端 未结 1 539
北恋
北恋 2020-12-28 18:31

I\'m looking for an event that fires while you are editing a Polyline in Google Maps, similar to the \'drag\' event on Markers. I\'ve found the \'capturing_changed\' event,

1条回答
  •  别那么骄傲
    2020-12-28 19:25

    The simple Polyline example from the documentation modified to add events on changes (insert_at, remove_at, set_at, dragend).

    
    
      
        
        
        Google Maps JavaScript API v3 Example: Polyline Simple
        
        
        
      
      
        

    working example

    code snippet:

    var flightPath = null;
    
    function initialize() {
      var myLatLng = new google.maps.LatLng(0, -180);
      var mapOptions = {
        zoom: 3,
        center: myLatLng,
        mapTypeId: google.maps.MapTypeId.TERRAIN
      };
    
      var map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
    
      var flightPlanCoordinates = [
        new google.maps.LatLng(37.772323, -122.214897),
        new google.maps.LatLng(21.291982, -157.821856),
        new google.maps.LatLng(-18.142599, 178.431),
        new google.maps.LatLng(-27.46758, 153.027892)
      ];
      flightPath = new google.maps.Polyline({
        path: flightPlanCoordinates,
        strokeColor: '#FF0000',
        strokeOpacity: 1.0,
        strokeWeight: 2,
        editable: true,
        draggable: true
      });
      google.maps.event.addListener(flightPath, "dragend", getPath);
      google.maps.event.addListener(flightPath.getPath(), "insert_at", getPath);
      google.maps.event.addListener(flightPath.getPath(), "remove_at", getPath);
      google.maps.event.addListener(flightPath.getPath(), "set_at", getPath);
      flightPath.setMap(map);
    }
    
    function getPath() {
      var path = flightPath.getPath();
      var len = path.getLength();
      var coordStr = "";
      for (var i = 0; i < len; i++) {
        coordStr += path.getAt(i).toUrlValue(6) + "
    "; } document.getElementById('path').innerHTML = coordStr; } google.maps.event.addDomListener(window, 'load', initialize);
    
    

    0 讨论(0)
提交回复
热议问题