簡體   English   中英

單擊地圖外部的單張openPopup方法

[英]Leaflet openPopup method with a click from outside the map

我在地圖上使用了名為Leafpile的Leaflet and Leafleat插件。 基本上是數據庫結果的集合,此映射的javascript代碼如下:

var cmAttr = '© 2013 OpenStreetMap',
cmUrl = 'http://{s}.tile.cloudmade.com/BC9A493B41014CAABB98F0471D759707/{styleId}/256/{z}/{x}/{y}.png';

var minimal   = L.tileLayer(cmUrl, {styleId: 1, attribution: cmAttr})

var southWest = new L.LatLng(3.8642546157214084, -199.86328125);
var northEast = new L.LatLng(73.12494524712693 , -24.08203125);
var bounds = new L.LatLngBounds(southWest, northEast);

var markers = new L.LeafpileGroup();
L.marker([40.91, -74.15]).bindPopup('The html content').addTo(markers),
L.marker([37.7, -121.90]).bindPopup('The html content').addTo(markers);

var map = L.map('map', {
    center: [34.488616,-97.8692325],
    minZoom: 4,
    scrollWheelZoom: false,
    zoom: 5,
    layers: [minimal, markers]
});


map.setMaxBounds(bounds);

我遇到的問題是,當我單擊html中結果的鏈接時,我想在地圖上打開一個彈出窗口(map.openPopup(popup)方法或類似的方法!!)。

<ul id="set">
<li><a href="#">RESULT #1</a></li>
<li><a href="#">RESULT #2</a></li>
</ul>

我已經嘗試了一切,但沒有成功。 我想將此事件綁定到我的jQuery函數中,例如:

$(document.body).on("click",'#set a', function(e) {
    //other code that I need
});

我對stackoverflow還是很陌生,但是我找到了一個可行且似乎足夠有效的解決方案。

您應該先創建一個空數組

var array =[]

位置數組應包含所有坐標。 我用位置具有屬性的對象填充了array,該屬性具有相應的latlng值。

locationarray= [{name:"Hotel London",location:[51.5, -0.09]},{name:"Hotel Barnaby",location:[51.49, -0.088]},{name:"Hotel Rendelliare",location:[51.51, -0.12]},{name:"Hotel Francion",location:[51.52, -0.089]},{name:"Hotel Waterloo",location:[51.5, -0.11]}]

然后用標記對象填充vararray。

for(i=0;i<locationarray.length;i++){

vararray[i] = L.marker(locationarray[i].location).addTo(map);
vararray[i].bindPopup('<div id="hotel'+i+'">' + locationarray[i].name + '</div>')

}

然后使用for循環創建div。 在for循環中,為div分配一個onclick處理程序或觸發函數的任何內容。 將用於迭代的值傳遞給函數。

for(i=0,len = objarray.length; i<len ;i++){
$('#viewportRS').append('<div onclick="$.clicked('+i+')" class="hotels" id="'+i+'">'+ whatevervar +'</div>')}

該函數應排除數字參數,並在vararray中的該索引處調用openPopup()

$.clicked = function(id){


vararray[id].openPopup()


}

我已經對其進行了無數次測試,並且可以正常工作。 因此,如果出現問題,請通知我們。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM