2017-04-04 56 views
1

我要添加在地圖上一個新的標記,當我點擊右鍵,添加新的標記,當我點擊地圖(OpenStreetMap的,單張JS)

function InitialiserCarte() { 

var map = L.map('map').setView([48.866667, 2.333333], 17); 

// create the tile layer with correct attribution 
var tuileUrl = 'http://{s}.tile.osm.org/{z}/{x}/{y}.png'; 

var attrib='Map data © <a href="http://openstreetmap.org">OpenStreetMap</a> contributors'; 

var osm = L.tileLayer(tuileUrl, { 
    minZoom: 8, 
    maxZoom: 17, 
    attribution: attrib 
}); 

osm.addTo(map); 

var marker = L.marker([48.866667, 2.333333]).addTo(map);} 

上,我打電話與這個jQuery這個功能(負載的頁面)

$(document).ready(function(){ 
    InitialiserCarte(); 
}); 

是否可以通過點擊操作動態添加標記?

+0

可能與http://stackoverflow.com/questions/18388288/how-do-you-add-marker-to-map-using-leaflet-map-onclick-function-event-handl和http:/ /stackoverflow.com/questions/9912145/leaflet-how-to-find-existing-markers-and-delete-markers/24342585#24342585 – user2441511

回答

1

從這裏開始:the Leaflet Quick Start guide。 「處理事件」部分討論如何添加事件。從這個快速入門指南,下面是鼠標點擊添加彈出一些示例代碼:

var popup = L.popup(); 

function onMapClick(e) { 
    popup 
     .setLatLng(e.latlng) 
     .setContent("You clicked the map at " + e.latlng.toString()) 
     .openOn(mymap); 
} 

mymap.on('click', onMapClick); 

嘗試修改onMapClick函數添加的,而不是一個彈出的標記。您需要使用e.latlng來設置標記位置。

+1

謝謝我已經理解,它的工作原理 –