【问题标题】:Cusotm markers in mapbox gl js does not work?mapbox gl js 中的自定义标记不起作用?
【发布时间】: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


【解决方案1】:

基于documentation of map.loadImage,如果您从外部域加载该图像,则该域必须支持CORS(跨域资源共享)。

由于您没有包括您要加载的图像是什么,我无法验证它,但它似乎与此有关。

已编辑:如果您需要启用 CORS 的服务器来上传图片,您可以尝试使用任何可用的图片上传服务器,例如 https://postimg.cc/。但除了简单的 PoC,我不会推荐这种方法。

我已经用这个image尝试了你的代码

我创建了一个fiddle with it on how to add a custom image for a marker... 并且它可以工作,所以说代码是正确的,但您遇到的问题是因为您尝试使用的图像未托管在 CORS 启用域

如果此解决方案解决了您的问题,请将其标记为“已接受答案”,这样也有助于其他用户知道它是正确的解决方案。

【讨论】:

  • 它不适用于任何图像,我尝试了多个图像,甚至来自谷歌图像
  • 这就是重点,不是任何域都接受 CORS,这是您需要隐式配置的东西。是否有任何理由将 url 指向另一个域?您不能将图像下载到您的域吗?我在带有自定义标记的 mapbox 项目中有自己的代码,因为图像托管在 CORS 域或页面正在运行的同一域中。
  • 我需要它来自一个 url,你知道我可以在哪里托管它以便它接受 CORS 吗?如果我想从本地链接它,我只需要输入照片吗?喜欢 custom-marker.png?
  • 用解决方案编辑了我的答案,包括在启用了 CORS 的域中托管图像的位置以及演示。如果它解决了您的问题,请将答案标记为“已接受答案”。
  • 非常感谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-12-10
  • 1970-01-01
  • 2018-12-18
  • 1970-01-01
  • 2020-02-09
  • 2019-10-29
相关资源
最近更新 更多