【问题标题】:Get feature from map using Mapbox使用 Mapbox 从地图中获取特征
【发布时间】:2015-10-31 06:55:18
【问题描述】:

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

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

【问题讨论】:

    标签: javascript leaflet mapbox geojson


    【解决方案1】:

    只需使用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 循环,或者下划线的 _.each
    • 我没有为我的示例中的循环编写代码,因为您原始代码示例中的问题是您试图从不正确的位置(e.target)获取 GeoJSON,而不是那样循环配置不正确。 ——
    • 你能告诉我触发错误的代码吗?还是更好的 jsfiddle?
    • 我收到了{"message":"Tileset does not exist"} 回复您的 features.json 电话。你在测试中看到同样的东西吗?检查你的 JS 控制台。
    • 我已将 .geojson 文件添加为地图,但无法加载数据。你能再检查一下代码吗?我错过了什么?
    猜你喜欢
    • 2019-04-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-18
    • 1970-01-01
    • 1970-01-01
    • 2018-06-08
    • 1970-01-01
    相关资源
    最近更新 更多