【问题标题】:Make marker pop-up使标记弹出
【发布时间】:2018-06-07 08:59:01
【问题描述】:

我在我的项目中使用 mapbox,需要在标记悬停时弹出窗口

我找到了这个例子

Example of hovers

但是有标记是从图层获取的

我有 JSON 格式的标记数据。这是我让它显示的方法

  export  module  HotelMapResults {
  const token = "***************";
   export function all_hotels_map_results(): void {
    Helpers.set_currency_settings();
    const json = gon.hotel_info;
    const centerLatlng = new mapboxgl.LngLat(gon.destination_city.lng, gon.destination_city.lat);
    mapboxgl.accessToken = token;
    let map = new mapboxgl.Map({
          container: "map-canvas-all",
          style: "mapbox://styles/mapbox/streets-v9",
          center: centerLatlng,
          zoom: 9
    });

    map.addControl(new mapboxgl.NavigationControl());
    map.on("load", function() {
    $.each(json, function(i, item) {
      let myLatlng = new mapboxgl.LngLat(item.lng, item.lat);
      let stars = "";
      for(let s = 0; s < item.rating; s++) {
        stars += '<img class="star-image" style="height:20px;width:20px;">';
        }
      const Popup_Content = '<div class="map-card__wrapper">'
      +'<div class="map-card__image-container">'
      +'<div class="map-card__image" style="background: url('+item.pictures[0].url+');">' +'</div>'
      +'</div>'
      +'<div class ="map-card__content-container ">'
      + '<div class ="map-card__title">'+item.name +'</div>'
      +'<p class="map-card__address">'+item.address1+'</p>'
      + '<div class ="map-card__review">'+stars +'</div>'
      + '<div class ="map-card__price-container">'+__("Flygbolag")+ ": "+ accounting.formatMoney(item.sales_prices[0])
      +'</div>'
      + '</div>';

      let marker = new mapboxgl.Marker()
        .setLngLat(myLatlng)
        .setPopup(new mapboxgl.Popup({ offset: 5 })
        .setHTML(Popup_Content))
        .addTo(map);
      });
    });

我尝试过像这样在 mouseenter 上显示警报

map.on("mouseenter","marker", function(){
  alert("Here");
})

这是我的json

 [
  {
    "name": "Ibis London Thurrock M25",
    "address1": "Weston Avenue",
    "rating": 2,
    "description": "Ibis London Thurrock hotel is an economy London hotel located 15 minutes' drive from Ebbsfleet International Station.",
    "lng": 0.268539,
    "lat": 51.477455,
    "pictures": [
      {
        "url": "https://aobtravel.s3.amazonaws.com/hotelpictures/006/866/211/LON-IBI9-1.jpg?1413518521",
        "description": "Exteriör"
      },
      {
        "url": "https://aobtravel.s3.amazonaws.com/hotelpictures/006/866/221/LON-IBI9-2.jpg?1413518521",
        "description": "Exteriör"
      },
      {
        "url": "https://aobtravel.s3.amazonaws.com/hotelpictures/006/866/231/LON-IBI9-3.jpg?1413518522",
        "description": "Exteriör"
      },
      {
        "url": "https://aobtravel.s3.amazonaws.com/hotelpictures/006/866/241/LON-IBI9-4.jpg?1413518522",
        "description": "Exteriör"
      },
      {
        "url": "https://aobtravel.s3.amazonaws.com/hotelpictures/006/866/253/LON-IBI9-5.jpg?1413518522",
        "description": "Gästrum"
      },
      {
        "url": "https://aobtravel.s3.amazonaws.com/hotelpictures/006/866/261/LON-IBI9-6.jpg?1413518523",
        "description": "Gästrum"
      },
      {
        "url": "https://aobtravel.s3.amazonaws.com/hotelpictures/006/866/269/LON-IBI9-7.jpg?1413518523",
        "description": "Restaurang"
      },
      {
        "url": "https://aobtravel.s3.amazonaws.com/hotelpictures/006/866/275/LON-IBI9-8.jpg?1413518523",
        "description": "Restaurang"
      },
      {
        "url": "https://aobtravel.s3.amazonaws.com/hotelpictures/006/866/283/LON-IBI9-9.jpg?1413518523",
        "description": "Lobby"
      },
      {
        "url": "https://aobtravel.s3.amazonaws.com/hotelpictures/006/866/291/LON-IBI9-10.jpg?1413518524",
        "description": "Lobby"
      },
      {
        "url": "https://aobtravel.s3.amazonaws.com/hotelpictures/007/050/709/LON-IBI9-11.jpg?1485170823",
        "description": "Annat"
      },
      {
        "url": "https://aobtravel.s3.amazonaws.com/hotelpictures/007/050/712/LON-IBI9-12.jpg?1485170823",
        "description": "Annat"
      },
      {
        "url": "https://aobtravel.s3.amazonaws.com/hotelpictures/007/050/715/LON-IBI9-13.jpg?1485170823",
        "description": "Annat"
      },
      {
        "url": "https://aobtravel.s3.amazonaws.com/hotelpictures/007/050/720/LON-IBI9-14.jpg?1485170823",
        "description": "Annat"
      }
    ],
    "sales_prices": [
      4788,
      4788,
      5964,
      5964
    ]
  }

]

但它不起作用。在我的情况下如何进行弹出式悬停?

【问题讨论】:

    标签: javascript jquery typescript mapbox mapbox-gl-js


    【解决方案1】:

    不是一个直接的解决方案,但您可能想查看https://github.com/mapbox/mapbox-gl-markers,它省去了很多代码,以便从标记的 GeoJSON 中自动提供带有弹出窗口的简单地图标记。

    【讨论】:

      【解决方案2】:

      您可以通过将事件绑定到标记的 HTML 元素来做到这一点。

      看起来像这样:

      var marker = document.createElement("img")
      marker.src = "/images/marker-icon.png"
      marker.height = 35
      
      marker.addEventListener("mouseenter", ()=>{
        // Add popup here
      })
      
      marker.addEventListener("mouseleave", ()=>{
        // Remove popup here
      })
      var mapBoxMarker = new mapboxgl.Marker(marker)
      

      【讨论】:

      • 但是我已经在地图上标记了。我不想创建标记 img 等。只需 mouseenter 和 mouseleave 事件
      • 您仍然可以使用您现在使用的标记。如果要将事件链接到它们,则需要引用它们正在使用的 HTML 元素。您将更改我的代码的前 3 行,使其具有您现在使用的相同标记。我认为没有其他方法可以解决这个问题。
      • 好的,我可以将它绑定到他们使用的 css 类?
      猜你喜欢
      • 2021-12-07
      • 2020-07-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-01
      • 1970-01-01
      相关资源
      最近更新 更多