链接到外部网站上的地图框标记?

时间:2014-10-12 16:07:08

标签: javascript html mapbox post-parameter

我有一个页面,其中包含一个包含大约7个标记的地图集地图。每个标记都包含一个缩略图,该缩略图也会添加到导航工具栏中,单击该缩略图会将您带到标记并打开它。

我想在外部网站上添加类似的链接。是否可以在外部网页上显示链接,将我带到我的地图框页面并打开特定标记?

//populate markers
oneArtPlease.on('layeradd', function(e) {
    var marker = e.layer,
    feature = marker.feature;

    // popupz
    var popupContent =  '<div class="thumbnail"><a target="_blank" class="popup" href="' + feature.properties.url + '">' +
        '<img src="' + feature.properties.image + '" width="300" title="Click for the full picture" /><br>' +
        feature.geometry.coordinates+'</br>'+
        feature.properties.picnote +
        '</a></div>';
    marker.bindPopup(popupContent,{
        closeButton: true,
        minWidth: 320
    });

    //change icon
    marker.setIcon(L.icon(feature.properties.icon));
    //populate thumbnail bar
    var link = info.insertBefore(document.createElement('a'),info.firstChild);
    link.className = 'item';
    link.href = '#';
    link.innerHTML ='<img src="' + feature.properties.image + '" class="navthumb"/>';
    link.onclick = function() {
        if (/active/.test(this.className)) {
            this.className = this.className.replace(/active/, '').replace(/\s\s*$/, '');
        } else {
            var siblings = info.getElementsByTagName('a');
            for (var i = 0; i < siblings.length; i++) {
                siblings[i].className = siblings[i].className
                .replace(/active/, '').replace(/\s\s*$/, '');
            };
            this.className += ' active';
            // move to marker and open on thumbnail click
            map.panTo(marker.getLatLng());
            marker.openPopup();
        }
        return false;
    };

});
new L.Control.Zoom({ position: 'topright' }).addTo(map);

1 个答案:

答案 0 :(得分:0)

我最终找到了一些用js解析URL参数的代码。这里的url参数是使用index.html?piece = X

传递的
function getURLParameter(name) {
  return decodeURIComponent((new RegExp('[?|&]' + name + '=' + '([^&;]+?)(&|#|;|$)').exec(location.search)||[,""])[1].replace(/\+/g, '%20'))||null
}

pieceID = getURLParameter('piece');

稍后,在填充我的标记时,我运行以下代码。

if (marker.feature.properties.pieceID == pieceID) {
    map.panTo(marker.getLatLng());
    marker.openPopup();
}

我还在geojson文件中为每个标记包含一个名为pieceID的属性。如果使用片段参数X传递URL并且标记的片段ID等于X,则在页面加载时它将打开。