我有一个脚本,可以从一些自定义字段在地图上创建制作者。在我在页面中插入ajax之前,标记在我更改页面时重置。现在,在我插入ajax之后,我需要重置我的标记并在触发事件后重新创建它们。我如何重置我的标记?这是我的谷歌地图代码。谢谢!
(function($) {
'use strict';
var marker = [];
function handleHouse(house, map, geocoder, infowindow) {
geocoder.geocode({'address': house.address}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
var marker = new google.maps.Marker({
map: map,
position: results[0].geometry.location
});
google.maps.event.addListener(marker, 'click', function() {
infowindow.setContent(house.title + house.desc);
infowindow.open(map, marker);
});
}
});
}
function handleHouses(houses, map, geocoder, infowindow) {
for (var id in houses) {
if (houses.hasOwnProperty(id)) {
handleHouse(houses[id], map, geocoder, infowindow);
}
}
}
$(function() {
if (typeof HOUSE_DATA === 'undefined') {
return;
}
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 11,
center: new google.maps.LatLng(36.5819074, -4.8703777,11.25),
mapTypeId: google.maps.MapTypeId.ROADMAP
});
var infowindow = new google.maps.InfoWindow();
var geocoder = new google.maps.Geocoder();
handleHouses(HOUSE_DATA, map, geocoder, infowindow);
$('#houses-result').on('houses-refresh', function() {
alert ('markers reseted');
handleHouses(HOUSE_DATA, map, geocoder, infowindow);
});
});
}(jQuery));
答案 0 :(得分:1)
您应该将反射存储在数组中的标记中,这样您就可以遍历数组并使用标记进行操作。 Here是一个示例,显示如何隐藏地图上的所有标记。替换此示例,您应该能够以任何方式更改标记。
所以看起来应该是这样的:
var marker = [];
var markers = [];
function handleHouse(house, map, geocoder, infowindow) {
geocoder.geocode({'address': house.address}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
var marker = new google.maps.Marker({
map: map,
position: results[0].geometry.location
});
markers.push(marker);
google.maps.event.addListener(marker, 'click', function() {
infowindow.setContent(house.title + house.desc);
infowindow.open(map, marker);
});
}
});
}
然后娱乐:
$('#houses-result').on('houses-refresh', function() {
alert ('markers reseted');
for (var i = 0; i < markers.length; i++) {
markers[i].setMap(null);
}
markers = [];
handleHouses(HOUSE_DATA, map, geocoder, infowindow);
});