简体   繁体   English

Mapbox:通过在地图外部单击来打开标记的弹出窗口

[英]Mapbox: Open a marker's popup by clicking outside the map

I have some divs in my code "connected" to my maps markers. 我的代码中有些div已“连接”到地图标记。 My divs have unique ID's and my markers have the same ID set as attributes (title and marker-id). 我的div具有唯一的ID,而我的标记具有与属性相同的ID集(标题和标记ID)。 Is there any way to open the markers popup when clicking on the div? 单击div时,有什么方法可以打开标记弹出窗口吗? Here's my code: 这是我的代码:

$('#alldealers .dealer').each(function(){
    t = $(this).find('h2').text();
    lat = $(this).find('.lat');
    lng = $(this).find('.lng');  
    userLng = parseFloat($(lng).text());
    userLat = parseFloat($(lat).text());   
    subUniqueNum = $(this).attr('data-link');

    L.mapbox.featureLayer({
        type: 'Feature',
        geometry: {
            type: 'Point',
            coordinates: [userLng, userLat]
        },
        properties: {
            title: subUniqueNum,
            'marker-id': subUniqueNum,
            'marker-size': 'small',
            'marker-color': '#f85649',
        }
    }).bindPopup('<button class="giveMeDetails" data-attr-scroll="'+subUniqueNum+'">'+t+'</button>').addTo(map);   
});

$('.mapicon_wrapper').click(function(e){    
    tot_p = $(this).parent().parent().parent().attr('id');//parent wrapper id:same as marker's ID
    $root.animate({
        scrollTop: $('html').offset().top
    }, 500, function () {

        // This is where I need to open the markers popup with the same title as $this parent ID (tot_p)

    });        
});

This method of assigning an ID to a marker and calling it in the DOM won't be reliable: if the marker is outside of the currently-viewed map area, it won't be present in the DOM. 这种为标记分配ID并在DOM中调用该ID的方法将不可靠:如果标记位于当前查看的地图区域之外,则它将不会出现在DOM中。 A correct method would be 正确的方法是

var markerLayer = L.mapbox.featureLayer().addTo(map);
var markers = [];
$('#alldealers .dealer').each(function(){
    t = $(this).find('h2').text();
    lat = $(this).find('.lat');
    lng = $(this).find('.lng');  
    userLng = parseFloat($(lng).text());
    userLat = parseFloat($(lat).text());   
    subUniqueNum = $(this).attr('data-link');

    markers.push({
        type: 'Feature',
        geometry: {
            type: 'Point',
            coordinates: [userLng, userLat]
        },
        properties: {
            title: subUniqueNum,
            id: subUniqueNum,
            t: t,
            'marker-size': 'small',
            'marker-color': '#f85649',
        }
    })
});

markerLayer.setGeoJSON(markers);

markerLayer.eachLayer(function (layer) {
  layer.bindPopup('<button class="giveMeDetails" data-attr-scroll="'+layer.feature.properties.title+'">'+layer.feature.properties.t+'</button>');   
});

$('.mapicon_wrapper').click(function(e){    
    var tot_p = $(this).parent().parent().parent().attr('id');//parent wrapper id:same as marker's ID
    $root.animate({
        scrollTop: $('html').offset().top
    }, 500, function () {

        // This is where I need to open the markers popup with the same title as $this parent ID (tot_p)
    markers.eachLayer(function (layer) {
      if (layer.feature.properties.id === tot_p) {
        layer.openPopup();
      }
    });
    });        
});

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

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