我正在尝试创建一个搜索附近位置的功能,并在点击默认标记时显示标记。如何确保不会出现相同的标记或确保标记不会被新标记掩盖。
这是我的代码: http://jsfiddle.net/fq57xf7e/
google.maps.event.addListener(marker, 'click', function(marker, i) {
console.log(this.markerDataIndex,this.anotherDataIndex);
infowindow.setContent(atm[this.markerDataIndex][this.anotherDataIndex][0]);
infowindow.open(map, this);
map.setCenter(new 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 request = {
location: man,
radius: 200,
types: ['shopping_mall','store']
};
placesList = document.getElementById('places');
var service = new google.maps.places.PlacesService(map);
service.nearbySearch(request, callback);
});
}
}
}
function callback(results, status, pagination) {
if (status != google.maps.places.PlacesServiceStatus.OK) {
return;
} else {
createMarkers(results);
if (pagination.hasNextPage) {
var moreButton = document.getElementById('more');
moreButton.disabled = false;
google.maps.event.addDomListenerOnce(moreButton, 'click',
function() {
moreButton.disabled = true;
pagination.nextPage();
});
}
}
}
function createMarkers(places) {
var bounds = new google.maps.LatLngBounds();
for (var i = 0, place; place = places[i]; i++) {
var tested = new google.maps.Marker({
map: map,
title: place.name,
position: place.geometry.location
});
placesList.innerHTML += '<li>' + place.name + '</li>';
}
}
答案 0 :(得分:0)
将旧标记放在前面很容易,将新标记(在createMarkers()
中创建)zIndex
-1
。{/ p>
为避免在同一位置存在重复的标记,您可以将“旧”标记()存储在对象中。
var defaultMarkers={}
因为属性名称使用标记位置的UrlValue
//inside setMarkers, after the creation of the markers in the loop
defaultMarkers[myLatLng.toUrlValue()]=marker;
在createMarkers()
内查看defaultMarkers
内是否有一个标记,其名称等于地点位置的UrlValue,以及何时不创建新标记
if(!defaultMarkers[place.geometry.location.toUrlValue()]){
var tested = new google.maps.Marker({
map: map,
title: place.name,
position: place.geometry.location,
zIndex:-1
});
}