我正在研究一个使用Google Maps API V3和JQuery的项目。基本上,该程序根据用户选择的时间从数据库中调用项目,然后将这些项目作为标记显示在地图上。
到现在为止,它一直工作正常。我想在用户更改时间时清除标记,以使无关的标记不会留在地图上。
但是,当我将clearLocations()
函数附加到任何JQuery时间选择元素时,都会引发错误:
对象#没有方法'setMap'`。
这是Google Maps的JavaScript,在调用时会引发错误:
function clearLocations() {
if (markers) {
for (i in markers) {
markers[i].setMap(null);
}
markers.length = 0;
}
}
当滑动器移动时,这是JQuery函数调用它:
function slideTime(event, ui){
var val0 = $("#time-slider").slider("values", 0),
val1 = $("#time-slider").slider("values", 1),
chosenDate = Math.round(Date.parse($('#datepicker').datetimepicker('getDate')) / 1000),
minrange = chosenDate + (val0 * 60),
maxrange = chosenDate + (val1 * 60),
timedifference = (maxrange - minrange)/60,
fulldate = new Date(minrange * 1000),
enddate = new Date(maxrange * 1000),
fdate = fulldate.getDate(),
fmonth = fulldate.getMonth()+1,
fyear = fulldate.getFullYear(),
edate = enddate.getDate(),
emonth = enddate.getMonth()+1,
eyear = enddate.getFullYear(),
fhours = fulldate.getHours(),
fminutes = fulldate.getMinutes(),
ehours = enddate.getHours(),
eminutes = enddate.getMinutes();
if (fmonth < 10) {
fmonth = "0" + fmonth; }
if (fdate < 10) {
fdate = "0" + fdate; }
if (fhours < 10) {
fhours = "0" + fhours; }
if (fminutes < 10) {
fminutes = "0" + fminutes; }
if (emonth < 10) {
emonth = "0" + emonth; }
if (edate < 10) {
edate = "0" + edate; }
if (ehours < 10) {
ehours = "0" + ehours; }
if (eminutes < 10) {
eminutes = "0" + eminutes; }
$("#final-from-value").text(fdate + '/' + fmonth + '/' + fyear + ' ' + fhours + ':' + fminutes);
$("#final-to-value").text(edate + '/' + emonth + '/' + eyear + ' ' + ehours + ':' + eminutes);
$("#hidden-start").val(minrange);
$("#hidden-finish").val(maxrange);
clearLocations();
searchMap();
}
我还有一个datetimepicker,它也可以在更改时调用该函数:
<input id="datepicker" name="date" type="text" onchange="slideTime(); searchMap(); clearLocations()" />
我已经尝试了所有可以想到的方法,但是老实说我不知道该怎么做。谢谢你的帮助。并且让我知道您还有其他需要看的地方。
更新
如果我不调用searchMap()函数,就没有错误,但是如果在清除位置后调用searchMap(),它将不喜欢它。
这是searchMap()的代码
function searchMap() {
//clearLocations();
var getstart = $("#hidden-start").val();
var getfinish = $("#hidden-finish").val();
// Change this depending on the name of your PHP file
downloadUrl("php/xml.php?start=" + getstart + "&finish=" + getfinish, function(data) {
var xml = data.responseXML;
markers = xml.documentElement.getElementsByTagName("marker");
for (var i = 0; i < markers.length; i++) {
var name = markers[i].getAttribute("title");
var address = markers[i].getAttribute("url");
var priority = parseInt(markers[i].getAttribute("priority"));
var point = new google.maps.LatLng(
parseFloat(markers[i].getAttribute("lat")),
parseFloat(markers[i].getAttribute("lng")));
var html = "<b>" + name + "</b> <br/>" + address;
//var icon = customIcons[type] || {};
var marker = new google.maps.Marker({
map: map,
position: point,
zIndex: priority,
icon: iconType[priority],
});
bindInfoWindow(marker, map, infoWindow, html);
}
});
}
最佳答案
我不是您的markers
变量中的内容,但是请尝试以下操作:
for (i in markers) {
if(!markers.hasOwnProperty(i)){continue;}
markers[i].setMap(null);
}
关于jquery - 尝试清除 map 时,对象#<Element>没有方法(Google Maps API V3),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/14342037/