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

  • SEARCH
  • Home
  • 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 8945941
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T12:22:57+00:00 2026-06-15T12:22:57+00:00

The following code grabs some JSON data from /home.json URL which contain 5 microposts

  • 0

The following code grabs some JSON data from /home.json URL which contain 5 microposts by 6 users. However, only the last 4 markers are shown (not even the 5th !!). I have spend 2 days to find the bug but unfortunately I can’t really get why this dosen’t work. If anyone could help me I would really appreciate it!

I have tested all lon/Lat variables through alert! All of them have the appropriate data. The map should be ok since the last 4 are shown !! Most probably the problem lies with my closure definition but I really cannot understand what I am doing wrong..

var GMAPS = window.GMAPS || {};
GMAPS.mainMap = function() {
  var map;
  var infowindow = new google.maps.InfoWindow();
  var jsonObject = {};

  function addMarkers() {
    var xhr = new XMLHttpRequest();
    xhr.open( "GET", "/home.json", true );
    xhr.onreadystatechange = function () {
      if ( xhr.readyState == 4 && xhr.status == 200 ) {
        jsonObject = JSON.parse( xhr.responseText );
        for (var i=0; i<jsonObject.length; i++) {
          for (var j=0; j<jsonObject[i].microposts.length; j++) {
            (function(Name, Title, Content, Latitude, Longitude) {
              return function() {
                //alert(Name + "\n" + Title + "\n" + Content + "\n" + Latitude + "\n" + Longitude + "\n");  //<-- this works!
                var position = new google.maps.LatLng(Latitude, Longitude);
                var contentString = "<h4>" + Name.bold() + "</h4>" + "<br />" + Title.bold()
                                + "<br />" + Content;

                var marker = new google.maps.Marker({
                  position: position,
                  title: Title,
                  map: map
                });
                google.maps.event.addListener(marker, 'click', (function(marker, contentString) { 
                    return function() { 
                      infowindow.setContent(contentString); 
                      infowindow.open(map, marker); 
                    } 
                })(marker, contentString));
              };
            })(jsonObject[i].name, jsonObject[i].microposts[j].title,
               jsonObject[i].microposts[j].content,
               jsonObject[i].microposts[j].lat,
               jsonObject[i].microposts[j].lon)();
          }
        }
      }
    };
    xhr.send(null);
  }

  function createMap() {
    map = new google.maps.Map(document.getElementById('main_map'), {
      zoom: 10,
      panControl: true,
      zoomControl: true,
      zoomControlOptions: {
        style: google.maps.ZoomControlStyle.SMALL
      },
      mapTypeControl: true,
      scaleControl: true,
      streetViewControl: true,
      overviewMapControl: true,
      scrollwheel: false,
      center: new google.maps.LatLng(37.975327,23.728701),
      mapTypeId: google.maps.MapTypeId.ROADMAP
    });
  }


  function initAddress() {
    if (navigator.geolocation) {
      navigator.geolocation.getCurrentPosition(showPosition);
    }
    else {
      var position = new google.maps.LatLng(0, 0);
      map.setCenter(position);
    }
  }

  function showPosition(position) {
    var position = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
    map.setCenter(position);
  }

  return {

    initMap : function() {
      createMap();
      initAddress();
      addMarkers();
    }
  };
}();

document.addEventListener("DOMContentLoaded", GMAPS.mainMap.initMap, false);
  • 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-15T12:22:58+00:00Added an answer on June 15, 2026 at 12:22 pm

    ** Note: you have an unnecessary extra set of parens at the end of your IIFE.

    Before your IIFE (inside the innermost for-loop), try adding:

    var jObj = jsonObject[i], mPost = jObj.microposts[j];
    

    Then in replace the arguments to the IIFE with:

    (function(Name, Title, Content, Latitude, Longitude) {
                  return function() {
                    //alert(Name + "\n" + Title + "\n" + Content + "\n" + Latitude + "\n" + Longitude + "\n");  //<-- this works!
                    var position = new google.maps.LatLng(Latitude, Longitude);
                    var contentString = "<h4>" + Name.bold() + "</h4>" + "<br />" + Title.bold()
                                    + "<br />" + Content;
    
                    var marker = new google.maps.Marker({
                      position: position,
                      title: Title,
                      map: map
                    });
                    google.maps.event.addListener(marker, 'click', (function(marker, contentString) { 
                        return function() { 
                          infowindow.setContent(contentString); 
                          infowindow.open(map, marker); 
                        } 
                    })(marker, contentString));
                  };
                })(jObj.name, mPost.title, mPost.content, mPost.lat, mPost.lon);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Please refer to the following code, which grabs some information from a PHP script,
I am working with some code in PHP that grabs the referrer data from
I have an application which grabs some data with the following sql: SELECT Sum(fieldname)
Following code iterates through many data-rows, calcs some score per row and then sorts
I have to work through some code and found the following function, which I
I've got the following code in my Services project, which is trying to grab
The following GridView with an EntityDataSource that grabs 3 fields from the Surveyors table
I know the following MySQL code is not correct can some help me fix
I have the following code that grabs all controllers, sorts it, and outputs in
I am writing some code to parse an xml file of the following format

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.