简体   繁体   English

本地存储中的传单标记,如何获取标记经度并从存储中排除?

[英]Leaflet markers on localstorage, how to get marker lat lng and exclude from storage?

I created a function that on click places a marker on map, and save it to localstorage. 我创建了一个函数,只需单击一下即可在地图上放置标记,然后将其保存到本地存储。 Inside the marker popup, its displayed the marker position, and a delete button. 在标记弹出窗口内,其显示标记位置和一个删除按钮。

How I can make that delete button get actual marker position, compare to the ones stored in localstorage, and if find an equal value, delete it from local storage? 如何使该删除按钮获得实际的标记位置,并与存储在本地存储中的标记进行比较,如果找到相等的值,则将其从本地存储中删除?

Using this: 使用这个:

for ( var i = 0, len = localStorage.length; i < len; ++i ) {
  console.log( localStorage.getItem( localStorage.key( i ) ) );
}

I get the what's in localstorage, I noticed that the markers are saved with a "\\", any way to improve this code? 我得到了本地存储中的内容,我注意到标记以“ \\”保存,有什么方法可以改进此代码?

["{\"lat\":1780,\"lng\":456}","{\"lat\":1280,\"lng\":904}","{\"lat\":1000,\"lng\":-132}","{\"lat\":216,\"lng\":300}"]

The code: 编码:

    function onMapClick(e) {

    var geojsonFeature = {
        "type": "Feature",
        "properties": {},
        "geometry": {
            "type": "Point",
            "coordinates": [e.latlng.lat, e.latlng.lng]
        }
    }

    var marker;

    L.geoJson(geojsonFeature, {

        pointToLayer: function(feature, latlng){

            marker = L.marker(e.latlng, {

            title: "Resource Location",
            alt: "Resource Location",
            riseOnHover: true,
            draggable: false,
            icon: totem

            }).bindPopup("<span>X: " + e.latlng.lng + ", Y: " + e.latlng.lat + "</span><br><a href='#' id='marker-delete-button'>Delete marker</a>");

            marker.on("popupopen", onPopupOpen);

            marker.on("dragend", function (ev) {

                var chagedPos = ev.target.getLatLng();
                this.bindPopup(chagedPos.toString()).openPopup();

            });

          // Begin store markers in local storage
          storeMarker(e.latlng);
          // End store markers in local storage

            return marker;
        }
    }).addTo(map);
}

function onPopupOpen() {
    var tempMarker = this;

    $("#marker-delete-button:visible").click(function () {

        map.removeLayer(tempMarker);
        localStorage.removeItem("markers");
    });
}

/// Load markers
function loadMarkers(){
    var markers = localStorage.getItem("markers");
    if(!markers) return;
    markers = JSON.parse(markers);
    markers.forEach(function(entry) {
        latlng = JSON.parse(entry);
            var geojsonFeature = {
                "type": "Feature",
                "properties": {},
                "geometry": {
                    "type": "Point",
                    "coordinates": [latlng.lat, latlng.lng]
                }
            }

        var marker;

        L.geoJson(geojsonFeature, {

            pointToLayer: function(feature){

                marker = L.marker(latlng, {

                    title: "Resource Location",
                    alt: "Resource Location",
                    riseOnHover: true,
                    draggable: true,
                    icon: totem


                }).bindPopup("<span>X: " + latlng.lng + ", Y: " + latlng.lat + "</span><br><a href='#' id='marker-delete-button'>Delete marker</a>");

                marker.on("popupopen", onPopupOpen);

                return marker;
            }
        }).addTo(map);
    });
}
/// Store markers
function storeMarker(marker){

    var markers = localStorage.getItem("markers");
    if(!markers) {
        markers = new Array();
        console.log(marker);
        markers.push(JSON.stringify(marker));
    }
    else
    {
        markers = JSON.parse(markers);
        markers.push(JSON.stringify(marker));
    }
    console.log(JSON.stringify(markers));
    localStorage.setItem('markers', JSON.stringify(markers));
}


    map.on('click', onMapClick);

    loadMarkers();

You seem to be confused about what locaStorage is/does, it stores data as key:value pairs, so what you have there is basically a list of stuff where you have: 您似乎对locaStorage是/确实是什么感到困惑,它将数据存储为key:value对,因此您所拥有的基本上是您所拥有的东西的列表:

key: \"lat\":1780,
value: \"lng\":456

As for deleting geoJSON features, a possibility is to use is the function onEachFeature() that can bind a function to geoJSON features before they are added to the map, so perhaps you can use that to bind a delete function when the delete button of the popup is clicked but while it can remove the layer from the map, it will not wipe the data from localStorage as leaflet as no way of referencing the data there. 至于删除geoJSON功能,可以使用的方法是onEachFeature()函数,该函数可以在将geoJSON特征添加到地图之前将其绑定到geoJSON特征,因此,当单击了弹出窗口,但是尽管它可以从地图上删除图层,但不会像从叶中那样从本地存储中擦除数据,因为无法引用那里的数据。

Another possibility is to add unique id's to your points when creating them so you can reference them more easily when you want to remove them from database. 另一种可能性是在创建点时将唯一的ID添加到您的点中,以便在要将其从数据库中删除时可以更轻松地引用它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM