mapbox gl js 中的自定义标记不起作用?
Cusotm markers in mapbox gl js does not work?
我正在尝试使用他们网站 https://docs.mapbox.com/mapbox-gl-js/example/geojson-markers/ 中的示例将自定义标记添加到 mapbox 地图,但每次我将他们的 link 替换为我的(相同的照片格式)或任何 link,没关系,照片不会渲染,如果有人能够向我展示一个带有自定义 photo/marker 的工作示例。我需要它才能使用此示例,我能够以另一种方式添加自定义标记,但我需要使用 .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'
}
});
}
);
});
基于documentation of map.loadImage
, if you are loading that image from an External domain, that domain must support CORS(跨源资源共享)。
由于您没有包括您要加载的图像,我无法验证它,但似乎与此有关。
已编辑: 如果您需要启用 CORS 的服务器来上传图片,您可以尝试使用任何可用的图片上传服务器,例如 https://postimg.cc/。但除了简单的 PoC,我不推荐这种方法。
我用这个 image
试过你的代码
并且我创建了一个 fiddle with it on how to add a custom image for a marker... 并且它可以工作,正如所说的代码是正确的,但您遇到的问题是因为您尝试使用的图像未托管在 CORS 启用域.
如果此解决方案解决了您的问题,请将其标记为 'answer accepted',这样也将帮助其他用户知道这是正确的解决方案。
我正在尝试使用他们网站 https://docs.mapbox.com/mapbox-gl-js/example/geojson-markers/ 中的示例将自定义标记添加到 mapbox 地图,但每次我将他们的 link 替换为我的(相同的照片格式)或任何 link,没关系,照片不会渲染,如果有人能够向我展示一个带有自定义 photo/marker 的工作示例。我需要它才能使用此示例,我能够以另一种方式添加自定义标记,但我需要使用 .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'
}
});
}
);
});
基于documentation of map.loadImage
, if you are loading that image from an External domain, that domain must support CORS(跨源资源共享)。
由于您没有包括您要加载的图像,我无法验证它,但似乎与此有关。
已编辑: 如果您需要启用 CORS 的服务器来上传图片,您可以尝试使用任何可用的图片上传服务器,例如 https://postimg.cc/。但除了简单的 PoC,我不推荐这种方法。
我用这个 image
试过你的代码并且我创建了一个 fiddle with it on how to add a custom image for a marker... 并且它可以工作,正如所说的代码是正确的,但您遇到的问题是因为您尝试使用的图像未托管在 CORS 启用域.
如果此解决方案解决了您的问题,请将其标记为 'answer accepted',这样也将帮助其他用户知道这是正确的解决方案。