最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Mapbox markers not respecting zindex - Stack Overflow

programmeradmin3浏览0评论

In a mapbox application that I am building, I am loading multiple markers onto the page depending on the users geolocation, the problem is though that I need certain markers to be above others, but the markers are not respecting the zIndex that I set. Here is my code.

function addMarker(type, radius, height, lat, long, id, special, name) {
    var circle_options;
    switch (type) {
        case marker.RESTRICTED:
            circle_options = {
                color: '#D90000',
                fillColor: '#D90000',  // Fill color
                fillOpacity: 0.4,    // Fill opacity
                stroke: false,
                zIndexOffSet: -1

            };
            break;

        case marker.NOT_RECOMMENDED:
            circle_options = {
                color: '#FF8000',
                fillColor: '#FF8000',  // Fill color
                fillOpacity: 0.75,    // Fill opacity
                stroke: false,
                zIndexOffSet: 1000

            };
            break;
        case marker.USER_RECOMMENDED:
            circle_options = {
                color: '#9FCC1C',
                fillColor: '#9FCC1C',  // Fill color
                fillOpacity: 0.75,    // Fill opacity
                stroke: false,
                zIndexOffSet: 1000

            };
            break;
        case marker.APPROVED:
            circle_options = {
                color: '#006ADC',
                fillColor: '#006ADC',  // Fill color
                fillOpacity: 0.75,
                stroke: false,
                zIndexOffSet: 1000
            };
            break;
    }
    var mark = L.circle([
   lat, long
    ], radius * 1609.34, circle_options);

    if (nav.currentPage() == 'location-details') {
        if (mapElement) {
            mapElement.setView([lat, long], zoomMap[radius])
            mapElement.removeLayer(use);
            mapElement.removeLayer(ic);

        }
    }
    else if (nav.currentPage() == 'home') {
        mark.objectId = id;
        circles.push(mark);
    }
    mark.on('click', function (e) {
        var self = id;
        circles.some(function (item) {
            if (self == item.objectId) {
                nav.navigate('location-details', false, item.objectId);
                return true;
            }
        });
    })
    mark.addTo(mapElement)
}

In a mapbox application that I am building, I am loading multiple markers onto the page depending on the users geolocation, the problem is though that I need certain markers to be above others, but the markers are not respecting the zIndex that I set. Here is my code.

function addMarker(type, radius, height, lat, long, id, special, name) {
    var circle_options;
    switch (type) {
        case marker.RESTRICTED:
            circle_options = {
                color: '#D90000',
                fillColor: '#D90000',  // Fill color
                fillOpacity: 0.4,    // Fill opacity
                stroke: false,
                zIndexOffSet: -1

            };
            break;

        case marker.NOT_RECOMMENDED:
            circle_options = {
                color: '#FF8000',
                fillColor: '#FF8000',  // Fill color
                fillOpacity: 0.75,    // Fill opacity
                stroke: false,
                zIndexOffSet: 1000

            };
            break;
        case marker.USER_RECOMMENDED:
            circle_options = {
                color: '#9FCC1C',
                fillColor: '#9FCC1C',  // Fill color
                fillOpacity: 0.75,    // Fill opacity
                stroke: false,
                zIndexOffSet: 1000

            };
            break;
        case marker.APPROVED:
            circle_options = {
                color: '#006ADC',
                fillColor: '#006ADC',  // Fill color
                fillOpacity: 0.75,
                stroke: false,
                zIndexOffSet: 1000
            };
            break;
    }
    var mark = L.circle([
   lat, long
    ], radius * 1609.34, circle_options);

    if (nav.currentPage() == 'location-details') {
        if (mapElement) {
            mapElement.setView([lat, long], zoomMap[radius])
            mapElement.removeLayer(use);
            mapElement.removeLayer(ic);

        }
    }
    else if (nav.currentPage() == 'home') {
        mark.objectId = id;
        circles.push(mark);
    }
    mark.on('click', function (e) {
        var self = id;
        circles.some(function (item) {
            if (self == item.objectId) {
                nav.navigate('location-details', false, item.objectId);
                return true;
            }
        });
    })
    mark.addTo(mapElement)
}
Share Improve this question edited Apr 23, 2015 at 2:37 Hossein Narimani Rad 32.6k19 gold badges91 silver badges121 bronze badges asked Aug 14, 2014 at 2:06 Elias NicholsElias Nichols 1831 silver badge7 bronze badges 4
  • why not just add the markers in the order you want them to display? There is no functionality for z-index in Mapbox – Vincent McNabb Commented Jan 25, 2015 at 1:16
  • 1 L.Circle extends L.Path so you could possibly use the bringToFront() and bringToBack() methods. But it does not allow you to precisely control the z-index, see this feature request. And Vincent is right about the order : see With JavaScript, can I change the Z index/layer of an SVG <g> element? – slaur4 Commented Feb 20, 2015 at 12:28
  • 3 Can you make a jsfiddle for it? – BDD Commented Apr 25, 2015 at 16:41
  • 1 Are you using positioned elements? developer.mozilla/en-US/docs/Web/CSS/z-index – user2182349 Commented Apr 29, 2015 at 11:40
Add a ment  | 

2 Answers 2

Reset to default 3

Looks like there's no zIndexOffSet option among Path options you can pass as a last param to L.Circle().

Though there is a className option which you could use to assign custom classes with different pre-set z-indexes to your markers.

If you're working with Layers, you can use this feature to control the order of rendering of your markers:

https://docs.mapbox./mapbox-gl-js/style-spec/layers/#layout-symbol-symbol-sort-key

发布评论

评论列表(0)

  1. 暂无评论