在googlemap api javascript中获取地图中的所有折线(使用绘图管理器)

时间:2016-05-19 06:01:57

标签: javascript google-maps google-polyline

如何获取我使用绘图管理器创建的所有折线? 我无法使用DrawingManager事件监听器,因为插入的折线是可编辑的(DrawingManager中的事件监听器只能在第一次插入后才能工作)。
并且可以绘制多条折线。

这是我的代码

function initMap(){

        //set center coordinate
        var myLat= /*value*/;
        var myLng= /*value*/;
        var center= {lat:myLat, lng:myLng};

        //create map
        var map = new google.maps.Map(document.getElementById('map'), {
            zoom: 16,
            center: center
        });

        addDrawingControl(map);
    }

  function addDrawingControl(map){
        //add drawing control
        var drawingControl = new google.maps.drawing.DrawingManager(
        {
            drawingMode : null,
            drawingControl : true,
            drawingControlOptions :{
                position : google.maps.ControlPosition.TOP_CENTER,
                drawingModes : [
                google.maps.drawing.OverlayType.POLYLINE
                ]
            },
            polylineOptions : {
                editable:true,
                draggable:false,
                geodesic:true
            }
        });
        drawingControl.setMap(map);
        //end of add drawing control

        //add event listener
        google.maps.event.addListener(drawingControl, 'polylinecomplete', function(polyline){
                var polylinePath = polyline.getPath();
                console.log("polyline : "+polylinePath.getArray());                 
        });
    }

1 个答案:

答案 0 :(得分:1)

保持对折线的引用。

var polylines = []; // array in the global scope

polylinecomplete事件上,将折线的引用添加到数组中。

//add event listener
google.maps.event.addListener(drawingControl, 'polylinecomplete', function(polyline) {
  polylines.push(polyline);
});

如果要捕获数据,请遍历数组,检索折线的当前状态。

google.maps.event.addDomListener(document.getElementById('btn'), 'click', function() {
  var htmlStr = "";
  for (var i = 0; i < polylines.length; i++) {
    htmlStr += "polyline #" + i + " # vertices=" + polylines[i].getPath().getLength() + " length=" + google.maps.geometry.spherical.computeLength(polylines[i].getPath()).toFixed(2) + " km<br>";
    for (var j = 0; j < +polylines[i].getPath().getLength(); j++) {
      htmlStr += "&nbsp;&nbsp;" + polylines[i].getPath().getAt(j).toUrlValue(6) + "<br>";
    }
  }
  document.getElementById('output').innerHTML = htmlStr;
});

proof of concept fiddle

代码段

var polylines = [];

function initMap() {
  //set center coordinate
  var myLat = 42;
  var myLng = -72;
  var center = {
    lat: myLat,
    lng: myLng
  };

  //create map
  var map = new google.maps.Map(document.getElementById('map'), {
    zoom: 16,
    center: center
  });

  addDrawingControl(map);
}

function addDrawingControl(map) {
  //add drawing control
  var drawingControl = new google.maps.drawing.DrawingManager({
    drawingMode: null,
    drawingControl: true,
    drawingControlOptions: {
      position: google.maps.ControlPosition.TOP_CENTER,
      drawingModes: [
        google.maps.drawing.OverlayType.POLYLINE
      ]
    },
    polylineOptions: {
      editable: true,
      draggable: false,
      geodesic: true
    }
  });
  drawingControl.setMap(map);
  //end of add drawing control

  //add event listener
  google.maps.event.addListener(drawingControl, 'polylinecomplete', function(polyline) {
    polylines.push(polyline);
    var polylinePath = polyline.getPath();
    console.log("polyline : " + polylinePath.getArray());
  });
  google.maps.event.addDomListener(document.getElementById('btn'), 'click', function() {
    var htmlStr = "";
    for (var i = 0; i < polylines.length; i++) {
      htmlStr += "polyline #" + i + " # vertices=" + polylines[i].getPath().getLength() + " length=" + google.maps.geometry.spherical.computeLength(polylines[i].getPath()).toFixed(2) + " km<br>";
      for (var j = 0; j < +polylines[i].getPath().getLength(); j++) {
        htmlStr += "&nbsp;&nbsp;" + polylines[i].getPath().getAt(j).toUrlValue(6) + "<br>";
      }
    }
    document.getElementById('output').innerHTML = htmlStr;
  })
}
google.maps.event.addDomListener(window, "load", initMap);
html,
body,
#map {
  height: 100%;
  width: 100%;
  margin: 0px;
  padding: 0px
}
<script src="https://maps.googleapis.com/maps/api/js?libraries=geometry,drawing"></script>
<input id="btn" value="get polyline data" type="button" />
<div id="output"></div>
<div id="map"></div>