javascript - LeafletJS, How to delete marker with button in bind popup - Stack Overflow

I'm making a web application using a leaflet map and I want the users to be able to put and remove

I'm making a web application using a leaflet map and I want the users to be able to put and remove markers as they wish but I can't figure out a way to do it.

The markers coordinates are stored in a database and when I load the page I use the coordinates (GET request) to put them on the map. And now I'm trying to find a way to delete a marker when users press a button located in the marker leaflet bindPopup.

$.ajax({
      //GET REQUEST
    })
    .done(function( data ) {
      for (i=0 ; i < data.length ; i++) { 

        // ......

        var mp = new L.Marker([marker["lat"], marker["lng"]],{draggable:'true'});
        mp.addTo(mymap);
        mp.bindPopup('<button type="button" onclick="">Delete Marker</button>',{maxWidth: "auto"}).openPopup(); //Here i want to add a function that deletes a specific marker 
        markers.addLayer(mp);

The main problem I have is when I put the code in the onclick part of the button to delete the marker, it doesn't take the environment variable and therefore not the marker so I can't even send a DELETE request without the id.

I'm making a web application using a leaflet map and I want the users to be able to put and remove markers as they wish but I can't figure out a way to do it.

The markers coordinates are stored in a database and when I load the page I use the coordinates (GET request) to put them on the map. And now I'm trying to find a way to delete a marker when users press a button located in the marker leaflet bindPopup.

$.ajax({
      //GET REQUEST
    })
    .done(function( data ) {
      for (i=0 ; i < data.length ; i++) { 

        // ......

        var mp = new L.Marker([marker["lat"], marker["lng"]],{draggable:'true'});
        mp.addTo(mymap);
        mp.bindPopup('<button type="button" onclick="">Delete Marker</button>',{maxWidth: "auto"}).openPopup(); //Here i want to add a function that deletes a specific marker 
        markers.addLayer(mp);

The main problem I have is when I put the code in the onclick part of the button to delete the marker, it doesn't take the environment variable and therefore not the marker so I can't even send a DELETE request without the id.

Share Improve this question edited Jul 18, 2019 at 14:06 Reza P. 3062 silver badges18 bronze badges asked Jul 18, 2019 at 14:01 DeyamiDeyami 111 silver badge6 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 8

Here is a working demo using document.createElement('button')

Note: bindPopup accepts HTMLElement

var mymap = L.map('Lmap').setView([41.349412, 2.151421], 10);

L.tileLayer('http://{s}.tile.openstreetmap/{z}/{x}/{y}.png', {
  maxZoom: 18,
  fadeAnimation: false,
  zoomAnimation: false,
  markerZoomAnimation: false,
  attribution: '&copy; <a href="http://osm/copyright">OpenStreetMap</a> contributors',
}).addTo(mymap);

var mp = new L.Marker([41.349412, 2.151421], {
  draggable: 'true'
});
mp.addTo(mymap);

let btn = document.createElement('button');
btn.innerText = 'Delete Marker';
btn.onclick =  function() {
  mymap.removeLayer(mp);
}

mp.bindPopup(btn, {
  maxWidth: 'auto'
}).openPopup();
#Lmap {
  position: absolute;
  top: 35px;
  left: 0;
  width: 100%;
  height: 80%
}
<link rel="stylesheet" href="https://unpkg./[email protected]/dist/leaflet.css" />
<script src="https://unpkg./[email protected]/dist/leaflet.js"></script>

<div id="Lmap"></div>

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744998655a4605356.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信