Javascript 确保旧的标记保持在顶部

Javascript 确保旧的标记保持在顶部,javascript,google-maps-api-3,Javascript,Google Maps Api 3,我正在尝试创建一个功能,搜索附近的位置,并在单击默认标记时显示标记。如何确保相同的标记不会出现,或确保标记不会被新标记覆盖 这是我的代码: google.maps.event.addListener(标记,'click',函数(标记,i){ log(this.markerDataIndex,this.anotherDataIndex); infowindow.setContent(atm[this.markerDataIndex][this.anotherDataIndex][0]); 打开(

我正在尝试创建一个功能,搜索附近的位置,并在单击默认标记时显示标记。如何确保相同的标记不会出现,或确保标记不会被新标记覆盖

这是我的代码:

google.maps.event.addListener(标记,'click',函数(标记,i){
log(this.markerDataIndex,this.anotherDataIndex);
infowindow.setContent(atm[this.markerDataIndex][this.anotherDataIndex][0]);
打开(地图,这个);
setCenter(新的google.maps.LatLng(atm[this.markerDataIndex][this.anotherDataIndex][1],atm[this.markerDataIndex][this.anotherDataIndex][2]);
map.setZoom(17);
var man=new google.maps.LatLng(atm[this.markerDataIndex][this.anotherDataIndex][1],atm[this.markerDataIndex][this.anotherDataIndex][2]);
var请求={
地点:男子,
半径:200,
类型:[“购物中心”、“商店”]
};
placesList=document.getElementById('places');
var service=newgoogle.maps.places.PlacesService(地图);
服务.nearbySearch(请求、回调);
});
}
}
}
函数回调(结果、状态、分页){
if(status!=google.maps.places.PlacesServiceStatus.OK){
返回;
}否则{
创建标记(结果);
if(分页。下一页){
var moreButton=document.getElementById('more');
moreButton.disabled=false;
google.maps.event.AddDomainListenerOnce(更多按钮,'点击',
函数(){
moreButton.disabled=true;
分页。下一页();
});
}
}
}
函数createMarkers(位置){
var bounds=new google.maps.LatLngBounds();
for(var i=0,place;place=places[i];i++){
var tested=new google.maps.Marker({
地图:地图,
标题:place.name,
位置:place.geometry.location
});
placesList.innerHTML+='
  • '+place.name+'
  • '; } }
    将旧标记放在前面很容易,给新标记(在
    createMarkers()
    中创建)一个
    zIndex
    -1

    为了避免在同一位置重复标记,可以将“旧”标记()存储在对象中

    var defaultMarkers={}
    
    作为属性名,使用标记位置的URL值

    //inside setMarkers, after the creation of the markers in the loop
    defaultMarkers[myLatLng.toUrlValue()]=marker;
    
    内部
    createMarkers()
    检查
    defaultMarkers
    内部是否有名称等于放置位置的URL值的标记,以及在不创建新标记时

    if(!defaultMarkers[place.geometry.location.toUrlValue()]){
             var tested = new google.maps.Marker({
                 map: map,
                 title: place.name,
                 position: place.geometry.location,
                 zIndex:-1
               });
          }
    

    请更正问题中的代码。你丢失了一些东西。你能提供如何重现问题的说明吗?见:
    if(!defaultMarkers[place.geometry.location.toUrlValue()]){
             var tested = new google.maps.Marker({
                 map: map,
                 title: place.name,
                 position: place.geometry.location,
                 zIndex:-1
               });
          }