问题描述
这是我在玩MapBox的过程中遇到的一些问题,它让GeoLocation API能够将数据发送回地图并更新它。我现在得到的:
mapboxgl.accessToken ='TOKEN-HERE';
var map = new mapboxgl.Map({
container:'map',// container id
style:'mapbox:// styles / mapbox / streets-v9',
中心:[-0.968539,54.562917],
zoom:9
});
$ b map.on('style.load',function(){
map.addSource(myMap,{
type:geojson,
data:https://api.mapbox.com/geocoding/v5/mapbox.places/UK_ADDRESS_HERE.json?country=gb&types=address&autocomplete=true&access_token=TOEKN
}) ;
map.addLayer({
'id':'test1',
'type':'line',
'source':'myMap',
'interactive':true
});
});
答案可能在于您如何编码 UK_ADDRESS_HERE
。
要求由于{query}参数可以包含任何值,因此应该使用URL编码。
这意味着像 10 Downing Street,Westminster
这样的简单地理编码请求必须使用至 10%20Downing%20Street% 2C%20Westminster
。
尝试此操作并验证您的请求是否正确。
curl https://api.tiles.mapbox.com/geocoding/v5/mapbox.places/10%20Downing%20Street%2C%20London.json?access_token=${MAPBOX_ACCESS_TOKEN}
I have being playing around with MapBox and I am having some issues with getting the GeoLocation API to send data back to the map and update the it.
This is what I got right now:
mapboxgl.accessToken = 'TOKEN-HERE';
var map = new mapboxgl.Map({
container: 'map', // container id
style: 'mapbox://styles/mapbox/streets-v9',
center: [-0.968539, 54.562917],
zoom: 9
});
map.on('style.load', function() {
map.addSource("myMap", {
"type": "geojson",
"data": "https://api.mapbox.com/geocoding/v5/mapbox.places/UK_ADDRESS_HERE.json?country=gb&types=address&autocomplete=true&access_token=TOEKN"
});
map.addLayer({
'id': 'test1',
'type': 'line',
'source': 'myMap',
'interactive': true
});
});
The answer may lie with how you are encoding UK_ADDRESS_HERE
.
The request format for the Mapbox Geocding API requires that since the {query} parameter can contain any value, it should be URL-encoded.
That means that a simple geocode request like 10 Downing Street, Westminster
must be encoded using encodeURIComponent to 10%20Downing%20Street%2C%20Westminster
.
Try this and verify that your request is proper.
curl https://api.tiles.mapbox.com/geocoding/v5/mapbox.places/10%20Downing%20Street%2C%20London.json?access_token=${MAPBOX_ACCESS_TOKEN}
这篇关于Mapbox地理位置,如何加载到地图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!