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 8542589
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T12:06:20+00:00 2026-06-11T12:06:20+00:00

I am trying to display multiple markers using JSON with geocoding. My code shows

  • 0

I am trying to display multiple markers using JSON with geocoding. My code shows all the markers at the correct place but does not show the Correct Infowindow on them or the Correct title. I know it has something to do with closures. But I do not understand it. Please help me out and kindly edit my code and explain why it needs to be edited?
HELP PLEASE!!

points = data.POINTS;
var geocoder;
//var map;

geocoder = new google.maps.Geocoder();
var latlng = new google.maps.LatLng(41.2324, - 98.4160);
var mapOptions = {
    zoom: 4,
    center: latlng,
    mapTypeId: google.maps.MapTypeId.ROADMAP
}
map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions);

var infoWindow = new google.maps.InfoWindow();

for (var i = 0, length = points.length; i < length; i++) {
    data = points[i],
    address = data[1] + ", " + data[2] + ", " + data[3] + ", " + data[4] + ", " + data[5];

    geocoder.geocode({
        'address': address
    }, function (results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
            map.setCenter(results[0].geometry.location);
            //Creating a marker and putting it on the map
            var marker = new google.maps.Marker({
                map: map,
                position: results[0].geometry.location,
                title: data[4],
                animation: google.maps.Animation.DROP
            });
            // Creating a closure to retain the correct data, notice how I pass the current data in the loop into the closure (marker, data)
            (function (marker, points) {
                // Attaching a click event to the current marker
                //console.log(data[6]);
                google.maps.event.addListener(marker, "click", function (e) {
                    infoWindow.setContent(data[6] + " " + data[0]);
                    infoWindow.open(map, marker);
                });
            })(marker, data);
        } 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-11T12:06:21+00:00Added an answer on June 11, 2026 at 12:06 pm

    The Answer is the following:
    What I needed was to call the add marker function outside the Loop. Thanks for all of your kind consideration of my problem. Hope my Solution will help someone else.

    points = data.POINTS;
    var geocoder;
    //var map;
    geocoder = new google.maps.Geocoder();
    var latlng = new google.maps.LatLng(41.2324, - 98.4160);
    var mapOptions = {
        zoom: 4,
        center: latlng,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    }
    map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions);
    infoWindow = new google.maps.InfoWindow();
    for (var i = 0; i < points.length; i++) {
        var myLoc = points[i];
        var geoOptions = {
            address: myLoc[1] + "," + myLoc[2] + "," + myLoc[3] + "," + myLoc[4] + "," + myLoc[5]
        }
        geocoder.geocode(geoOptions, addMarkers(myLoc[6]));
    }
    
    function addMarkers(myTitle) {
        return function (results, status) {
            if (status == google.maps.GeocoderStatus.OK) {
                var marker = new google.maps.Marker({
                    map: map,
                    position: results[0].geometry.location,
                    title: myTitle,
                    zIndex: i
                });
                (function (marker, myTitle) {
    
                    // Attaching a click event to the current marker
                    //console.log(data[6]);
                    google.maps.event.addListener(marker, "click", function (e) {
                        infoWindow.setContent(myTitle);
                        infoWindow.open(map, marker);
                    });
                })(marker, myTitle);
            }
        };
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to display multiple chart's on one screen but i am not
I am trying to display multiple li's under a ul. But by using the
I'm trying to display multiple, static jQuery Progress Bars on a single page, all
I'm trying to get multiple markers on a Google map using API V3. The
I am trying to display multiple icons to the Treeview item but it is
I am trying to display a custom dialog box multiple times using the following
I am trying to display several multiple, manually called image galleries using fancybox. I
I am trying to display multiple bing maps on the same page, with each
I am trying to display multiple Pushpin objects on an EntityLayer for Bing Maps.
I am trying to use getResources to display multiple resources withing one resource, including

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.