切换谷歌地图折线和多边形之间

Toggle between google map polyline and polygon

本文关键字:多边形 之间 折线 谷歌地图      更新时间:2023-09-26

根据这个stackoverflow答案中的答案,我绘制了一个示例谷歌地图折线,如图所示。在这里,我试图在折线和多边形之间切换基于单选按钮。我尝试创建另一个多边形数组

 polygon= new goo.Polygon({
               paths: [],
               geodesic: true,
               strokeColor: '#FF0000',
               strokeOpacity: 1.0,
               strokeWeight: 2
           }),

并尝试在点击单选按钮时绘制地图,

if($("#polyline").is(":checked")) {
line.setMap(map);
} else {
polygon.setMap(map);
}

我还在每个事件中设置了适当的坐标polygon.getPaths()。但是它给出了一些错误信息。

代码片段(来自fiddle in comments):

var Line = [];
var polygon = [];
var Latlngs = [];
var Path;
function initMap() {
  var goo = google.maps;
  map = new goo.Map(document.getElementById('map'), {
    zoom: 10,
    center: new goo.LatLng(12.8799313333, 77.5991386667)
  });
  line = new goo.Polyline({
    path: [],
    geodesic: true,
    strokeColor: '#FF0000',
    strokeOpacity: 1.0,
    strokeWeight: 2,
    map: map
  });
  polygon = new goo.Polygon({
    paths: [],
    geodesic: true,
    strokeColor: '#FF0000',
    strokeOpacity: 1.0,
    strokeWeight: 2,
    map: map
  });
  markers = new goo.MVCArray;
  goo.event.addListener(map, 'click', function(e) {
    var marker = new goo.Marker({
      position: e.latLng,
      map: map,
      draggable: true
    });
    markers.push(marker);
    //push new point to the path
    line.getPath().push(marker.getPosition());
    polygon.getPaths().push(marker.getPosition());
    goo.event.addListener(marker, 'dragend', function() {
      //simply update the path
      line.getPath().setAt(markers.indexOf(this),
        this.getPosition());
      polygon.getPaths().setAt(markers.indexOf(this),
        this.getPosition());
    });
    goo.event.addListener(marker, 'dblclick', function() {
      //remove marker and path-point
      line.getPath().removeAt(markers.indexOf(this));
      polygon.getPaths().removeAt(markers.indexOf(this));
      markers.removeAt(markers.indexOf(this));
      this.setMap(null)
    });
  });
  $("radBut input").click(function() {
    if ($(this).id == "polygon") {
      polygon.setMap(map);
      line.setPath([]);
    } else {
      line.setMap(map);
      polygon.setPaths([]);
    }
  });
}
google.maps.event.addDomListener(window, 'load', initMap);
html,
body {
  height: 100%;
  margin: 0;
  padding: 0;
}
#map {
  height: 100%;
}
<script src="http://www.google.com/jsapi"></script>
<script src="https://maps.googleapis.com/maps/api/js?v=3.exp"></script>
<div id="radBut">
  <input type="radio" name="poly" id='polyline' checked>Polyline</input>
  <input type="radio" name="poly" id='polygon'>Polygon</input>
</div>
<br/>
<div id="map"></div>

编辑:我已经添加了一个按钮来清除绘制的线/多边形

     $("#clear").click(function () {
       line.setPath([]);
       polygon.setPath([]);
       for (var i = markers.length; i > 0; i--) {
           markers.removeAt(i);
           markers.getArray()[i - 1].setMap(null);
       }
       Latlngs = [];
   });

一旦我清除了绘制的地图,如果我再次绘制另一条线并试图通过双击它来删除一个标记,则双击事件无法正常工作。小提琴在

UPDATE:上述编辑中的问题通过将标记重置为markers = new goo.MVCArray;来解决。

我在这里创建了一个工作的Fiddle,你可以根据你的代码在折线和多边形之间切换。

我不得不对你的代码做一些改变来让它工作:

。用多边形的path性质代替paths2将多边形对象的映射设置为null,以阻止多边形在折线的顶部被渲染(因为单选按钮被设置为折线)

polygon = new goo.Polygon({
    path: [],
    ...
    map: null
});

iii。将polygon.getPaths().push(marker.getPosition());的代码行改为polygon.getPath().push(marker.getPosition());,因为我们现在使用的是path属性(而不是paths)。

第四。修改拖放和dblclick事件处理程序以使用polygon.getPath()而不是polygon.getPaths():

goo.event.addListener(marker, 'dragend', function () {
    ...
    polygon.getPath().setAt(markers.indexOf(this), this.getPosition());
 });
goo.event.addListener(marker, 'dblclick', function () {
    ...
    polygon.getPath().removeAt(markers.indexOf(this));
    ...
});

第四。将$("radBut input")事件处理程序修改为:

$("#radBut input").click( function() {
    if($(this).attr('id') == "polygon") {
        polygon.setMap(map);
        line.setMap(null);
    } else {
        line.setMap(map);
        polygon.setMap(null);
    }
});

注意,jQuery CSS选择器现在是$("#radBut input")而不是$("radBut input"),我们使用$(this).attr('id')来获得所选单选按钮的id。也没有必要将折线或多边形路径设置为空数组(这将清除多边形或数组点)。相反,使用setMap(null);将从地图中删除折线或多边形,但保留数组中的点。