在我将KMZ转换为KML然后转换为JSON文件后,我得到了以下结构:
{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"geometry": {
"type": "Polygon",
"coordinates": [
[
[lon1, lat1],
[lon2, lat2],
[lon3, lat3],
...
]
]
},
"properties": {
"property1": "value1",
"property2": "value2",
...
}
},
在我的脑海中找到GPS点,我应该迭代坐标点并检查点是否在GEOJSON多边形内,所以我使用turf.js库中的代码
// define the URL for the GeoJSON data
const url = 'path/geojson.json';
// define the GPS point to check
const pointToCheck = turf.point([-6.542, 34.224]);
// fetch the GeoJSON data
fetch(url)
.then(response => response.json())
.then(myJSON => {
// loop through each feature in the GeoJSON data
myJSON.features.forEach(feature => {
// get the polygon geometry for this feature
const polygon = feature.geometry;
// check if the GPS point is inside the polygon geometry
const isInside = turf.booleanPointInPolygon(pointToCheck, polygon);
// if the GPS point is inside the polygon, log the feature's properties
if (isInside) {
console.log(feature.properties);
// loop through each set of coordinates in the polygon
polygon.coordinates.forEach(coordinates => {
coordinates.forEach(coordinate => {
console.log(coordinate);
});
});
}
});
})
.catch(error => {
console.error('Error fetching GeoJSON data:', error);
});
UPDATE我正确上传JSON文件
我的问题是,因为我从来没有使用过Turf.js,所以这个函数booleanPointInPolygon(pointToCheck, polygon);
是否可以正确地检查我的点是否在这个几何体内
我试图从JSON中获取数据,如果我给予他的点在该GEOJSON文件中存在的几何图形内
1条答案
按热度按时间vuktfyat1#
只使用Google Maps JavaScript API v3几何库(而不是turf)的一个选项是在geometry library中使用
containsLocation
方法:proof of concept fiddle
代码片段: