【发布时间】:2020-07-25 12:46:41
【问题描述】:
我正在尝试使用他们网站 https://docs.mapbox.com/mapbox-gl-js/example/geojson-markers/ 中的示例将自定义标记添加到 mapbox 地图,但每次我用我的(相同的照片格式)或任何链接替换他们的链接时,没关系,照片没有渲染,如果有人能够向我展示一个带有自定义照片/标记的工作示例。我需要它使用这个例子,我能够以另一种方式添加自定义标记,但我需要这种特定的方式与 .addSource 和 .addLayer
mapboxgl.accessToken = 'pk.eyJ1IjoibWFya2V0aW5nYnNvIiwiYSI6ImNrYnYwZmk3YjAxZjgyem1wY2Zmc3F4Y2EifQ.gMF-eCCaAHHgWIUoRcnfkg';
var map = new mapboxgl.Map({
container: 'map',
style: 'mapbox://styles/mapbox/light-v10',
center: [-96, 37.8],
zoom: 3
});
map.on('load', function() {
// Add an image to use as a custom marker
map.loadImage(
'https://docs.mapbox.com/mapbox-gl-js/assets/custom_marker.png', //here is the problem if i try to replace the image
function(error, image) {
if (error) throw error;
map.addImage('custom-marker', image);
// Add a GeoJSON source with 2 points
map.addSource('points', {
'type': 'geojson',
'data': {
'type': 'FeatureCollection',
'features': [
{
// feature for Mapbox DC
'type': 'Feature',
'geometry': {
'type': 'Point',
'coordinates': [
-77.03238901390978,
38.913188059745586
]
},
'properties': {
'title': 'Mapbox DC'
}
},
{
// feature for Mapbox SF
'type': 'Feature',
'geometry': {
'type': 'Point',
'coordinates': [-122.414, 37.776]
},
'properties': {
'title': 'Mapbox SF'
}
}
]
}
});
// Add a symbol layer
map.addLayer({
'id': 'points',
'type': 'symbol',
'source': 'points',
'layout': {
'icon-image': 'custom-marker',
// get the title name from the source's "title" property
'text-field': ['get', 'title'],
'text-font': [
'Open Sans Semibold',
'Arial Unicode MS Bold'
],
'text-offset': [0, 1.25],
'text-anchor': 'top'
}
});
}
);
});
【问题讨论】:
-
您发布的代码与 Mapbox 示例中的代码相同,并且它可以工作,如果您发布您正在编写的代码和我们可以尝试尝试的 Minimal Reproducible Example 会更好解决您的问题
标签: mapbox mapbox-gl-js