gpt4 book ai didi

javascript - 创建 Mapbox map 后,如何更新 Mapbox map 上的 GeoJSON?

转载 作者:行者123 更新时间:2023-12-03 10:46:49 25 4
gpt4 key购买 nike

我有这个html:

<div id="clickthis"><p>addGeoJSON</p></div>
<div id='raw'></div>
<div id='map'></div>

这是我的 JavaScript 代码:

var geojson = [
{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [-77.03238901390978,38.913188059745586]
},
"properties": {
"title": "Mapbox DC",
"description": "1714 14th St NW, Washington DC",
"marker-color": "#fc4353",
"marker-size": "large",
"marker-symbol": "monument"
}
},
{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [-122.414, 37.776]
},
"properties": {
"title": "Mapbox SF",
"description": "155 9th St, San Francisco",
"marker-color": "#fc4353",
"marker-size": "large",
"marker-symbol": "harbor"
}
}
];
L.mapbox.accessToken = 'pk.eyJ1IjoibWFwcGllc3RpY2tlciIsImEiOiJIn0.IgCoorxIUvE0CjcPriROeQ';
var map = L.mapbox.map('map', 'mappiesticker.kh78ogof').setView([34, -118.50], 9);

$('#clickthis').click(function(){
alert('alkr');
dataLayer = L.mapbox.featureLayer(geojson);
dataLayer.addTo(map);
})

当我单击该 div 时,警报会触发,但没有其他 react 。我尝试使用 .setGeoJSON 函数,但它返回未定义。我的结论是,这仅在创建 map 时使用,而不是在创建后添加 GeoJSON

最佳答案

我在使用您的 token 时收到 401 错误:

Failed to load resource: the server responded with a status of 401 (Unauthorized)

当我将您的代码与 Mapbox 的示例 token 和 map 一起使用时(注意,您应该使用您自己的 token 和 map ),它完全没问题:

L.mapbox.accessToken = 'pk.eyJ1IjoicGF1bC12ZXJob2V2ZW4iLCJhIjoiZ1BtMWhPSSJ9.wQGnLyl1DiuIQuS0U_PtiQ';

var map = L.mapbox.map('mapbox', 'examples.map-i86nkdio', {
'center': [0, 0],
'zoom': 1
});

Plunker 上的工作示例:http://plnkr.co/edit/wrlC7AAHLBWsDgzk8PRw?p=preview

我会仔细检查您的帐户 token 和 map ID。

关于javascript - 创建 Mapbox map 后,如何更新 Mapbox map 上的 GeoJSON?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28549923/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com