简体   繁体   English

hover 上的地图框标记的弹出窗口不起作用

[英]popups for mapbox markers on hover not working

I'm trying to show popups on hover for Mapbox markers.我正在尝试在 hover 上显示 Mapbox 标记的弹出窗口。

I have tried following the examples and I got it to work to do what I want showing different marker icons based on a class but it's the popups that don't work.我已经尝试按照示例进行操作,并且可以根据 class 显示不同的标记图标,但弹出窗口不起作用。

I need to show the popups on the mouse hover of the markers.我需要在标记的鼠标 hover 上显示弹出窗口。

I think it has something to do with the data source?我认为这与数据源有关? Any help would be appreciated.任何帮助,将不胜感激。 Thanks谢谢

My code is as below,我的代码如下,

 mapboxgl.accessToken = 'example-token-here'; var geojson = { 'type': 'FeatureCollection', 'features': [{ 'type': 'Feature', 'properties': { 'message': 'Foo', 'class': 'tree', 'description': '<strong>Make it Mount Pleasant</strong><p>Make it Mount Pleasant is a handmade and vintage market and afternoon of live entertainment and kids activities. 12:00-6:00 pm</p>', 'iconSize': [60, 60] }, 'geometry': { 'type': 'Point', 'coordinates': [-66.324462890625, -16.024695711685304] } }, { 'type': 'Feature', 'properties': { 'message': 'Bar', 'description': '<strong>Make it Mount Pleasant</strong><p>Make it Mount Pleasant is a handmade and vintage market and afternoon of live entertainment and kids activities. 12:00-6:00 pm</p>', 'class': 'xmas', 'iconSize': [50, 50] }, 'geometry': { 'type': 'Point', 'coordinates': [-61.2158203125, -15.97189158092897] } }, { 'type': 'Feature', 'properties': { 'message': 'Baz', 'description': '<strong>Make it Mount Pleasant</strong><p>Make it Mount Pleasant is a handmade and vintage market and afternoon of live entertainment and kids activities. 12:00-6:00 pm</p>', 'class': 'meadow', 'iconSize': [40, 40] }, 'geometry': { 'type': 'Point', 'coordinates': [-63.29223632812499, -18.28151823530889] } } ] }; var map = new mapboxgl.Map({ container: 'map', style: 'mapbox://styles/mapbox/streets-v11', center: [-65.017, -16.457], zoom: 5 }); // add markers to map geojson.features.forEach(function(marker) { // Get each feature class var cls = marker.properties['class']; if (cls == 'tree') { // create a DOM element for the tree marker var el = document.createElement('div'); el.className = 'marker'; el.style.backgroundImage = 'url(https://1mt.brightapps.co/wp-content/uploads/2021/02/tree.png)'; el.style.width = marker.properties.iconSize[0] + 'px'; el.style.height = marker.properties.iconSize[1] + 'px'; } if (cls == 'xmas') { // create a DOM element for the tree marker var el = document.createElement('div'); el.className = 'marker xmas-marker'; el.style.backgroundImage = 'url(https://1mt.brightapps.co/wp-content/uploads/2021/02/xmas.png)'; el.style.width = marker.properties.iconSize[0] + 'px'; el.style.height = marker.properties.iconSize[1] + 'px'; } // add marker to map new mapboxgl.Marker(el).setLngLat(marker.geometry.coordinates).addTo(map); }); // Create a popup, but don't add it to the map yet. var popup = new mapboxgl.Popup({ closeButton: false, closeOnClick: false }); map.on('mouseenter', geojson, function(e) { // Change the cursor style as a UI indicator. map.getCanvas().style.cursor = 'pointer'; var coordinates = e.features[0].geometry.coordinates.slice(); var description = e.features[0].properties.description; // Ensure that if the map is zoomed out such that multiple // copies of the feature are visible, the popup appears // over the copy being pointed to. while (Math.abs(e.lngLat.lng - coordinates[0]) > 180) { coordinates[0] += e.lngLat.lng > coordinates[0]? 360: -360; } // Populate the popup and set its coordinates // based on the feature found. popup.setLngLat(coordinates).setHTML(description).addTo(map); }); map.on('mouseleave', geojson, function() { map.getCanvas().style.cursor = ''; popup.remove(); });
 .marker { display: block; border: none; border-radius: 50%; cursor: pointer; padding: 0; background-repeat: no-repeat; background-size: cover; }.mapboxgl-popup { max-width: 100px; }.as-console-wrapper { max-height: 15%;important: bottom; 0; }
 <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous"> <script src='https://api.mapbox.com/mapbox-gl-js/v2.1.1/mapbox-gl.js'></script> <link href='https://api.mapbox.com/mapbox-gl-js/v2.1.1/mapbox-gl.css' rel='stylesheet' /> <div class="container-fluid"> <div class="row"> <div class="col-lg-8"> <div id="map" style="height: 500px;"></div> </div> <div class="col-lg-4 bg-secondary"> </div> </div> </div>

There are several concept misunderstandings in your code.您的代码中有几个概念误解。 You're creating custom image markers (which are in the end HTML elements) based on a source, but you're creating the handler at map level over the source, instead attached to the markers.您正在基于源创建自定义图像标记(最终是 HTML 元素),但是您在源上的 map 级别创建处理程序,而不是附加到标记。 Second, you create one single popup before it's even instantiated, and then try to position on the event, but also you remove it in the detach event, so even if that would ever run, it'll run only once.其次,您甚至在实例化之前创建一个弹出窗口,然后尝试在事件上 position ,但您也在分离事件中将其删除,因此即使它会运行,它也只会运行一次。

If what you want is to show a pop-up on mouseover when the cursor is over one of your trees... I have created this fiddle for you showing the popup on mouseover .如果您想要的是在 cursor 位于您的一棵树上时在鼠标悬停时显示弹出窗口......我已经为您创建了这个小提琴,在 mouseover 上显示弹出窗口

图片

Relevant code is below...相关代码如下...

var geojson = {
    'type': 'FeatureCollection',
    'features': [{
        'type': 'Feature',
        'properties': {
            'message': 'Foo',
            'class': 'tree',
            'description': '<strong>Make it Mount Pleasant</strong><p>Make it Mount Pleasant is a handmade and vintage market and afternoon of live entertainment and kids activities. 12:00-6:00 p.m.</p>',
            'iconSize': [60, 60]
        },
        'geometry': {
            'type': 'Point',
            'coordinates': [-66.324462890625, -16.024695711685304]
        }
    },
    {
        'type': 'Feature',
        'properties': {
            'message': 'Bar',
            'description': '<strong>Make it Mount Pleasant</strong><p>Make it Mount Pleasant is a handmade and vintage market and afternoon of live entertainment and kids activities. 12:00-6:00 p.m.</p>',
            'class': 'xmas',
            'iconSize': [50, 50]
        },
        'geometry': {
            'type': 'Point',
            'coordinates': [-61.2158203125, -15.97189158092897]
        }
    },
    {
        'type': 'Feature',
        'properties': {
            'message': 'Baz',
            'description': '<strong>Make it Mount Pleasant</strong><p>Make it Mount Pleasant is a handmade and vintage market and afternoon of live entertainment and kids activities. 12:00-6:00 p.m.</p>',
            'class': 'meadow',
            'iconSize': [40, 40]
        },
        'geometry': {
            'type': 'Point',
            'coordinates': [-63.29223632812499, -18.28151823530889]
        }
    }
    ]
};

var map = new mapboxgl.Map({
    container: 'map',
    style: 'mapbox://styles/mapbox/streets-v11',
    center: [-65.017, -16.457],
    zoom: 5
});

// Create a popup, but don't add it to the map yet.
var popup;

// add markers to map
geojson.features.forEach(function (marker) {
    // Get each feature class
    var cls = marker.properties['class'];

    if (cls == 'tree') {
        // create a DOM element for the tree marker

        var el = document.createElement('div');
        el.className = 'marker';
        el.style.backgroundImage =
            'url(https://1mt.brightapps.co/wp-content/uploads/2021/02/tree.png)';
        el.style.width = marker.properties.iconSize[0] + 'px';
        el.style.height = marker.properties.iconSize[1] + 'px';


    }

    if (cls == 'xmas') {
        // create a DOM element for the tree marker

        var el = document.createElement('div');
        el.className = 'marker xmas-marker';
        el.style.backgroundImage =
            'url(https://1mt.brightapps.co/wp-content/uploads/2021/02/xmas.png)';
        el.style.width = marker.properties.iconSize[0] + 'px';
        el.style.height = marker.properties.iconSize[1] + 'px';

    }

    el.addEventListener('mouseover', function () {
        // Change the cursor style as a UI indicator.
        map.getCanvas().style.cursor = 'pointer';
        console.log(marker);

        var coordinates = marker.geometry.coordinates.slice();
        var description = marker.properties.description;

        // Ensure that if the map is zoomed out such that multiple
        // copies of the feature are visible, the popup appears
        // over the copy being pointed to.
        //while (Math.abs(marker.lngLat.lng - coordinates[0]) > 180) {
        //  coordinates[0] += marker.lngLat.lng > coordinates[0] ? 360 : -360;
        //}
        console.log(coordinates);
        // Populate the popup and set its coordinates
        // based on the feature found.
        popup = new mapboxgl.Popup({
            closeButton: false,
            closeOnClick: false
        }).setLngLat(coordinates).setHTML(description).addTo(map);
    });

    el.addEventListener('mouseout', function () {
        map.getCanvas().style.cursor = '';
        popup.remove();
    });



    // add marker to map
    new mapboxgl.Marker(el)
        .setLngLat(marker.geometry.coordinates)
        .addTo(map);
});


  [1]: https://i.stack.imgur.com/deiVz.png

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

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