Javascript 通过ajax添加/删除传单标记

Javascript 通过ajax添加/删除传单标记,javascript,jquery,ajax,wordpress,leaflet,Javascript,Jquery,Ajax,Wordpress,Leaflet,我想通过ajax请求删除旧标记而不是新标记。第一次有效,第二次无效。我使用change事件进行此操作 我的问题 <code> if(map.hasLayer(markers)){ map.removeLayer(markers); } </code> map.hasLayer(markers)首次重新运行true并成功删除marker 第二次映射。hasLayer(markers)返回false,但标记现在仍显示在我的映射中 我还尝试使

我想通过ajax请求删除旧标记而不是新标记。第一次有效,第二次无效。我使用change事件进行此操作

我的问题

<code>

    if(map.hasLayer(markers)){
       map.removeLayer(markers);
    }

</code>
  • map.hasLayer(markers)首次重新运行true并成功删除marker
  • 第二次映射。hasLayer(markers)返回false,但标记现在仍显示在我的映射中
  • 我还尝试使用updatemarker.clearLayers()第一次工作,但第二次不工作
  • 注意:实际上我的问题是从地图上删除的第一个标记/图层。我使用变更事件。第一次变更有效,但第二次变更无效

    这是我的完整代码

    <code>
        jQuery(document).ready(function($) {
                // calculate distance
                function calcDistance(fromLat, fromLng, toLat, toLng) {
                    return google.maps.geometry.spherical.computeDistanceBetween(
                    new google.maps.LatLng(fromLat, fromLng), new google.maps.LatLng(toLat, toLng));
                }
                // get user current location
                var currentLocation = null;
                $('#get_location').on('click', function(){
                    var currentAction = $(this);
                    if ("geolocation" in navigator){
                        navigator.geolocation.getCurrentPosition(function(position){ 
                            var currentLatitude = position.coords.latitude;
                            var currentLongitude = position.coords.longitude;
                            currentLocation = currentLatitude +','+currentLongitude;
                            $(currentAction).parent().addClass('active');
                        });
                    }
                });
                $('.range-slider__range').on('change', function(){
                    var updatemarker = new L.MarkerClusterGroup();
                    var radius = parseInt($(this).val(), 10) * 1000;
                    var data = {
                        'action'            : 'my_action',
                        'radius'            : $(this).val(),
                        'currentLocation'   : currentLocation
                    };
                    // since 2.8 ajaxurl is always defined in the admin header and points to admin-ajax.php
                    jQuery.post('<?php print admin_url('admin-ajax.php'); ?>', data, function(response, status) {
                        if(status == 'success'){
                            var existingResponseInRadius = [];
                            var existingResponseHTML = '';
                            var currentLocationArray = currentLocation.split(',');
    
                            $.each(response,function(index, value){
                                var distance = calcDistance(currentLocationArray[0], currentLocationArray[1], response[index].lat, response[index].lng);
    
                                if (distance < radius){ 
                                    existingResponseInRadius.push(value);
                                    existingResponseHTML += value.content;
                                }      
                            });
                            $('#cplgr-listing-search-result').html(existingResponseHTML);
                            // update post counter
                            $('#post_count').text($('.listing-single').length);
    
                            var addressPoints = document.getElementsByClassName('listing-single');
    
                            console.log(map.hasLayer(markers));
    
                            if(map.hasLayer(markers)){
                                 map.removeLayer(markers);
                            }
    
                            if (addressPoints.length > 0 ){
                                for (var i = 0; i < addressPoints.length; i++) {
    
                                    var markerContent = document.getElementsByClassName('listing-single');
                                    // 
                                    var helloLondonHtmlIcon = new L.HtmlIcon({
                                        html: "<div class='marker mcn" + jQuery(addressPoints[i]).data("postid") + "' data-markerid=" + jQuery(addressPoints[i]).data("postid") + "><img src='" + jQuery(addressPoints[i]).data('mapicon') + "' alt='icon' /></div>",
                                        markerid: "" + jQuery(addressPoints[i]).data("postid") + "",
                                    });
                                    // 
                                    updatemarker.on('clusterclick', function () {
                                        var helloLondonHtmlIcon = new L.HtmlIcon({
                                            html: "<div class=marker mcn" + jQuery(addressPoints[i]).data("postid") + " data-markerid=" + jQuery(addressPoints[i]).data("postid") + "></div>",
                                            markerid: "" + jQuery(addressPoints[i]).data("postid") + "",
                                        });
                                    });
    
                                    // update map position
                                    map.panTo(new L.LatLng(jQuery(addressPoints[0]).data("lat"), jQuery(addressPoints[0]).data("lng")));
    
                                    var popupInfo = markerContent[i];
                                    var marker = L.marker(new L.LatLng(jQuery(addressPoints[i]).data("lat"), jQuery(addressPoints[i]).data("lng")), { icon: helloLondonHtmlIcon });
                                    marker.bindPopup(popupInfo.cloneNode(true), { minWidth: 300, offset: new L.Point(0, -170) });
                                    updatemarker.addLayer(marker);
                                }
                                map.addLayer(updatemarker);
                            } else {
                                updatemarker.clearLayers();
                                map.panTo(new L.LatLng('-52.945287', '12.014960'));
                            }
                        }
                    });
                });
            });
    
    </code>
    
    
    jQuery(文档).ready(函数($){
    //计算距离
    功能计算距离(fromLat、fromLng、toLat、toLng){
    返回google.maps.geometry.spheremic.computeDistanceBetween(
    新的google.maps.LatLng(fromLat,fromLng),新的google.maps.LatLng(toLat,toLng));
    }
    //获取用户当前位置
    var currentLocation=null;
    $('get#u location')。在('click',function()上{
    var currentAction=$(此);
    if(导航器中的“地理位置”){
    navigator.geolocation.getCurrentPosition(函数(位置){
    var currentLatitude=位置坐标纬度;
    var currentLength=position.coords.longitude;
    currentLocation=currentLatitude+','+currentLatitude;
    $(currentAction).parent().addClass('active');
    });
    }
    });
    $('.range-slider__range')。打开('change',function(){
    var updatemarker=newl.MarkerClusterGroup();
    var radius=parseInt($(this).val(),10)*1000;
    风险值数据={
    “行动”:“我的行动”,
    “radius”:$(this.val(),
    “currentLocation”:currentLocation
    };
    //因为2.8 ajaxurl总是在admin头中定义,并指向admin-ajax.php
    jQuery.post(“”,数据,函数(响应,状态){
    如果(状态=‘成功’){
    var existingResponseInRadius=[];
    var existingResponseHTML='';
    var currentLocationArray=currentLocation.split(',');
    $.each(响应、函数(索引、值){
    var距离=计算距离(currentLocationArray[0],currentLocationArray[1],response[index].lat,response[index].lng);
    如果(距离<半径){
    existingResponseInRadius.push(值);
    existingResponseHTML+=value.content;
    }      
    });
    $('#cplgr listing search result').html(existingResponseHTML);
    //更新邮政柜台
    $('#post_count').text($('.listing single').length);
    var addressPoints=document.getElementsByClassName('listing-single');
    console.log(map.hasLayer(markers));
    if(地图图层(标记)){
    地图移除层(标记);
    }
    如果(addressPoints.length>0){
    对于(变量i=0;i

    更改单击功能,例如“$”(“#获取位置”)。在('click',function(){” 到“$(document.body)。在('click','#get_location',函数(){”。。。
    它应该可以工作。

    没有get_位置可以工作。我的问题是$('.range-slider__range')。在('change',function(){}事件上