当前位置: 首页 > 知识库问答 >
问题:

谷歌地图3 API-点击功能(来自geojson)并检查它是否包含位置

陆英毅
2023-03-14

我已在地图上成功加载geojson文件。我可以单击每个多边形来更改笔划并访问其属性。但我想知道,在每个多边形中是否有某些点。

我使用google.maps.geometry.poly.containsLocation()来表示普通多边形。是否有一种方法可以从event.feature.getGeometry()提取多边形。。。要在ContainesLocation中使用或使用其他方法来检查此问题?

map.data.loadGeoJson('inc-tracts.json');
var featureStyle = {
    strokeColor: '#000000',
    strokeOpacity: 0.5,
    strokeWeight: 3,
}
map.data.setStyle(featureStyle);
map.data.addListener('click', function(event) {
    console.log(event.feature.getProperty("CTNAME"));
    // This is where I want to check if point(s) fall within it.
}

共有2个答案

通鸿风
2023-03-14

好吧,我解决了这个问题

map.data.loadGeoJson('inc-tracts.json');
var featureStyle = {
    strokeColor: '#000000',
    strokeOpacity: 0.5,
    strokeWeight: 3,
}
map.data.setStyle(featureStyle);
map.data.addListener('click', function(event) {
    testPoly = new google.maps.Polygon( { paths:event.feature.getGeometry().getAt(0).getAt(0).getArray() } );
    if ( google.maps.geometry.poly.containsLocation(somePoint, testPoly) ) {
    // This works now, still have to loop through the arrays for the multipolygons
    }
}

奇怪的是,如果我使用event.feature.getGeometry(). getAt(0)(它返回一个多边形)中的。不确定这是因为我使用的Geojson还是其他原因。

王叶五
2023-03-14

google.maps.data.多边形不是google.maps.多边形。google.maps.geometry.poly.contains位置方法有两个论证:


Methods                                         Return Value    Description
containsLocation(point:LatLng, polygon:Polygon) boolean         Computes whether the given point lies inside the specified polygon.

一个google.maps.LatLng对象和一个google.maps.Polygon(不是google.maps.data.Polygon)。您需要从google.maps.data.Polygon中的数据创建google.maps.Polygon才能使用该功能。

工作小提琴

概念验证代码片段:

function initialize() {
  // Create a simple map.
  features = [];
  map = new google.maps.Map(document.getElementById('map-canvas'), {
    zoom: 4,
    center: {
      lat: -28,
      lng: 137.883
    }
  });
  var markers = [
    [31.713127, 35.206804],
    [31.712762, 35.22028],
    [31.706117, 35.210753],
    [31.717216, 35.210066],
    [31.701152, 35.188265],
    [31.704073, 35.19144]
  ];
  var gmarkers = [];
  var info = new google.maps.InfoWindow();
  for (var i = 0; i < markers.length; i++) {
    gmarkers.push(new google.maps.Marker({
      position: {
        lat: markers[i][0],
        lng: markers[i][1]
      },
      draggable: true,
      map: map
    }));
    google.maps.event.addListener(gmarkers[gmarkers.length - 1], 'click', function() {
      info.setContent(this.getPosition().toUrlValue(6));
      info.open(map, this);
    });
  }

  google.maps.event.addListener(map, 'click', function(e) {
    document.getElementById('info').innerHTML += e.latLng.toUrlValue(6) + "<br>";
  })
  google.maps.event.addListener(map, 'click', function() {
    info.close();
  });
  // process the loaded GeoJSON data.
  var bounds = new google.maps.LatLngBounds();
  google.maps.event.addListener(map.data, 'addfeature', function(e) {
    if (e.feature.getGeometry().getType() === 'Polygon') {
      var polys = e.feature.getGeometry().getArray();
      for (var i = 0; i < polys.length; i++) {
        for (var j = 0; j < polys[i].getLength(); j++) {
          bounds.extend(polys[i].getAt(j));
        }
      }
      map.fitBounds(bounds);
    }
  });
  map.data.addGeoJson(data);
  map.data.addListener('click', function(event) {
    if (event.feature.getGeometry().getType() === 'Polygon') {
      var polyPath = event.feature.getGeometry().getAt(0).getArray();
      var poly = new google.maps.Polygon({
        paths: polyPath
      });
      for (var i = 0; i < gmarkers.length; i++) {
        if (google.maps.geometry.poly.containsLocation(gmarkers[i].getPosition(), poly)) {
          gmarkers[i].setIcon("http://maps.google.com/mapfiles/ms/icons/blue.png");
        } else {
          gmarkers[i].setIcon("");
        }
      }
    };
  });
}

google.maps.event.addDomListener(window, 'load', initialize);
var data = {
  "type": "FeatureCollection",
  "features": [{
    "type": "Feature",
    "properties": {
      "name": "test",
      "desc": "test desc",
      "inDailyProgram": true,
      "color": "red"
    },
    "geometry": {
      "type": "Polygon",
      "coordinates": [
        [
          [
            35.188327,
            31.699929,
            0
          ],
          [
            35.187895,
            31.699669,
            0
          ],
          [
            35.187014,
            31.699729,
            0
          ],
          [
            35.186867,
            31.700016,
            0
          ],
          [
            35.186783,
            31.700395,
            0
          ],
          [
            35.186921,
            31.700787,
            0
          ],
          [
            35.187232,
            31.701291,
            0
          ],
          [
            35.18763,
            31.701844,
            0
          ],
          [
            35.187442,
            31.702328,
            0
          ],
          [
            35.18692,
            31.702779,
            0
          ],
          [
            35.187064,
            31.703654,
            0
          ],
          [
            35.187433,
            31.703794,
            0
          ],
          [
            35.188155,
            31.70344,
            0
          ],
          [
            35.188921,
            31.702917,
            0
          ],
          [
            35.189348,
            31.702887,
            0
          ],
          [
            35.189828,
            31.70302,
            0
          ],
          [
            35.190313,
            31.703443,
            0
          ],
          [
            35.190359,
            31.704104,
            0
          ],
          [
            35.190224,
            31.704348,
            0
          ],
          [
            35.189797,
            31.704585,
            0
          ],
          [
            35.189753,
            31.704948,
            0
          ],
          [
            35.189847,
            31.705107,
            0
          ],
          [
            35.190187,
            31.705015,
            0
          ],
          [
            35.190604,
            31.705041,
            0
          ],
          [
            35.190931,
            31.705171,
            0
          ],
          [
            35.191435,
            31.70526,
            0
          ],
          [
            35.191861,
            31.705231,
            0
          ],
          [
            35.192482,
            31.705008,
            0
          ],
          [
            35.192945,
            31.704893,
            0
          ],
          [
            35.193564,
            31.704449,
            0
          ],
          [
            35.192869,
            31.704004,
            0
          ],
          [
            35.192256,
            31.703737,
            0
          ],
          [
            35.191754,
            31.703371,
            0
          ],
          [
            35.191306,
            31.703001,
            0
          ],
          [
            35.190838,
            31.702632,
            0
          ],
          [
            35.190546,
            31.70221,
            0
          ],
          [
            35.190348,
            31.701739,
            0
          ],
          [
            35.190323,
            31.701589,
            0
          ],
          [
            35.189814,
            31.701624,
            0
          ],
          [
            35.189443,
            31.701456,
            0
          ],
          [
            35.189108,
            31.701217,
            0
          ],
          [
            35.188509,
            31.700359,
            0
          ],
          [
            35.188327,
            31.699929,
            0
          ]
        ]
      ]
    }
  }, {
    "type": "Feature",
    "properties": {
      "name": "test",
      "desc": "test desc",
      "inDailyProgram": true,
      "color": "red"
    },
    "geometry": {
      "type": "Polygon",
      "coordinates": [
        [
          [
            35.209224,
            31.718497,
            0
          ],
          [
            35.209775,
            31.718459,
            0
          ],
          [
            35.210282,
            31.717788,
            0
          ],
          [
            35.210741,
            31.71723,
            0
          ],
          [
            35.21132,
            31.716803,
            0
          ],
          [
            35.211748,
            31.716193,
            0
          ],
          [
            35.212124,
            31.715632,
            0
          ],
          [
            35.212315,
            31.714798,
            0
          ],
          [
            35.21227,
            31.714137,
            0
          ],
          [
            35.212647,
            31.713599,
            0
          ],
          [
            35.21316,
            31.713412,
            0
          ],
          [
            35.214134,
            31.713095,
            0
          ],
          [
            35.215018,
            31.712675,
            0
          ],
          [
            35.215822,
            31.7119,
            0
          ],
          [
            35.21577,
            31.711156,
            0
          ],
          [
            35.215564,
            31.710175,
            0
          ],
          [
            35.215104,
            31.709128,
            0
          ],
          [
            35.21473,
            31.708518,
            0
          ],
          [
            35.214301,
            31.707911,
            0
          ],
          [
            35.214086,
            31.707207,
            0
          ],
          [
            35.213709,
            31.706154,
            0
          ],
          [
            35.213519,
            31.705807,
            0
          ],
          [
            35.212415,
            31.705441,
            0
          ],
          [
            35.211313,
            31.705103,
            0
          ],
          [
            35.210114,
            31.704964,
            0
          ],
          [
            35.20915,
            31.705031,
            0
          ],
          [
            35.208402,
            31.704612,
            0
          ],
          [
            35.207939,
            31.704119,
            0
          ],
          [
            35.207657,
            31.704636,
            0
          ],
          [
            35.207123,
            31.704922,
            0
          ],
          [
            35.206421,
            31.705164,
            0
          ],
          [
            35.205969,
            31.70529,
            0
          ],
          [
            35.205926,
            31.705613,
            0
          ],
          [
            35.205553,
            31.705808,
            0
          ],
          [
            35.205577,
            31.706157,
            0
          ],
          [
            35.205694,
            31.706459,
            0
          ],
          [
            35.205902,
            31.70686,
            0
          ],
          [
            35.206285,
            31.707193,
            0
          ],
          [
            35.206113,
            31.707375,
            0
          ],
          [
            35.206005,
            31.707544,
            0
          ],
          [
            35.206139,
            31.707746,
            0
          ],
          [
            35.206713,
            31.708194,
            0
          ],
          [
            35.207228,
            31.708428,
            0
          ],
          [
            35.207474,
            31.708798,
            0
          ],
          [
            35.207463,
            31.709435,
            0
          ],
          [
            35.207359,
            31.709878,
            0
          ],
          [
            35.207255,
            31.710418,
            0
          ],
          [
            35.207232,
            31.71089,
            0
          ],
          [
            35.20712,
            31.711257,
            0
          ],
          [
            35.206802,
            31.711473,
            0
          ],
          [
            35.206408,
            31.711645,
            0
          ],
          [
            35.206061,
            31.711753,
            0
          ],
          [
            35.205639,
            31.711857,
            0
          ],
          [
            35.205398,
            31.711766,
            0
          ],
          [
            35.205213,
            31.711698,
            0
          ],
          [
            35.205289,
            31.711992,
            0
          ],
          [
            35.205266,
            31.712464,
            0
          ],
          [
            35.205096,
            31.712808,
            0
          ],
          [
            35.204885,
            31.713348,
            0
          ],
          [
            35.204829,
            31.71414,
            0
          ],
          [
            35.204947,
            31.714644,
            0
          ],
          [
            35.205089,
            31.715104,
            0
          ],
          [
            35.205489,
            31.715687,
            0
          ],
          [
            35.205906,
            31.716113,
            0
          ],
          [
            35.206464,
            31.716586,
            0
          ],
          [
            35.20684,
            31.716421,
            0
          ],
          [
            35.207254,
            31.716005,
            0
          ],
          [
            35.207524,
            31.715517,
            0
          ],
          [
            35.207901,
            31.714965,
            0
          ],
          [
            35.207949,
            31.714464,
            0
          ],
          [
            35.208022,
            31.713919,
            0
          ],
          [
            35.20835,
            31.713855,
            0
          ],
          [
            35.208542,
            31.714229,
            0
          ],
          [
            35.208847,
            31.71465,
            0
          ],
          [
            35.209151,
            31.715044,
            0
          ],
          [
            35.20929,
            31.71545,
            0
          ],
          [
            35.209362,
            31.715694,
            0
          ],
          [
            35.209315,
            31.716214,
            0
          ],
          [
            35.209177,
            31.716619,
            0
          ],
          [
            35.209031,
            31.716906,
            0
          ],
          [
            35.208958,
            31.717132,
            0
          ],
          [
            35.208853,
            31.717333,
            0
          ],
          [
            35.208878,
            31.717691,
            0
          ],
          [
            35.209224,
            31.718497,
            0
          ]
        ]
      ]
    }
  }]
};
html,
body {
  height: 100%;
  margin: 0px;
  padding: 0px;
  width: 100%;
}
#map-canvas {
  height: 100%;
  width: 100%;
}
<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCkUOdZ5y7hMm0yrcCQoCvLwzdM6M8s5qk"></script>
<div id='info'></div>
<div id="map-canvas"></div>
 类似资料:
  • 我有一张地图,上面有很多自定义的大标记。现在,我希望允许用户在地图上创建路径(显示为折线,稍后保存为地理坐标对列表)。 如果用户单击地图,我可以使用地图的setOnMapClickedListener方法收集这些位置。但是如果用户点击一个标记(setOnMarkerClickedListener),我只能检索标记的位置(通常是标记的ancor的位置)。

  • 问题内容: 我正在实现是否有位图,那么它应该将图像从imageview保存到内部存储器,否则在应用程序的内部存储器中设置另一个位图。这是代码:_ 问题答案: 您可以按以下方式检查它: 只需检查Bitmap值,如下所示:

  • 我需要一些帮助来绘制我正在绘制的地图。地图并不特别复杂,因为我是一个初学者,我有一堆带有信息窗口的标记(完成后还会有更多标记),单击标记或选择页面HTML端下拉菜单的相应项时可以打开这些标记。 当信息窗口打开时(在HTML菜单中单击或选择),我想做但自己找不到的是在地图上自动居中标记。我假设有某种函数可以分配给click或infowindow打开事件,但无法确定是哪种函数以及如何实现它。 我的代码

  • 我一直在阅读GoogleMapsJSAPI的文档,我无法理解这一点。我想将geojson文件加载到地图,以便它可以显示所有标记,但在文档(如下所示)中,它使用url。除了使用本地文件(使用我自己的geojson)之外,我如何做同样的事情? 这是GoogleAPI文档 这是我的geojson。根据控制台,错误出现在“类型”:“FeatureCollection”(the):) 控制台错误 控制台错误

  • 在Android版谷歌地图V2中,当用户点击了当前位置标记(蓝色带白色边框)时,最好的识别选项是什么? 我在互联网上找不到任何例子,我能想象的唯一方法是:

  • 为了更熟悉Android编程,我一直在开发一款位置欺骗软件,我注意到我的应用程序似乎不会欺骗谷歌地图。它适用于Facebook和我测试过的其他一些应用程序,但即使关闭了Wifi和GPS,谷歌地图也知道我的确切位置,而不是我伪造的位置。我从Play Store(FakeLocation)下载了另一个欺骗应用,它似乎也没有欺骗谷歌。 这个人似乎也有同样的问题(Android Mock位置不适用于谷歌地