Adding ID's to google map markers

I have a script that loops and adds markers one at a time.

I am trying to get the current marker to have an info window and and only have 5 markers on a map at a time (4 without info windows and 1 with)

How would I add an id to each marker so that I can delete and close info windows as needed.

This is the function I am using to set the marker:

function codeAddress(address, contentString) {

var infowindow = new google.maps.InfoWindow({
  content: contentString

if (geocoder) {

  geocoder.geocode( { 'address': address}, function(results, status) {

    if (status == google.maps.GeocoderStatus.OK) {


       var marker = new google.maps.Marker({
          map: map, 
          position: results[0].geometry.location

      } else {
       alert("Geocode was not successful for the following reason: " + status);


JavaScript is a dynamic language. You could just add it to the object itself.

var marker = new google.maps.Marker(markerOptions);
marker.metadata = {type: "point", id: 1};

Also, because all v3 objects extend MVCObject(). You can use:

marker.setValues({type: "point", id: 1});
// or
marker.set("type", "point");
marker.set("id", 1);
var val = marker.get("id");

HTML Global id Attribute, Try this: $('element').attr('id', 'value');. So it becomes; $(function() { $('span . breadcrumb').each(function(){ $('#nav').attr('id', $(this).text());� Your Apple ID balance includes the balance from any Apple Gift Cards or App Store & iTunes Gift Cards that you have redeemed. If you have a gift card, learn what type of gift card you have and how to redeem it. Apple ID balance isn’t available in all countries or regions. Check if you can pay with your Apple ID balance in your country or region.

Just adding another solution that works for me.. You can simply append it in the marker options:

var marker = new google.maps.Marker({
    map: map, 
    position: position,

    // Custom Attributes / Data / Key-Values
    store_id: id,
    store_address: address,
    store_type: type

And then retrieve them with:


jQuery - Add ID instead of Class, Here's a two-second review. If an element has an ID, you can link to it with natural browser behavior. It's great if headings have them, because� An Apple ID is basically an account that lets you download and install apps and games from the App Store, purchase music, movies, and books from iTunes, sync Calendars, Reminders, and Contacts through iCloud, and use FaceTime and iMessages in the Messages apps.

I have a simple Location class that I use to handle all of my marker-related things. I'll paste my code below for you to take a gander at.

The last line(s) is what actually creates the marker objects. It loops through some JSON of my locations, which look something like this:

{"locationID":"98","name":"Bergqvist Järn","note":null,"type":"retail","address":"Smidesvägen 3","zipcode":"69633","city":"Askersund","country":"Sverige","phone":"0583-120 35","fax":null,"email":null,"url":"","lat":"58.891079","lng":"14.917371","contact":null,"rating":"0","distance":"45.666885421019"}

Here is the code:

If you look at the target() method in my Location class, you'll see that I keep references to the infowindow's and can simply open() and close() them because of a reference.

See a live demo: (type in a Swedish city, like stockholm, and hit enter)

var Location = function() {
    var self = this,
        args = arguments;

    self.init.apply(self, args);

Location.prototype = {
    init: function(location, map) {
        var self = this;

        for (f in location) { self[f] = location[f]; } = map; = self.locationID;

        var ratings = ['bronze', 'silver', 'gold'],
            random = Math.floor(3*Math.random());

        self.rating_class = 'blue';

        // this is the marker point
        self.point = new google.maps.LatLng(parseFloat(, parseFloat(self.lng));

        // Create the marker for placement on the map
        self.marker = new google.maps.Marker({
            position: self.point,
            icon: new google.maps.MarkerImage('/wp-content/themes/arbesko/img/locator/'+self.rating_class+'SmallMarker.png'),
            shadow: new google.maps.MarkerImage(
                                        new google.maps.Size(52, 18),
                                        new google.maps.Point(0, 0),
                                        new google.maps.Point(19, 14)

        google.maps.event.addListener(self.marker, 'click', function() {

        google.maps.event.addListener(self.marker, 'mouseover', function() {

        google.maps.event.addListener(self.marker, 'mouseout', function() {

        var infocontent = Array(
            '<div class="locationInfo">',
                '<span class="locName br">''</span>',
                '<span class="locAddress br">',
                    self.address+'<br/>'+self.zipcode+' '' ',
                '<span class="locContact br">'

        if ( {
            infocontent.push('<span class="item br locPhone">''</span>');

        if (self.url) {
            infocontent.push('<span class="item br locURL"><a href="http://'+self.url+'">'+self.url+'</a></span>');

        if ( {
            infocontent.push('<span class="item br locEmail"><a href="mailto:''">Email</a></span>');

        // Add in the lat/long

        infocontent.push('<span class="item br locPosition"><strong>Lat:</strong> ''<br/><strong>Lng:</strong> '+self.lng+'</span>');

        // Create the infowindow for placement on the map, when a marker is clicked
        self.infowindow = new google.maps.InfoWindow({
            content: infocontent.join(""),
            position: self.point,
            pixelOffset: new google.maps.Size(0, -15) // Offset the infowindow by 15px to the top


    // Append the marker to the map
    addToMap: function() {
        var self = this;


    // Creates a sidebar module for the item, connected to the marker, etc..
    sidebarItem: function() {
        var self = this;

        if (self.sidebar) {
            return self.sidebar;

        var li = $('<li/>').attr({ 'class': 'location', 'id': 'location-' }),
            name = $('<span/>').attr('class', 'locationName').html(,
            address = $('<span/>').attr('class', 'locationAddress').html(self.address+' <br/> '+self.zipcode+' '' ';


        li.bind('click', function(event) {

        self.sidebar = li;

        return li;

    // This will "target" the store. Center the map and zoom on it, as well as 
    target: function(type) {
        var self = this;

        if (locator.targeted) {

        locator.targeted = this;

        if (type != 'map') {

        // Open the infowinfow;

for (var i=0; i < locations.length; i++) {
    var location = new Location(locations[i],;

    // Add the sidebar item

    // Add the map!

On Adding IDs to Headings, )Returns: jQuery. Description: Create a new jQuery object with elements added to the set of matched elements. <span id="a">Hello Again</span>. <script>. The id attribute provides a unique identifier for an element within the document. It may be used by an a element to create a hyperlink to this particular element.

Why not use an cache that stores each marker object and references an ID?

var markerCache= {};
var idGen= 0;

function codeAddress(addr, contentStr){
    // create marker
    // store
    markerCache[idGen++]= marker;

Edit: of course this relies on a numeric index system that doesn't offer a length property like an array. You could of course prototype the Object object and create a length, etc for just such a thing. OTOH, generating a unique ID value (MD5, etc) of each address might be the way to go.

.add(), [This thread is closed.] Is it possible to add ids to section blocks? If not, is adding custom IDs to section blocks a planned update to the plugin? I have the following JavaScript that creates a div and then appends it to the body and then inserts some dynamically generated HTML into it. cartDiv = document.createElement('div'); This div I would like to add an id and/or a class to it.

Marker already has unique id


Adding ID to section block?, In this article, we're going to explain how to add Classes and IDs to Elements. About the Structure of IDs and Classes; Adding Classes� Your Apple ID is the account you use for all Apple services.

Elements Class and ID Structure, Click on the Arc Toolbox icon ( ). Click the plus sign next to Data Management Tools and then Distributed Geodatabase. Select Add Global IDs to open the tool. Allowing trusted organizations to add information to your record ensures the data connected with your ORCID iD is authoritative and trustworthy, as well as saving you time entering information manually. The organization which added the work to your record will be listed as the source of the item.

Adding Global IDs to GIS Data, To add it to your reports, you must create a custom fields duplicating the IDs. This custom field reveal the hidden ID field so it can be pulled into a� When adding insurance information for your client, the most important piece of information to verify will be the Payer ID to make sure you've selected the appropriate insurance payer. This guide walks through everything to keep in mind when searching for an insurer, and the steps you can take if the payer you need seems to be missing.

Adding a Lead ID or Contact ID and Owner ID in a Salesforce Report , Try adding ID in h2 error. I keep getting a error that tells me that my ID is not on the h2 but it is there. Any help? Here is my html code. <!DOCTYPE html> <html>� Adding MFA to My Account Follow New articles New articles and comments. What is multi-factor authentication (MFA)? What happens when I select ‘Text Message or

  • I am curious how, once you have given markers id's, how you woud go about accessing these markers outside of the map canvas. $('#1').doSomething(); for example?
  • This helped a bunch. None of the examples had this. Good for passing values for consumption with click return functions.
  • That does not answer the question. At all.
  • The only solution I would use out of these.
  • The double underscore and gm prefix should indicate to you that this is a private variable that has not guarantees. Avoid it.