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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T18:14:35+00:00 2026-05-21T18:14:35+00:00

I want to form a circle around the marker when the marker is clicked!

  • 0

I want to form a circle around the marker when the marker is clicked!

So, Listener is added to marker and the function needs to act on the circle.

Here is my Code :

   for(var j=0;j<lat.length;j++)
            {
                var pos = new google.maps.LatLng(lat[j],lng[j]);
                var marker_options = {position: pos, map:map, draggable:false};
                marker[j] = new google.maps.Marker(marker_options);
                circle_option = {strokeColor:"#FF0000",strokeOpacity:0.8,strokeWeight:2,fillColor:"#FF0000",fillOpacity:0.35,map:null,center:pos,radius:500};
                circle[j] = new google.maps.Circle(circle_option);
                google.maps.event.addListener(marker[j], 'click', function() {circle[j].setMap(map);});  // Error : circle[j] is undefined
            }

Error: circle[j] is undefined ?? (On the event.addListener line !)

Why.. it should be defined there ?

How to do this is the right way ? Please help!

  • 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-05-21T18:14:35+00:00Added an answer on May 21, 2026 at 6:14 pm

    You have a closure problem with j. When your function is called, j will reference the last value that j had in the for loop. So, j will be lat.length which is larger than the size of circle. The solution is to force j to be evaluated when generating the callback function:

    function make_callback(circle, map) {
        return function() {
            circle.setMap(map);
        };
    }
    

    and then, in your loop:

    google.maps.event.addListener(marker[j], 'click', make_callback(circle[j], map));
    

    Wrapping the callback generation in a separate function will give you the value of circle[j] at the instant when you call make_callback rather than the value when the callback is called.

    j is a reference to a value, the value that it points at depends on when you ask j what its value is. When you bind a function like this:

    google.maps.event.addListener(marker[j], 'click', function() { something(j); });
    

    The anonymous function doesn’t ask j what its value is until the function is called, the function simply remembers that it is going to use j. When the callback function is executing, it will ask j for its current value and use that. This means two things:

    • All the callbacks that you bound in that loop will use the same value of j.
    • j will be lat.length as that’s the last value that j was assigned during the loop.

    By using the make_callback function to build the callbacks, we’re asking j for its value at the time that we’re binding the callback. This is just a standard trick to force j to be evaluated when it has the value we want.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to extract around 20 element types from some SVG documents to form
Here is the Aim of my Program. I Want around 5-6 Circles to be
I want to place many divs in a form of a circle. I will
I want a form where user can change password. I am able to encrypt
Im using jquery and want each form text input to appear one by one,
When a user clicks a submit button I want the form to be submitted.
I'm trying to create a splash screen using LWUIT. I want a form to
I want to change form attributes with JQuery. In other browsers it works fine,
I want a simple form with one text box and a submit button. For
I want that the form will not close by doing Alt + F4 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.