从地图框 API 获取地理 JSON 数据以确定多边形中的点
Get geoJSON data from mapbox API to determine point in polygon
我最近按照本教程来确定标记显示的点是否位于定义的多边形内:https://www.mapbox.com/mapbox.js/example/v1.0.0/point-in-polygon/
效果很好,但他们使用 ajax 提取多边形地理 JSON 数据并将其应用于地图。我已经绘制了一张绘制了多边形区域的地图框地图。我正在使用他们的 API 来显示我的地图。我现在需要访问 geoJSON 数据来确定点是否在区域内。如何通过 API 访问该数据?
以下是我如何提取地图、geoJSON 和一个函数来测试一个点是否在 geoJSON 区域中:
//mapbox connection
var mapID = "<map-id>";
L.mapbox.accessToken = "<my-token>";
//init map
var map = L.mapbox.map("mapData", mapID, {
attributionControl: false,
zoomControl: false
}).setView([53.799, -1.548], 13, {
pan: { animate: true },
zoom: { animate: true }
});
//get geoJSON
var geoJson;
$http.get("https://a.tiles.mapbox.com/v4/" + mapID + "/features.json?access_token=" + L.mapbox.accessToken).success(function(data){
geoJson = data;
});
//determine point in polygon
function determinePointInPolygon(){
var coords = [-1.3, 5.2];
var layer = leafletPip.pointInLayer(coords, L.geoJson(geoJson), true);
if (!layer.length) //point not in polygon
else //point is in polygon
}
集成功能将加载到L.mapbox.featureLayer
中,该可作为L.mapbox.Map
实例的成员使用。假设您将地图实例存储在名为map
图层将map.featureLayer
的引用中:
// Make sure the featureLayer is ready
map.featureLayer.on('ready', function (e) {
// Loop over the features
e.target.eachLayer(function (layer) {
// Do your thing
// here "layer" holds an instance of the marker/polygon/polyline
// "layer.feature" holds the actual geojson feature
});
});
下面是该概念的示例:http://plnkr.co/edit/D5IfRTLV0yXTqOmzNCYA?p=preview
如果您愿意,您还可以单独加载切片和要素,无需mapid
即可实例化地图:
var map = L.mapbox.map('mapbox', null, {
'center': [0, 0],
'zoom': 1
});
var tileLayer = L.mapbox.tileLayer('yourMapId').addTo(map);
var featureLayer = L.mapbox.featureLayer('yourMapId').addTo(map);
示例:http://plnkr.co/edit/9pYeRu6UmxuVL1TLzwPR?p=preview
然后,您可以省略addTo
方法,首先处理要素,然后将图层添加到地图:
var featureLayer = L.mapbox.featureLayer('yourMapId');
// Make sure the featureLayer is ready
featureLayer.on('ready', function (e) {
// Loop over the features
e.target.eachLayer(function (layer) {
// Do your thing
});
// Add layer to the map
e.target.addTo(map);
});
什么最适合你。但是我差点忘了实际回答你的问题(虽然我认为你不再需要这个了),实际的GeoJSON特征集合可以通过使用L.mapbox.FeatureLayer
的getGeoJSON
方法获得(当然,只有当图层准备好时):
// Make sure the featureLayer is ready
featureLayer.on('ready', function (e) {
// Fetch you featurecollection
var geojson = e.target.getGeoJSON();
});
当你把它拉进去时,你可以将JSON存储在其他地方:
首先定义一个对象,你想在全局的某个地方存储它:
var geoJSON;
然后,当您将其拉入时,只需使用数据定义它
$.ajax({
url: '/mapbox.js/assets/data/us-states.geojson',
dataType: 'json',
success: function load(d) {
geoJSON = d; // THIS IS THE LINE I ADDED
var states = L.geoJson(d).addTo(map);
L.marker([38, -102], {
icon: L.mapbox.marker.icon({
'marker-color': '#f86767'
}),
draggable: true
}).addTo(map)
.on('dragend', function(e) {
var layer = leafletPip.pointInLayer(this.getLatLng(), states, true);
if (layer.length) {
state.innerHTML = '<strong>' + layer[0].feature.properties.name + '</strong>';
} else {
state.innerHTML = '';
}
});
}
});
- 用程序搜索JQuery数据表中的文本
- 要求输入在数据列表中
- 正在将数据主题添加到所有项目
- 函数参数中的数据与指定变量之间的任何性能差异
- 在VanillaJS中模拟模型双向数据绑定
- CSS-如何定位内容数据标题
- 使用电话间隙在Android应用程序中显示SQL Lite的数据
- 无法在数据endVal中设置值=“”;{{ucount}}”;使用Angular JS的CountUp
- 序列化数据属性中对象的最可靠方法
- 如何将JSON数据导入我的ejs模板
- 不显示带有本地json文件数据的谷歌地图脚本
- 如何将json数据显示为html
- 画布数据到图像
- 从传单数据到GeoJSON的多边形
- 从地图框 API 获取地理 JSON 数据以确定多边形中的点
- 谷歌地图/融合表Javascript HTML赢得't显示/可视化所有多边形数据(通过色标)
- 通过传单中来自 csv 的数据定义 json 多边形的颜色
- PHP 或 Jquery,用于解释和格式化多边形映射的 json 数据
- 在GeoJson数据接收到的谷歌地图多边形上贴上标签
- 从地图数据中删除特定多边形