Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8512449
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T04:17:02+00:00 2026-06-11T04:17:02+00:00

I know this is a common problem but I have stared, read googled and

  • 0

I know this is a common problem but I have stared, read googled and stared some more but I cannot see what is wrong.

I populate my maps from a MySql Table, reverse geocode the UK Postcode, draw a circle around the result but I cannot get my head around why my closure function isn’t working.

As I understand it the closure principle is about accessing variable from outside the scope of the loop which I am trying to accomplish with the function – but for the life of me I cannot get it to work.

I’m sure someone will have the answer to my glaring error and I would appreciate any help anyone can offer.

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

        });

    }

});
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T04:17:04+00:00Added an answer on June 11, 2026 at 4:17 am

    I use function closure with functions for the marker (createMarker) and the geocoder (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);
    
        }
    
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know this has been a very common and existing problem, but I have
I know that this is very common problem and I have gone through almost
I know this is the most common problem in the history of programming but
I know this must be a pretty common problem, but I haven't been able
i know this sounds really common and so trivial but , am having a
I know this is a common error, but bear with me. I've pursued the
I know this is an old question, but I have spend any hours on
I know this is a common question but I can't find any answer. I
I stumbled across this before and I know it is a common problem. One
I know this question may be a little bit common and over asked but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.