我知道这是一个普遍的问题,但是我凝视,阅读谷歌并凝视了更多,但我看不出有什么问题.
我从MySql表中填充地图,对UK邮政编码进行反向地理编码,在结果周围画一个圆,但是我无法理解为什么我的闭包功能不起作用.
据我了解,闭包原理是关于从循环范围之外访问变量,而这个范围是我试图用函数来完成的,但是对于我而言,我无法使其正常工作.
我确信有人会为我的明显错误提供答案,并且我会很感激任何人都可以提供的帮助.
var mapOptions = {
zoom : 8,
center : new google.maps.LatLng(37.09024, -95.712891),
mapTypeId : google.maps.MapTypeId.ROADMAP
};
var infowindow = new google.maps.InfoWindow({
content : contentString
});
var geocoder = new google.maps.Geocoder();
var map = new google.maps.Map(document.getElementById('map_canvas'), mapOptions);
var contentString;
var i
downloadUrl("getXML.php", function(data) {
var xml = data.responseXML;
var markers = xml.documentElement.getElementsByTagName("marker");
for (i = 0; i < markers.length; i++) {
var name = markers[i].getAttribute("name");
contentString =(name);
var postcode = markers[i].getAttribute("postcode");
var dateadded = markers[i].getAttribute("dateadded");
var extra = markers[i].getAttribute("extra");
geocoder.geocode({
'address' : postcode
}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
map.setCenter(results[0].geometry.location);
var marker = new google.maps.Marker({
map : map,
clickable : true,
position : results[0].geometry.location
})
google.maps.event.addListener(marker, 'click', (function(marker, i) {
return function() {
infowindow.setContent(name );
infowindow.open(map, marker);
}
})(marker, i));
var circle = new google.maps.Circle({
map : map,
radius : 12070.99, // 15 miles in metres
fillColor : '#AA0000'
});
circle.bindTo('center', marker, 'position');
} else {
alert('Geocode was not successful for the following reason: ' + status);
}
});
}
});
解决方法:
我对标记(createMarker)和地址解析器(geocodeMarker)使用函数闭包
var mapOptions = {
zoom : 8,
center : new google.maps.LatLng(37.09024, -95.712891),
mapTypeId : google.maps.MapTypeId.ROADMAP
};
var infowindow = new google.maps.InfoWindow({
content : contentString
});
var geocoder = new google.maps.Geocoder();
var map = new google.maps.Map(document.getElementById('map_canvas'), mapOptions);
var contentString;
var i;
function createMarker(latlng, name, html)
{
var marker = new google.maps.Marker({
map : map,
clickable : true,
position : latlng
})
google.maps.event.addListener(marker, 'click', function() {
infowindow.setContent(name );
infowindow.open(map, marker);
});
var circle = new google.maps.Circle({
map : map,
radius : 12070.99, // 15 miles in metres
fillColor : '#AA0000'
});
circle.bindTo('center', marker, 'position');
}
function geocodeMarker(postcode, name, html)
{
geocoder.geocode({
'address' : postcode
}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
map.setCenter(results[0].geometry.location);
createMarker(results[0].geometry.location, html);
} else {
alert('Geocode was not successful for the following reason: ' + status);
}
});
}
downloadUrl("getXML.php", function(data) {
var xml = xmlParse(data);
var markers = xml.documentElement.getElementsByTagName("marker");
for (i = 0; i < markers.length; i++) {
var name = markers[i].getAttribute("name");
contentString =(name);
var postcode = markers[i].getAttribute("postcode");
var dateadded = markers[i].getAttribute("dateadded");
var extra = markers[i].getAttribute("extra");
geocodeMarker(postcode, name, contentString);
}
});
标签:javascript,google-maps 来源: https://codeday.me/bug/20191011/1891020.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。