2

我知道这是一个常见问题,但我已经盯着,阅读谷歌并盯着更多,但我看不出有什么问题。

我从 MySql 表中填充我的地图,对英国邮政编码进行反向地理编码,在结果周围画一个圆圈,但我无法理解为什么我的闭包函数不起作用。

据我了解,闭包原则是关于从我试图用函数完成的循环范围之外访问变量 - 但对于我的一生,我无法让它工作。

我相信有人会为我的明显错误找到答案,我将不胜感激任何人可以提供的任何帮助。

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);
            }

        });

    }

});
4

1 回答 1

0

我将函数闭包与标记(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);

    }

});
于 2012-09-05T02:03:53.003 回答