【发布时间】:2014-01-24 13:04:43
【问题描述】:
我使用地理编码允许用户导航到特定地点,然后将特定半径内的标记添加到该导航点。如果用户再次单击按钮并想要导航到另一个地方,它应该删除第一个请求的所有标记。我不得不说导航点用蓝色标记标记,其他点用红色标记标记。我使用这段代码来做到这一点:
// global
var previousTarget = 0;
var markers = new Array(); // All my markers are stored here
// end global
geocoder.geocode(
{'address': address},
function(results, status){
if(status == google.maps.GeocoderStatus.OK){
map.setCenter(results[0].geometry.location);
marker = new google.maps.Marker({
map: map,
position: results[0].geometry.location,
icon: 'http://maps.google.com/mapfiles/ms/micons/blue.png'
});
var marker, i;
var splitted = locations.splitted(",");
for(var i = 0; i < splitted.length; i++){
geocoder.geocode(
{'address': splitted[i]},
function(results, status){
if(results != null){
marker = new google.maps.Marker({
map: map,
position: results[0].geometry.location
});
markers.push(marker);
google.maps.event.addListener(marker, 'click', (function(marker, i){
return function() {
$.ajax({
url: 'myfile.php',
type: 'POST',
data: {addrData: jqXHR.responseText},
success: function(datas, textStatus, jqXHR){
for(var l = 0; l < markers.length; l++){
if(marker == markers[l]){
infowindow.setContent('test');
infowindow.open(map, marker);
}
}
},
error: function(jqXHR, textStatus, errorThrown){
alert("Error");
}
});
}
})(marker, i));
}
}
);
if(previousTarget > 1){ // Here is the function to click the button twice
markers.setMap(null);
}
}
}
}
);
如果我这样做,通过按两次按钮会在控制台中出现错误:
Uncaught TypeError: Object [object Array] has no method 'setMap'
如果我将markers 更改为marker,它只会删除蓝色标记。谁能给我一个提示,我在哪里犯了错误?
【问题讨论】:
-
更多关于 MrUpsidown 答案的信息可以在这里找到:developers.google.com/maps/documentation/javascript/examples/…
标签: javascript jquery google-maps google-maps-api-3