javascript - 从多边形数组中删除多边形子集

标签 javascript arrays google-maps-api-3

我在谷歌地图上按组绘制多边形。每个组都有一个独特的颜色,传递到 html/JavaScript 中。

从 Delphi 调用

function MarkArea(Lat, Lng, otherColor) {
    var latLng = new google.maps.LatLng(Lat,Lng);
    drawUserGrids(latLng, otherColor);
}

js中调用的函数

    function drawUserGrids(point, otherColor) {
    // Square limits
 //  these are QtrMinutes stored in the database and drawn
        var bottomLeftLat = (Math.floor(point.lat() / llOffset) * llOffset);
        var bottomLeftLong = (Math.floor(point.lng() / llOffset) * llOffset);

        var gridLineSquare = [
            new google.maps.LatLng(bottomLeftLat, bottomLeftLong),  //lwr left
            new google.maps.LatLng(bottomLeftLat, (bottomLeftLong + llOffset)),  //lwr right
            new google.maps.LatLng((bottomLeftLat + llOffset), (bottomLeftLong + llOffset)),  //upr right
            new google.maps.LatLng((bottomLeftLat + llOffset), bottomLeftLong)];  //upr left


        drawGridBox = true;

        if (drawGridBox == true) {
            gridUserArea = new google.maps.Polygon({
                path: gridLineSquare,
                draggable:false,
                geodesic:true,
                editable :false,
                fillColor:  otherColor,   << unique
                fillOpacity: 0.35,
                strokeColor: "#CC0099",
                strokeOpacity: 0.1,
                strokeWeight: 1
            });

            gridUserArea.setMap(map);
            userGridArray.push(gridUserArea);
        }
    }

通过填充颜色取消映射组的函数

function deListOneColor(otherColor){
    if (userGridArray) {
        for (var i in userGridArray) {
          if (userGridArray[i].gridUserArea.fillColor == otherColor)
            userGridArray[i].setMap(null);
        }
    }
}

我的目标是为用户提供一种根据颜色取消映射特定区域的方法。

JS 向我抛出一个错误: 无法获取未定义或空引用的属性“fillColor”。

我是否正确访问了多边形?

最佳答案

MVCE

google.maps.Polygon 没有 userGridArea 属性(如果需要,您可以创建,但不需要)。这对我有用:

function deListOneColor(otherColor) {
  if (userGridArray) {
    for (var i in userGridArray) {
      if (userGridArray[i].get("fillColor") == otherColor)
        userGridArray[i].setMap(null);
    }
  }
}

proof of concept fiddle

代码片段:

var map;
var userGridArray = [];

function initialize() {
  map = new google.maps.Map(
    document.getElementById("map_canvas"), {
      center: new google.maps.LatLng(37.4419, -122.1419),
      zoom: 8,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    });
  drawUserGrids(map.getCenter(), "#FF0000");
  drawUserGrids(new google.maps.LatLng(37.639097, -120.996878), "#0000FF");
  google.maps.event.addDomListener(document.getElementById('deletebtn'), 'click', function() {
    deListOneColor(document.getElementById('color').value);
  });
}
google.maps.event.addDomListener(window, "load", initialize);

var llOffset = 0.25;

function drawUserGrids(point, otherColor) {
  // Square limits
  //  these are QtrMinutes stored in the database and drawn
  var bottomLeftLat = (Math.floor(point.lat() / llOffset) * llOffset);
  var bottomLeftLong = (Math.floor(point.lng() / llOffset) * llOffset);

  var gridLineSquare = [
    new google.maps.LatLng(bottomLeftLat, bottomLeftLong), //lwr left
    new google.maps.LatLng(bottomLeftLat, (bottomLeftLong + llOffset)), //lwr right
    new google.maps.LatLng((bottomLeftLat + llOffset), (bottomLeftLong + llOffset)), //upr right
    new google.maps.LatLng((bottomLeftLat + llOffset), bottomLeftLong)
  ]; //upr left


  drawGridBox = true;

  if (drawGridBox == true) {
    gridUserArea = new google.maps.Polygon({
      path: gridLineSquare,
      draggable: false,
      geodesic: true,
      editable: false,
      fillColor: otherColor,
      fillOpacity: 0.35,
      strokeColor: "#CC0099",
      strokeOpacity: 0.1,
      strokeWeight: 1
    });

    gridUserArea.setMap(map);
    userGridArray.push(gridUserArea);
  }
}

function deListOneColor(otherColor) {
  if (userGridArray) {
    for (var i in userGridArray) {
      if (userGridArray[i].get("fillColor") == otherColor)
        userGridArray[i].setMap(null);
    }
  }
}
html,
body,
#map_canvas {
  height: 100%;
  width: 100%;
  margin: 0px;
  padding: 0px
}
<script src="https://maps.googleapis.com/maps/api/js"></script>
<input type="button" value="delete" id="deletebtn" />
<input value="#FF0000" id="color" />
<div id="map_canvas"></div>

关于javascript - 从多边形数组中删除多边形子集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34122161/

相关文章:

javascript - 如何从对象内部调用类函数?

javascript - 从另一个域加载音频

javascript - jQuery 中的 '#' 符号是什么意思?

java - 无法从 jsonobject 中提取 jsonarray

用于在多维数组中查找相邻值的 C++ 数据结构

javascript - 禁用或更改 Google map api 3 上位置的默认行为

javascript - Google map javascript api bug android 浏览器(Jelly bean 及之前版本)

javascript - 如何将 json 放入我的 View 中?

java - 如何创建一组空数组并用用户输入填充它并打印出来?

java - 获取特定位置的 map View 我选择纬度和经度