Javascript 使用Mapbox从地图中获取要素

Javascript 使用Mapbox从地图中获取要素,javascript,leaflet,mapbox,geojson,Javascript,Leaflet,Mapbox,Geojson,我正在使用多边形形状文件,我想打开和关闭地图上某些要素的可见性。但是,我无法了解这些特性的属性。我尝试了下面的代码,但没有通过循环。我该怎么办 <html> <head> <meta charset=utf-8 /> <title>getMap</title> <meta name='viewport' content='initial-scale=1,maximum-scale=1,user-scalable=no' />

我正在使用多边形形状文件,我想打开和关闭地图上某些要素的可见性。但是,我无法了解这些特性的属性。我尝试了下面的代码,但没有通过循环。我该怎么办

<html>
<head>
<meta charset=utf-8 />
<title>getMap</title>
<meta name='viewport' content='initial-scale=1,maximum-scale=1,user-scalable=no' />
<script src='https://api.mapbox.com/mapbox.js/v2.2.2/mapbox.js'></script>
<link href='https://api.mapbox.com/mapbox.js/v2.2.2/mapbox.css' rel='stylesheet' />
<style>
body { margin:0; padding:0; }
#map { position:absolute; top:0; bottom:0; width:100%; }
</style>
</head>
<body>
<link href='https://api.mapbox.com/mapbox.js/plugins/leaflet-fullscreen/v0.0.4/leaflet.fullscreen.css' rel='stylesheet' />
<script src='https://api.mapbox.com/mapbox.js/plugins/leaflet-fullscreen/v0.0.4/Leaflet.fullscreen.min.js'></script>
<script src='https://api.mapbox.com/mapbox.js/plugins/leaflet-hash/v0.2.1/leaflet-hash.js'></script>
<div id='map'>
</div>
<script>
L.mapbox.accessToken = 'pk.eyJ1IjoibWVsYm91cm5ldW5pIiwiYSI6ImNpZzc4YmNvdTBrdzR0dWx6cW1tZG00d2UifQ.T4kFqEpykbTCepilYVeL_g';
var map = L.mapbox.map('map', 'mapbox.streets', {attributionControl: true});
var featureLayer =     L.mapbox.featureLayer('melbourneuni.hetjfw29').addTo(map);
featureLayer.on('ready', function(){
// navigate the GeoJSON to get to the coordinates
var geojson = this.getGeoJSON();});
</script>
</body>
</html>

获取地图
正文{margin:0;padding:0;}
#映射{位置:绝对;顶部:0;底部:0;宽度:100%;}
L.mapbox.accessToken='pk.eyJ1IjoibWVsYm91cm5ldW5pIiwiYSI6ImNpZzc4YmNvdTBrdzR0dWx6cW1tZG00d2UifQ.T4kFqEpykbTCepilYVeL';
var map=L.mapbox.map('map','mapbox.streets',{attributecontrol:true});
var featureLayer=L.mapbox.featureLayer('melbourneuni.hetjfw29').addTo(map);
featureLayer.on('ready',function(){
//导航GeoJSON以获取坐标
var geojson=this.getGeoJSON();});

只需使用
var geojson=this.getGeoJSON()
,如

featureLayer.on('ready', function(e){
    var geojson = this.getGeoJSON()
    // which returns featureCollection, iterate through `geojson.features` array next as you need
});

只需使用
var geojson=this.getGeoJSON()
,如

featureLayer.on('ready', function(e){
    var geojson = this.getGeoJSON()
    // which returns featureCollection, iterate through `geojson.features` array next as you need
});

你说它不通过循环是什么意思?我的意思是你需要使用一个普通的JS for循环,或者下划线的389;。在我的示例中,eachI没有为循环编写代码,因为你原始代码示例中的问题是你试图从错误的位置(
e.target
)获取GeoJSON,而不是循环配置不正确你能告诉我触发错误的代码吗?或者更好的是JSFIDLE?我收到一条
{“消息”:“Tileset不存在”}
响应您的features.json调用。您在测试中看到了相同的情况吗?检查您的JS控制台。我已经添加了.geojson文件作为映射,但无法加载数据。你能再检查一遍代码吗?我错过了什么?你说它没有通过循环是什么意思?我的意思是你需要使用一个普通的JS for循环,或者下划线的389;。在我的示例中,eachI没有为循环编写代码,因为你原始代码示例中的问题是你试图从错误的位置(
e.target
)获取GeoJSON,而不是循环配置不正确你能告诉我触发错误的代码吗?或者更好的是JSFIDLE?我收到一条
{“消息”:“Tileset不存在”}
响应您的features.json调用。您在测试中看到了相同的情况吗?检查您的JS控制台。我已经添加了.geojson文件作为映射,但无法加载数据。你能再检查一遍代码吗?我错过了什么?