如何更新地图的状态



我正在尝试在单击地图时创建一个多边形,每次向数组添加新的 LatLng 对象时,我都会将其分配给多边形对象,但我意识到多边形也保存了较旧的值,并且不会仅获取新创建的数组的值。分配新更新的数组后,如何重新渲染多边形。

        function add_point(map, location) {
           upcoming_point = {lat: location.lat(), lng:location.lng() }
           poligon_array.push(upcoming_point)
           if(polygon_array.length>5){
             polygon_array.shift();
           }
           create_Polygon(polygon_array);
         }
         function create_Polygon(polygon_array) {
             var Polygon = new google.maps.Polygon({
                 paths: polygon_array,
                 strokeColor: '#FF0000',
                 strokeOpacity: 0.8,
                 strokeWeight: 2,
                 fillColor: '#FF0000',
                 fillOpacity: 0.35
               });
              Polygon.setMap(map);
          }

Polygon.setPath(newArrayOfPoints)将更新多边形。

var polygon;
function add_point(map, location) {
  upcoming_point = {lat: location.lat(), lng:location.lng() }
  poligon_array.push(upcoming_point)
  if(polygon_array.length > 5){
    polygon_array.shift();
  }
  if (polygon) {
    polygon.setPath(polygon_array);
  } else {
    polygon = create_Polygon(polygon_array);
  }
}
function create_Polygon(polygon_array) {
  var Polygon = new google.maps.Polygon({
    paths: polygon_array,
    strokeColor: '#FF0000',
    strokeOpacity: 0.8,
    strokeWeight: 2,
    fillColor: '#FF0000',
    fillOpacity: 0.35
  });
  Polygon.setMap(map);
  return Polygon;
}

相关内容

  • 没有找到相关文章

最新更新