2012-10-22 64 views
1

我和小葉openPopup方法的問題。單張及Mapbox:OpenPopup不工作

showMap = function(elements) { 
    var jsonp = 'http://a.tiles.mapbox.com/v3/blahblahblah.jsonp'; 
    var m = new L.Map("my_map").setView(new L.LatLng(51.5, -0.09), 15); 

    var geojsonLayer = new L.GeoJSON(); 

    var PlaceIcon = L.Icon.extend({ 
     iconSize: new L.Point(25, 41), 
     shadowSize: new L.Point(40, 35), 
     popupAnchor: new L.Point(0, -30) 
    }); 

    var icon = new PlaceIcon(__HOME__ + '/images/leaflet/marker.png'); 
    var marker; 


    for (var i = 0; i < elements.length; i++) { 
     var address = $("<div/>").html(elements[i].address).text(); 
     var latlng = new L.LatLng(elements[i].latitude, elements[i].longitude); 
     marker = new L.Marker(latlng, {icon: icon}).bindPopup(address); 

     if (i == 0) { 
      marker.openPopup(); 
     } 
     m.addLayer(geojsonLayer).addLayer(marker); 
    } 
    // Get metadata about the map from MapBox 
    wax.tilejson(jsonp, function(tilejson) { 
     m.addLayer(new wax.leaf.connector(tilejson)); 
    }); 
} 

當我點擊一個標記,我打開彈出窗口。但是我想在加載地圖時打開第一個彈出窗口。 (和標記打開其他彈出窗口點擊)

ANNY想法?

回答

2

我假設當你點擊你看到彈出一個標誌,但你沒有得到載入地圖時的第一標記的彈出窗口自動顯示?

首先,它看起來並不像你實際使用以GeoJSON所以GeoJSON的層是沒有必要的(你可以只使用一個FeatureLayer),但應該不會造成任何問題。無論圖層組你使用你應該只一次將它添加到地圖中,然後將所有子層的圖層組。您目前多次在您不想執行的「for」循環中添加geojsonLayer。 其次,你得叫marker.openPopup()的標記添加到地圖。 試着改變你的代碼周圍看起來是這樣的:

var layerGroup = new L.FeatureGroup(); 
layerGroup.addTo(m); 

for (var i = 0; i < elements.length; i++) { 
    var address = $("<div/>").html(elements[i].address).text(); 
    var latlng = new L.LatLng(elements[i].latitude, elements[i].longitude); 
    marker = new L.Marker(latlng, {icon: icon}).bindPopup(address); 

    //You don't add the marker directly to the map. The layerGroup has already 
    //been added to the map so it will take care of adding the marker to the map 
    layerGroup.addLayer(marker); 

    if (i == 0) { 
     marker.openPopup(); 
    } 
} 
5

將openPopup調用您的標記添加到地圖中,你應該罰款。