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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T17:29:53+00:00 2026-06-13T17:29:53+00:00

I want to populate a Google Map with markers from locations stored in a

  • 0

I want to populate a Google Map with markers from locations stored in a database and then allow the user to delete any of the markers by double clicking the marker to be removed. Using the code below populates the map but when any of the markers are double clicked the last marker is removed.

I assume this is happening because the double click event listener is being reassigned each time to the current marker but I can’t work out how to stop this happening.

I have a jsFiddle set up here http://jsfiddle.net/simonbarker/TA4HP/

var markers = {"locations": [{"name" : "UK 1", "date" : "Nov 1", "lat" : 51, "lng" : 0.164},
                               {"name" : "France 1", "date" : "Nov 2", "lat" : 50, "lng" : 0.164},
                               {"name" : "France 2", "date" : "Nov 2", "lat" : 49, "lng" : 0.164}]
                };

  var map;
  var center = new google.maps.LatLng(markers.locations[1].lat,markers.locations[1].lng);

  var mapOptions = {
    zoom: 8,
    center: center,
    mapTypeId: google.maps.MapTypeId.ROADMAP
  }

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

  createMarkers();


  //create markers by traversing throught the markers object
  function createMarkers(){

    var i = 0;
    while(markers.locations[i]){

      var marker = new google.maps.Marker({
          position: new google.maps.LatLng(markers.locations[i].lat, markers.locations[i].lng),
          animation: google.maps.Animation.DROP,
          map: map
      });

      //store marker in the locations object
      markers.locations[i].marker = marker;

      //add delete handler
      google.maps.event.addListener(marker, 'dblclick', function(event) {
        marker.setMap(null);
        delete markers.locations[i];
      });

      i++;

    }
  }​

Any help would be great

UPDATE:

I solved this by changing the delete handler function to

//add delete handler
          google.maps.event.addListener(marker, 'dblclick', function(event) {
            this.setMap(null);
            delete markers.locations[this.id];
          });

and adding an id to the marker when creating it

var marker = new google.maps.Marker({
              id: i, 
              position: new google.maps.LatLng(markers.locations[i].lat, markers.locations[i].lng),
              animation: google.maps.Animation.DROP,
              map: map
          });
  • 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-13T17:29:54+00:00Added an answer on June 13, 2026 at 5:29 pm

    You need to create your markers in a separate function to obtain closure

    Same answer as this

    Explanation here.

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

Sidebar

Related Questions

I want to populate my database with test data my user and profile models
I want to populate the infowindow of my google maps markers with dynamic content.
I'm using coldfusion to populate a google map - and I want different colored
I want to populate a table form a MYSQL database. I have defined the
I want to populate a drop down menu with a column from a MySQL
I want to populate database with Cyrillic text namespace :db do task :populate =>
I have a google map where the user can create a polyline. I have
I want to populate GridView below with images: <asp:GridView ID=GrdDynamic runat=server AutoGenerateColumns=False> <Columns> </Columns>
I want to populate a table, defined in layout xml file through the programmatic
I want to populate a select list such that it increments for each 5

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.