How to get focus to map marker when click on a link outside the map?

白昼怎懂夜的黑 提交于 2019-12-25 08:48:30

问题


So, I have a map with the mapquest leaflet which showing few markers on it and have some popup messages. However, everything working well but underneath of the map I have one table where I am showing hotel number. so like this with link:

<a href='#Hotel22'>Hotel 22</a><a href='#Hotel23'>Hotel 23</a><a href='#Hotel24'>Hotel 24</a>

So, when any user click on #Hotel22 then it will direct take focus to map's particulare marker and open up the marker window. So that user will know that the Hotel 22 is here on map...

If any one know this so My map is created in leaflet but with mapquest leaflet api way. As because of project I cann't copy/paste some of the complex code here....

Thank you in advanced my friends.:)


回答1:


You basically have to maintain an associative array of your markers.

<a href="#" onclick="focusOn('paris')">Paris</a>

// javascript
var markers = {};
markers["paris"] = L.marker([48.85749, 2.35107]).addTo(mymap)
.bindPopup("<b>Hello world!</b><br />I am Paris.");

function focusOn(city) {
   markers[city].openPopup();
}

See example




回答2:


You can refer to the markers by their layerId. Make a reference to it in the list (and on the marker if you want to list to highlight when rolling over the marker).

                        marker = L.marker([c.shapePoints[0], c.shapePoints[1]]);
                        srs.addLayer(marker);
                        layerid = srs.getLayerId(marker);
                        marker.on('mouseover', function(a){
                            over(srs.getLayerId(a.target));
                        })
                        .on('mouseout', function(a){
                            out(srs.getLayerId(a.target));
                        })
                        .bindPopup(c.name);
                        tabletext = tabletext + '<tr id="row' + layerid + '" ' +
                            'onmouseover="over(' + layerid + ');" ' +
                            'onmouseout="out(' + layerid + ');">' +
                            '<td>' + c.name + '</td>' +
                            '</tr>';

Then in the over and out functions you can control the marker and list.

        function over(id) {
            srs.getLayer(id).setIcon(newicon);
            $('#row' + id).css('backgroundImage', highlight);
        }

See it in action here.



来源:https://stackoverflow.com/questions/37670313/how-to-get-focus-to-map-marker-when-click-on-a-link-outside-the-map

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!