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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T01:13:50+00:00 2026-06-03T01:13:50+00:00

Im probably doing something dumb here but I can’t figure out why both of

  • 0

Im probably doing something dumb here but I can’t figure out why both of my markers end up with the same title. Both markers end up with the correct location from the geocoder and are displayed in separate places.

Before render

function gMapInitialize() {

  var myLocs = [<ui:repeat value = "#{LocationBean.locations.address}" var = "loc"       varStatus = "loop">
                   [ "#{loc.name}","#{loc.street}","#{loc.city}","#{loc.state}", "#{loc.zipCode}"]#{!loop.last ? ',' : ''}
                   </ui:repeat>];
   //<![CDATA[                
     var myOptions = {
      center: new google.maps.LatLng(37.212832,-76.750488),
      zoom: 8,
      mapTypeId: google.maps.MapTypeId.HYBRID
    };

    var map = new google.maps.Map(document.getElementById("map_canvas"),
        myOptions);

    var geocoder = new google.maps.Geocoder();

    for(var i = 0; i < myLocs.length; i++){
    var myLoc = myLocs[i];
    var geoOptions = {
    address: myLoc[1] + "," + myLoc[2] + "," + myLoc[3] + "," + myLoc[4],

    }
    geocoder.geocode( geoOptions ,function(results, status){

    if(status == google.maps.GeocoderStatus.OK){
    var marker = new google.maps.Marker({
    map: map,
    position: results[0].geometry.location,
    title: myLoc[0],
    zIndex: i
    });


    }
    });               


  }
  }
 // ]]>

After Render:

function gMapInitialize() {

  var myLocs = [
                   [ "Richmond","9 North 3rd Street","Richmond","VA", "23219"],

                   [ "Hampton Roads","632 North Witchduck Road","Virginia Beach","VA", "23462"]
                   ];
   //<![CDATA[                
     var myOptions = {
      center: new google.maps.LatLng(37.212832,-76.750488),
      zoom: 8,
      mapTypeId: google.maps.MapTypeId.HYBRID
    };

    var map = new google.maps.Map(document.getElementById("map_canvas"),
        myOptions);

    var geocoder = new google.maps.Geocoder();

    for(var i = 0; i < myLocs.length; i++){
    var myLoc = myLocs[i];
    var geoOptions = {
    address: myLoc[1] + "," + myLoc[2] + "," + myLoc[3] + "," + myLoc[4],

    }
    geocoder.geocode( geoOptions ,function(results, status){

    if(status == google.maps.GeocoderStatus.OK){
    var marker = new google.maps.Marker({
    map: map,
    position: results[0].geometry.location,
    title: myLoc[0],
    zIndex: i
    });


    }
    });               


  }
  }
 // ]]>

Am I trying to do too much in one loop? I keep hearing something about closure but am new to javascript and know nothing about it.

Ohh yeah: I get the second of the two names on each marker. The markers are in the right position but both are titled “Hampton Roads”. So, the right info is being passed to the title, but its as if title of the first is being overwritten with the title of the second.

UPDATE Well, I fixed it and I think I understand a bit more about closure. So mainly the loop keeps looping and by the time the function is called its already at the end. So even in the below, each marker will probably have the same zIndex and I should pass the “i” as well.

for(var i = 0; i < myLocs.length; i++){

    var myLoc = myLocs[i];
    var geoOptions = {
    address: myLoc[1] + "," + myLoc[2] + "," + myLoc[3] + "," + myLoc[4],

    }

    geocoder.geocode( geoOptions ,addMarkers(myLoc[0]));
    }

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


    }
    };



  }
  • 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-03T01:13:51+00:00Added an answer on June 3, 2026 at 1:13 am

    It’s a simple closure issue. myLoc[i] refers to the same entity after the loop ends in both cases. Just wrap it in a function to create a new scope:

       for(var i = 0; i < myLocs.length; i++){
        (function(num) {
        var myLoc = myLocs[num];
        var geoOptions = {
        address: myLoc[1] + "," + myLoc[2] + "," + myLoc[3] + "," + myLoc[4],
    
        }
        geocoder.geocode( geoOptions ,function(results, status){
    
        if(status == google.maps.GeocoderStatus.OK){
        var marker = new google.maps.Marker({
        map: map,
        position: results[0].geometry.location,
        title: myLoc[0],
        zIndex: num
        });
        }(i));
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm probably doing something stupid but I can't figure out what it is. The
I know this is probably really dumb, but I can't figure out why this
There is probably something dumb I am doing wrong here. public function get_scores($id) {
I am probably doing something very wrong and noobish but I can't seem to
I'm probably doing something very stupid but I can't get following regexp to work
I'm probably doing something stupid, but I can't spot it. I've installed Eclipse Helios
I know I am probably just doing something dumb wrong, but I need to
I'm probably doing something silly, but here it goes. I'm trying to get the
I'm probably doing something stupid but I just can't seem to get the permissions
I'm probably doing something realy stupid but I cant get this to work: var

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.