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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T07:00:01+00:00 2026-05-29T07:00:01+00:00

Ok so i have a map with some markers on it. There is a

  • 0

Ok so i have a map with some markers on it. There is a click event on the map that should get the closest marker however when the find_closest_marker(event) function runs it breaks on the map.markers.length saying that its undefined.

Here is a link to what im trying to http://jsfiddle.net/VyzFq/7/

Can anyone can please show me where I’m going wrong here.

  • 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-29T07:00:03+00:00Added an answer on May 29, 2026 at 7:00 am

    Looks like there are a couple of problems:

    1) the map.markers array hasn’t been declared. declare it above the for loop, and push the markers onto the markers array.

    ...
    var infowindow = new google.maps.InfoWindow();
    
    var marker, i;
    map.markers = []; // ADD THIS LINE
    for (i = 0; i < locations.length; i++) {  
        marker = new google.maps.Marker({
            position: new google.maps.LatLng(locations[i][1], locations[i][2]),
            map: map
        });
        map.markers.push(marker); // ADD THIS LINE
    ...
    

    2) the find_closest_point function can’t see the map variable. move the find_closest_point function to the scope where you are adding the click event listener:

    // CHANGE THIS LINE
    google.maps.event.addListener(map, 'click', find_closest_marker);
    
    // TO:
    
    google.maps.event.addListener(map, 'click', function( event ) {
        var lat = event.latLng.lat();
        var lng = event.latLng.lng();
        var R = 6371;
        var distances = [];
        var closest = -1;
        for( i=0;i<map.markers.length; i++ ) {
            var mlat = map.markers[i].position.lat();
            var mlng = map.markers[i].position.lng();
            var dLat  = rad(mlat - lat);
            var dLong = rad(mlng - lng);
            var a = Math.sin(dLat/2) * Math.sin(dLat/2) +
                Math.cos(rad(lat)) * Math.cos(rad(lat)) * Math.sin(dLong/2) * Math.sin(dLong/2);
            var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
            var d = R * c;
            distances[i] = d;
            if ( closest == -1 || d < distances[closest] ) {
                closest = i;
            }
        }
    
        alert(map.markers[closest].title);
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some google map markers that sit on a map. They are controlled
I have an XML file that I would like to map some attributes of
I have some questions about Perl's map function. Specifically: How does %hash = map
hoping to get some help here. I have locations on a database that I
I have a MapActivity. It shows a Google map with some markers on it.
After some advice on how to clear markers on googlemaps, I have a map
I have a Google Map on our site that has a list of markers
I have a toolbar that manipulates a Google map. Some of the functions affect
I have an application showing the map and some markers on it. I use
I have a Google map that is showing a number of markers. When the

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.