Javascript 在不重新加载google地图的情况下更新/重新加载标记

Javascript 在不重新加载google地图的情况下更新/重新加载标记,javascript,jquery,google-maps,Javascript,Jquery,Google Maps,我使用下面的代码来生成标记引脚。它可以完美地加载,但在这张地图的左边我有过滤器。如何在不重新加载地图的情况下重新加载标记?这已经造成了一些挫折,因此任何帮助都将不胜感激 非常感谢, //Google map results var contentStrings = []; var infowindow = new google.maps.InfoWindow(); var mapinited = false; var map

我使用下面的代码来生成标记引脚。它可以完美地加载,但在这张地图的左边我有过滤器。如何在不重新加载地图的情况下重新加载标记?这已经造成了一些挫折,因此任何帮助都将不胜感激

非常感谢,

    //Google map results
        var contentStrings = [];
        var infowindow = new google.maps.InfoWindow();
        var mapinited = false;
        var map;
        var myOptions = {
          zoom: 11,
          center: myLatlng,
          mapTypeId: google.maps.MapTypeId.ROADMAP
        }
        var currentinfobox;
        var myLatlng;
        var markersArray=[];
        var LatLngList = [];

$().ready(function() {

    //reinit search 
    if (window.location.hash) {
        submitForm(window.location.hash.replace('#',''));
    }
    else if (readCookie('sf')) {
            //submitForm(readCookie('sf'));
    }

    //init map
    $('#map_view').click(function() {
        if (mapinited) {
            return;
        } else {
            mapinited = true;
            initMap();
        }



    function initMap() {
            locate(["Ireland"],function(result) {
        map = new google.maps.Map(document.getElementById("search_map"), myOptions);
        myLatlng = new google.maps.LatLng(result.lat(),result.lng());

              var key =0;

               $.each(map_results, function(key, value){
        LatLngList[key] = new google.maps.LatLng(value.lat,value.long)
        contentStrings[key] =
                '<div id="ginfo_content" class="map-pop-up">'+
                  '<span class="content-top">&nbsp;</span>'+
                  '<div class="content-middle">'+
                    '<div class="map-filler">'+
                      '<a class="map-close" href="javascript:;" onclick="infowindow.close();" title="Close">x</a>'+
                      '<br class="clearfix">'+
                      '<div class="map-pop-up-left">'+
                        '<a href="profile.php?id='+ value.user_id +'"><div class="thumbnail"><img src="'+ value.image +'" width="64" height="64"></div></a>'+

                        '<a href="javascript:;" class="user-contact" onClick="to='+ value.user_id +';contact_showCaptcha();pop_up(\'pop-up-contact\');">Contact</a>'+

                      '</div>'+
                      '<div class="map-pop-up-right">'+
                        '<h2><a href="profile.php?id='+ value.user_id +'">'+ value.firstname +' '+value.lastname+',</a> '+ value.address +'</h2>'+
                        '<p>'+ stripslashes(value.about) +'</p>'+
                      '</div>'+
                      '<br class="clearfix">'+
                      '<div class="map-pop-up-footer"><a href="profile.php?id='+ value.user_id +'" class="view-profile">View Profile</a><span class="telephone">Telephone: '+ value.phone +'</span></div>'+
                    '</div>'+
                  '</div>'+
                  '<span class="content-bottom">&nbsp;</span>'+
                '</div>';
                 key++;
               });//end each

                map_results="";

        google.maps.event.addListener(infowindow, 'domready', function() {
            var infocontent = $('#ginfo_content').clone();
            var l = $('#ginfo_content').parent().parent().parent().addClass('original_popup').html('');
            $('.original_popup').append(infocontent).show();
            $('.original_popup').css('width','360px').css('height','230px').css('left','+=27px').css('top','+=65px');
        });

        var zoomChangeBoundsListener = google.maps.event.addListener(map, 'zoom_changed', function() {
            if (this.getZoom() > 14) // Change max/min zoom here
                this.setZoom(14);        
            google.maps.event.removeListener(zoomChangeBoundsListener);
        });
        var infoboxlistener = google.maps.event.addListener(map, 'zoom_changed', `enter code here`function() {
            infowindow.close();
        });
        loadMapInit(LatLngList,contentStrings);

    });
    }

    });

  });
//谷歌地图结果
var contentStrings=[];
var infowindow=new google.maps.infowindow();
var mapinited=false;
var映射;
变量myOptions={
缩放:11,
中心:myLatlng,
mapTypeId:google.maps.mapTypeId.ROADMAP
}
var-currentinfobox;
var Mylatng;
var-markersArray=[];
var LatLngList=[];
$().ready(函数()){
//重新搜索
if(window.location.hash){
submitForm(window.location.hash.replace('#','');
}
else if(readCookie('sf')){
//提交格式(readCookie('sf'));
}
//初始映射
$(“#映射视图”)。单击(函数(){
如果(mapinited){
返回;
}否则{
mapinited=true;
initMap();
}
函数initMap(){
定位([“爱尔兰”],函数(结果){
map=new google.maps.map(document.getElementById(“搜索地图”),myOptions);
myLatlng=new google.maps.LatLng(result.lat(),result.lng());
var-key=0;
$。每个(映射结果、函数(键、值){
LatLngList[key]=新的google.maps.LatLng(value.lat,value.long)
ContentString[键]=
''+
' '+
''+
''+
''+
“
”+ ''+ ''+ ''+ ''+ ''+ “+value.address+”+ “”+stripslashes(value.about)+“

”+ ''+ “
”+ '电话:'+value.phone+''+ ''+ ''+ ' '+ ''; key++; })//结束每一个 map_results=“”; google.maps.event.addListener(infowindow,'domready',function(){ var infocontent=$('#ginfo_content').clone(); var l=$('ginfo_content').parent().parent().parent().addClass('original#u popup').html(“”); $('.original_popup').append(infocontent.show(); $('.original_popup').css('width','360px').css('height','230px').css('left','+=27px').css('top','+=65px'); }); var zoomchangebundslistener=google.maps.event.addListener(映射'zoom_changed',函数(){ if(this.getZoom()>14)//在此处更改最大/最小缩放 这是setZoom(14); google.maps.event.removeListener(zoomChangeBoundsListener); }); var infoboxlistener=google.maps.event.addListener(映射'zoom_changed','enter code here'函数(){ infowindow.close(); }); loadMapInit(LatLngList,contentStrings); }); } }); });
希望这能回答您的问题:

创建标记时,可以使用“映射”参数设置其映射并使其显示。或者,如果要将其绑定到过滤器,可以忽略map参数,稍后使用marker.setMap(map)

如果要对事件“删除”和“添加”标记,可以使用marker.setMap(null)删除标记,并使用marker.setMap(map)重新添加

var marker = new google.maps.Marker({
    position: myLatlng,
    title:"Hello World!"
});

// To add the marker to the map, call setMap();
marker.setMap(map);

// To remove the marker from the map
marker.setMap(null);

更新:
因此,如果要“重新加载”标记,可以遍历当前活动标记的数组,将其映射设置为null,然后在映射上重置它们。

My page在两种情况下加载映射1)首先加载2)通过ajax调用不断刷新google映射

以下几点对我有用

<input type="hidden" id="lonDeg"><!--route lat to here via ajax call-->
<input type="hidden" id="latDeg"><!--route lon to here via ajax call-->

 <script>
  var map;
  function initMap() {
    // these two lines are the only variation from the native google 
    // API code.  They allow for dynamic updates of the lon/lat (below)
    var lonDeg = parseFloat($("#lonDeg").val());
    var latDeg = parseFloat($("#latDeg").val());

    var midp = {lat:latDeg, lng:lonDeg };

    map = new google.maps.Map(document.getElementById('map'), {
      scaleControl: true,
      center: midp,
      zoom: 10

    });
     var marker = new google.maps.Marker({map: map, position: midp});
        marker.addListener('click', function() {
      infowindow.open(map, marker);
    });
  }
    </script>

在我的例子中,我使用SVG图标,在标记(及其图标)出现在地图上之后,我在代码中更改
strokeColor

要使贴图显示新颜色,我只需再次调用setMap:

marker.setMap(map);
我发现没有必要先用
marker.setMap(null)删除它


我有一个预感,这将与标记的其他更改一起工作,如其图标的URL等。

非常欢迎。当我在过去这样做时,我在页面上保留了一个所有标记的数组(如果它们在不同的“层”中,则使用散列数组),要打开和关闭每个层,只需遍历该数组索引并相应地设置标记。
<button id="getNextMap">Go to Next Map</button>
    var qstring = 'myArgsToPass';
    var csv  = new Array();

    $.ajax({
        url: 'mapProfileGrabber.php',
        type: 'POST',
        data: {q:qstring},
        success: function(data) {
            csv = data.split(",");                        
            $("#lonDeg").val(csv[0]);
            $("#latDeg").val(csv[1]);

            initMap();
    }
marker.setMap(map);