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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 19, 20262026-06-19T03:58:47+00:00 2026-06-19T03:58:47+00:00

I have a data which is being pulled in via JSON. It is pulling

  • 0

I have a data which is being pulled in via JSON. It is pulling in correctly and is positioning the markers where they are meant to be.
The issue I’m having is to do with the google.maps.event.addListener, all markers are show the same content in the info window, for some reason it does not seem to be looping through the data correctly.

Below is what I have, any thoughts on why this is not working correctly, thanks.

$.get('http://localhost/map/map/locations', function(result) {
    var locations = $.parseJSON(result);


    // Markers
    var markers = [];

    // Looping through the JSON data
    for (var i = 0, length = locations.length; i < length; i++) {

        var data = locations[i];

        // Creating a marker and putting it on the map
        var marker = new google.maps.Marker({
            position: new google.maps.LatLng(data.lat, data.long),
            map: map,
            title: data.title,
            icon: iconSrc[data.category]
        });
        markers.push(marker);


        google.maps.event.addListener(marker, 'click', (function(marker, i) {
            return function() {
                infoWindow.setContent("<div class='cont_box' id='" + data.entry_id + "'> <h2>" + data.title + "</h2> <div class='cont'><p><img src='" + data.infoImage + "' alt='' width='100' height='66' style='float:right;'>" + data.windowText + "</p></div><div style='clear:both;'></div><div class='link'><a href='" + data.moreLink + "' target='_blank'>" + data.moreText + "</a></div></div>");
                infoWindow.open(map, marker);
            }
        })(marker, i));

    }// END for loop

});

Update

After adding the data var into the context it works fine.

I now have to show and hide marker depending on their category. I think this maybe a similar issue. What is the best way to include these hide and show functions with the data var?

Functions:

    /** Shows all markers of a particular category, and ensures the checkbox is checked **/
    function show(category) {
        for (var i=0; i<locations.length; i++) {
            if (data.category == category) {
                markers[i].setVisible(true);
            }
        }
    }// END function show



    /** Hides all markers of a particular category, and ensures the checkbox is cleared **/
    function hide(category) {
        for (var i=0; i<locations.length; i++) {
            if (data.category == category) {
              markers[i].setVisible(false);
            }
        }
    }// END function hide

    /** Show or hide the categories initially **/
    show("financial_services");
    show("government");
    show("identity_access");
    show("machine_to_machine");
    show("telecommunications");
    show("transport");

    /** Action when checkbox is clicked **/
    $(".checkbox").click(function(){
        var cat = $(this).attr("value");

        // If checked
        if ( $(this).is(":checked") ){
            show(cat);
        }
        else {
            hide(cat);
        }
    });

This is how it currently sits with everything, it is withing the $.get call, but outside the for loop which has the data var in it:

    // JSON feed
$.get('http://localhost/map/map/locations', function(result) {
    var locations = $.parseJSON(result);


    // Markers
    var markers = [];

    // Looping through the JSON data
    for (var i = 0, length = locations.length; i < length; i++) {

        var data = locations[i];

        // Creating a marker and putting it on the map
        var marker = new google.maps.Marker({
            position: new google.maps.LatLng(data.lat, data.long),
            map: map,
            title: data.title,
            icon: iconSrc[data.category]
        });
        markers.push(marker);


        google.maps.event.addListener(marker, 'click', (function(marker, i, loc) {
            return function() {
                infoWindow.setContent("<div class='cont_box' id='" + loc.entry_id + "'> <h2>" + loc.title + "</h2> <div class='cont'><p><img src='" + loc.infoImage + "' alt='' width='100' height='66' style='float:right;'>" + loc.windowText + "</p></div><div style='clear:both;'></div><div class='link'><a href='" + loc.moreLink + "' target='_blank'>" + loc.moreText + "</a></div></div>");
                infoWindow.open(map, marker);
            }
        })(marker, i, data));

    }// END for loop


    /** Shows all markers of a particular category, and ensures the checkbox is checked **/
    function show(category) {
        for (var i=0; i<locations.length; i++) {
            if (data.category == category) {
                markers[i].setVisible(true);
            }
        }
    }// END function show



    /** Hides all markers of a particular category, and ensures the checkbox is cleared **/
    function hide(category) {
        for (var i=0; i<locations.length; i++) {
            if (data.category == category) {
              markers[i].setVisible(false);
            }
        }
    }// END function hide

    /** Show or hide the categories initially **/
    show("financial_services");
    show("government");
    show("identity_access");
    show("machine_to_machine");
    show("telecommunications");
    show("transport");

    /** Action when checkbox is clicked **/
    $(".checkbox").click(function(){
        var cat = $(this).attr("value");

        // If checked
        if ( $(this).is(":checked") ){
            show(cat);
        }
        else {
            hide(cat);
        }
    });
});
  • 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-19T03:58:49+00:00Added an answer on June 19, 2026 at 3:58 am

    Your addListener open a new context, so your data variable is not available. Add the data var in the context :

    google.maps.event.addListener(marker, 'click', (function(marker, i, myData) {
        return function() {
           infoWindow.setContent("<div class='cont_box' id='" + myData.entry_id + "'> <h2>");
           infoWindow.open(map, marker);
        }
    })(marker, i, data));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So basically I have data being pulled from a database for which I want
I have a question regarding the some data which is being transfered from one
I am getting JSON string from website. I have data which looks like this
I have data which is constantly being read by many threads. This data needs
I am building an Android application and I have JSON data which contains Unicode
I have the following code which pulls json data from an ASP.NET page and
I have some data being pulled in from an Entity model. This contains attributes
I have the following code, which compiles but doesn't bring back any data. Here
I have data which resembles the following: D.STEIN,DS,01,ALTRES,TTTTTTFFTT D.STEIN,DS,01,APCASH,TTTTTTFFTT D.STEIN,DS,01,APINH,TTTTTTFFTT D.STEIN,DS,01,APINV,TTTTTTFFTT D.STEIN,DS,01,APMISC,TTTTTTFFTT D.STEIN,DS,01,APPCHK,TTTTTTFFTT D.STEIN,DS,01,APWLNK,TTTTTTFFTT
I have data which resembles the following: D.STEIN,DS,01,ALTRES,TTTTTTFFTT D.STEIN,DS,01,APCASH,TTTTTTFFTT D.STEIN,DS,01,APINH,TTTTTTFFTT D.STEIN,DS,01,APINV,TTTTTTFFTT D.STEIN,DS,01,APMISC,TTTTTTFFTT D.STEIN,DS,01,APPCHK,TTTTTTFFTT D.STEIN,DS,01,APWLNK,TTTTTTFFTT

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.