javascript - 使用 Mapbox 从 map 获取要素

标签 javascript leaflet mapbox geojson

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

<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>

最佳答案

只需使用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
});

关于javascript - 使用 Mapbox 从 map 获取要素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33448932/

相关文章:

javascript - 如何在 Ionic 2 中使用条形码扫描仪重定向到其他 View

javascript - jQuery 文件上传插件成功回调

javascript - Angular2 导入自定义 JavaScript 库

javascript - 传单——缩放时无法更改圆标记上的半径

ios - 如何更改 Mapbox 中目的地以外位置的标记?

javascript - 如何在单击时更改 var map = L.mapbox.map (mapbox.js)

javascript - 此 html 代码的 DRY 一致性

javascript - React 中的 CSS 伪元素

javascript - Leaflet 中的 TopoJSON,来自 Omnivore : reading properties

php - 将 Leaflet-js ID、Lat 和 Lng 存储到 Mysql 数据库