繁体   English   中英

Mapbox 更改某些位置的图标

[英]Mapbox Changing the Icon of certain locations

我对 Mapbox GL JS 比较陌生,我正在做一个项目,我发现要更改地图上某些位置的颜色和图标。 我去了 Mapbox studio 并获得了这些位置的特定 ID。 但是,我不知道如何在我的 JS 代码中匹配它们并更改它们的颜色和图标。

我还试图显示一个弹出窗口,当用户单击该位置的图标时,该窗口显示有关这些位置的信息。 也很难匹配 id 并打开弹出窗口。

这是我当前的 JS 代码:

const police_station = {
    "type": "FeatureCollection",
    "features": [{
        "type": "Feature",
        "properties": {
            "icon": "police-15",
            "color": "#004cff",
            "title": "City of Seattle Police Department",
            "description": "1519 12th Ave, Seattle, WA 98122"
        },
        "geometry": {
            "type": "Point",
            "coordinates": [-122.31725715100764,
                47.614924945431525
            ]
        }
    }]
}
// User can click on icon to comment and view information
for (const feature of police_station.features) {
    // create a HTML element for each feature
    const el = document.createElement('div');
    el.className = 'police';
    // make a marker for each feature and add to the map
    new mapboxgl.Marker(el).setLngLat(feature.geometry.coordinates).setPopup(new mapboxgl.Popup({
        offset: 25
    }).setHTML(`
                                      <h2>${feature.properties.title}</h2>
                                      <p>${feature.properties.description}</p>
                                      <button class="open-button" onclick="openForm()">Comment</button>`)).addTo(map);

CSS:

  .police {
    background-image: url('../assets/police.svg');
    background-size: cover;
    width: 25px;
    height: 25px;
    border-radius: 50%;
    cursor: pointer;
  }

地图截图

假设我想更改唯一 ID 为 12345 的餐厅 Plum Bistro 的图标和图标颜色,并有一个显示其信息的弹出窗口,我该怎么做?

您可以将 innterHTML 添加到您的 el (标记)。 检查这个:

// User can click on icon to comment and view information
for (const feature of police_station.features) {
    // create a HTML element for each feature
    const el = document.createElement('div');
    el.className = 'police';
    el.innerHTML = '<img src="YOUR URL TO A IMG ex: /assets/marker-on-map.svg">';
    // make a marker for each feature and add to the map

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM