2014-03-06 40 views
2

我設法用leaflet.js創建地圖& jQuery mobile。 現在我需要擺脫jQuery手機,而只是使用jQuery。小冊子地圖:使多邊形可點擊

一切正常,但我不能點擊我在地圖上繪製的多邊形了。它曾與jQuery手機合作過。

任何提示?

這裏是我的簡化代碼:

var map = L.map('map', { 
     zoomControl: false 
    }); 
    L.tileLayer('http://{s}.tile.cloudmade.com/**apikey***/997/256/{z}/{x}/{y}.png', { 
     attribution: 'Map data &copy; <a href="http://openstreetmap.org">OpenStreetMap</a> contributors, <a href="http://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>, Imagery &copy; <a href="http://cloudmade.com">CloudMade</a>', 
     maxZoom: 18 
    }).addTo(map); 

對於多邊形:

var geojsonFeature = { "type": "Polygon","coordinates": value.polygon};        
var polycolor = getGebColor(value.geb_nr);       
var geojsonStyle = {"color": polycolor};        
polygons[i] = L.geoJson(geojsonFeature, {style: geojsonStyle}).addTo(map); 

// make clickable 
polygons[i].on('click', function(e) { 
     if (lastMarker) { 
     map.removeLayer(lastMarker); 
    } 
    var url = "http://*****/tugetherMap.php?callback=&id="+value.id+"&type=B"; 
    markers[i] = L.marker([value.point[1], value.point[0]]).addTo(map); 
    gebName = value.nameLang;          

    markers[i].bindPopup("<a class='gebOnMap' href='gebaeude.html' >"+gebName+"</a>").openPopup(); 
    lastMarker = markers[i];        
    }); 

多邊形[I]。對( '點擊',...)是其中一部分不再工作了。它適用於map.on(「點擊」,...)

+0

在將多邊形添加到地圖之前,您是否嘗試添加點擊功能? – apohl

+0

沒有。但我只是嘗試了一下,它給了我一個錯誤,因爲多邊形在那個時候沒有定義。 – lornz

+0

你可以分割它,所以你定義多邊形,添加點擊功能,然後將其添加到地圖。 'polygons [i] = L.geoJson(geojsonFeature,{style:geojsonStyle});' '多邊形[I]。對(' 點擊 '功能(E){};' '多邊形[I] .addTo(地圖);」 – apohl

回答

0

有一個問題與此並用以下

function onEachFeature(feature, layer) { 
    // Do all your popups and other binding in here 
    // does this feature have a property named popupContent? 
    if (feature.properties && feature.properties.popupContent) { 
     layer.bindPopup(feature.properties.popupContent); 
    } 
} 

var geojsonFeature = { 
"type": "Feature", 
"properties": { 
    "name": "Coors Field", 
    "amenity": "Baseball Stadium", 
    "popupContent": "This is where the Rockies play!" 
}, 
"geometry": { 
    "type": "Point", 
    "coordinates": [-104.99404, 39.75621] 
} 
}; 

L.geoJson(geojsonFeature, { 
    onEachFeature: onEachFeature 
}).addTo(map); 

在你的代碼,我想解決它,它會

polygons[i] = L.geoJson(geojsonFeature, {onEachFeature: onEachFeature, 
             style: geojsonStyle}).addTo(map); 



function onEachFeature(feature, layer) { 
    // Some jazz and magic you need to do with your layer and feature objects 
} 
+0

感謝您的提示我已經嘗試過,我認爲這是創建彈出式對象的清潔解決方案 不幸的是,它並沒有幫助我的案例 – lornz

+0

這個功能的全部目的是做這一項工作,讓她感到羞恥她讓你失望,我會說這是最好的方法爲什麼沒有爲你工作有沒有控制檯錯誤? –

+0

沒有錯誤。沒有任何反應。這是問題。我沒有反應點擊多邊形 – lornz

0

您需要將每個多邊形綁定到單擊事件處理程序,如下所示。

L.geoJson(geojsonFeature, { 
    onEachFeature: function(feature, layer) { 
     layer.on('click', function(e) { 
      // Do whatever you want here, when the polygon is clicked. 
     }); 
    } 
}).addTo(map); 
0

我的解決方案是將Leaflet降級到0.7.3或升級到1.0-beta2(撰寫本文時的最新版本)。