如何从谷歌地图中删除旧标记新的?

时间:2015-12-15 12:24:39

标签: javascript jquery google-maps

我已经使用标记加载了默认的谷歌地图,并且必须使用当前标记删除以前的标记。我在页面加载之前使用了googolle mpa和initiazlie函数

请查看我的以下代码,并告诉我如何实现这一目标。

$(document).ready(function() {
var markers = [];

//var markers = [];
var track_click = 0; //track user click on "load more" button, righ now it is 0 click

var total_pages = 4;
var page;

$.ajax({
    url:"<?php echo base_url('search_jobs/fetch_jobs')?>",
      type: "POST",
       dataType: 'json', 
    beforeSend: function(){
    $('#loader-icon').show();
    },
    complete: function(){
    $('#loader-icon').hide();
    },
    success: function (result) {  



    //alert("hi");
    $('#jobsfound').html(result.search_results);

        var list = result.map_array;

        var image = "<?php echo base_url(); ?>assets/images/pros_marker.png";


        $.each(list, function(index, value) {

                var newLatLng = new google.maps.LatLng(list[index].latitude, list[index].longitude);
            console.log(list[index].latitude, list[index].longitude);


              markers [index] = new google.maps.Marker({
      map: map
    });
     markers[index].setPosition(newLatLng); 
                    //markers.push(marker);


        })

        //initialize();
    }

    })


 $.ajax({
    url:"<?php echo base_url('search_jobs/fetch_jobs')?>",
      type: "POST",

    data:'location_checkboxes='+ values + '&budget_value=' + budget_value  + 
    '&job_dates=' + job_date  + '&page='+track_click,
     dataType: 'json', 
    beforeSend: function(){
    $('#loader-icon').show();
    },
    complete: function(){
    $('#loader-icon').hide();
    },
    success: function (result) {
         $('#jobsfound').html(result.search_results);
         //alert($("#test").text());
        //initialize();
        var list = result.map_array;
        var image = "<?php echo base_url(); ?>assets/images/pros_marker.png";
        $.each(list, function(index, value) {

            var lat = parseFloat(value.latitude);
             var lng = parseFloat(value.longitude);
            var newLatLng = new google.maps.LatLng(list[index].latitude, list[index].longitude);

                    //markers[index] = new google.maps.Marker({
  //                            position: newLatLng,
 //                         icon:image,
 //                         map: map,
   //                           draggable: false
   //                       });

              markers [index] = new google.maps.Marker({
      map: map
    });
     markers[index].setPosition(newLatLng); 
                     //markers.push(marker);

        })

    }
})      
    google.maps.event.addDomListener(window, 'load', initialize);

1 个答案:

答案 0 :(得分:0)

在初始化函数之前将标记设置为全局数组(请参阅http://www.w3schools.com/googleapi/google_maps_basic.asp

var markers = [];

$(document).ready(function() {


//var markers = [];

//var markers = [];
var track_click = 0; //track user click on "load more" button, righ now it is 0 click

var total_pages = 4;
var page;

$.ajax({
    url:"<?php echo base_url('search_jobs/fetch_jobs')?>",
      type: "POST",
       dataType: 'json', 
    beforeSend: function(){
    $('#loader-icon').show();
    },
    complete: function(){
    $('#loader-icon').hide();
    },
    success: function (result) {  



    //alert("hi");
    $('#jobsfound').html(result.search_results);

        var list = result.map_array;

        var image = "<?php echo base_url(); ?>assets/images/pros_marker.png";


        $.each(list, function(index, value) {

            var contentString = '<div id="content">'+list[index].title+'</div>';


            var infowindow = new google.maps.InfoWindow({
                content: contentString
              });
                var newLatLng = new google.maps.LatLng(list[index].latitude, list[index].longitude);
            console.log(list[index].latitude, list[index].longitude);


             markers [index] = new google.maps.Marker({
                  icon:image,

                  map: map
            });

             markers[index].setPosition(newLatLng); 

              markers[index].addListener('mouseover', function() {
                infowindow.open(map, marker);
              });

                    //markers.push(marker);


        })

        //initialize();
    }

    })


 });