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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T21:53:57+00:00 2026-05-26T21:53:57+00:00

I have a Google Map with some simple buttons added to pan to different

  • 0

I have a Google Map with some simple buttons added to pan to different locations.

<input id="loc0" type="button" value="Cape Sable (6)">
<input id="loc1" type="button" value="Florida Bay (3)">

Originally, I was using this code to make the buttons work:

var locations = [
    new google.maps.LatLng(25.171, -81.060),
    new google.maps.LatLng(25.211, -80.650) 
];


$('#loc0').click(function(){
    map.panTo(locations[0]);
    map.setZoom(12);
});

$('#loc1').click(function(){
    map.panTo(locations[1]);
    map.setZoom(12);
});

This works fine, but I would love to write the click function as a loop, so I don’t have to keep using redundant code. But I can’t seem to figure it out.

I have tried the following:

for (i = 0; i < locations.length; i++) {
    $('#loc'+i).click(function() {
        map.panTo(locations[i]);
        map.setZoom(12);
    });

}

But I get an error when trying to use the buttons: panTo: latLng must be of type LatLng (122 out of range 43). I’m not well-versed with jQuery, so I suspect this is something simple I don’t understand yet. Any hints?

  • 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-26T21:53:58+00:00Added an answer on May 26, 2026 at 9:53 pm

    Nothing immediately pops out as being wrong, this looks like it should be correct.

    Can you put up a JS Fiddle demonstrating the problem at all?

    My first hunch would be that your code is correct but somehow you are getting invalid data in the locations array.

    If that’s not the case, I wonder if it’s an issue with enclosure and the variables. Something like this may help:

    for (var i = 0; i < locations.length; i++) {
        var location = locations[i];
        $('#loc'+i).click(function() {
            map.panTo(location);
            map.setZoom(12);
        });
    }
    

    I first made the declaration of the “i” variable specific to each loop, so there’s no way that it can get overwritten improperly somewhere else. I also added the additional “fail safe” of defining the “location” variable in each iteration of the loop so that you can ensure the correct variable makes it to the click function.

    EDIT
    Definitely an issue with enclosure and the variable content when accessed via click. Something else you could try is this:

    1: Add a common class to each button, something like class=”locButton”

    2: Try this:

    $(".locButton").each(function(){
        $(this).click(function(){
            var locId = parseInt($(this).attr('id').replace('loc',''));
            map.panTo(locations[locId]);
            map.setZoom(12);
        });
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a MapActivity. It shows a Google map with some markers on it.
I have some problem to get lat/lng from google map in my application. When
I have a toolbar that manipulates a Google map. Some of the functions affect
I have some google map markers that sit on a map. They are controlled
I have implemented Simple Google Map Direction Api Javascript in my webpage. When my
I have a Google Map that suddenly stopped working for no apparent reason (I
I have a scenario where I am using Google Map JavaScript API to process
I have an App which launches the google Map App. The code is: UIApplication
I have a view containing a UIWebView which is loading a google map (so
I have a dynamic map in Google Maps (API v2) I want to overlay

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.