need to access the index of leaflet marker

I have a map in leaflet that lets me add and delete points.

When deleting a point I console.log the index of the point marker.

However, what I want to do is when just clicking on a point to display its popup I want to also console.log its index  value in the marker list.

My reason for doing this is because later I will need to target this marker and its popup and change its value.

Thanks,

<!DOCTYPE html>
<html>
    <head>
        <meta name="description" content="Leaflet API - Adding a Marker" />
		<title>Leaflet API: Adding a Marker and Getting ID</title>
        <link rel="stylesheet" href="http://cdn.leafletjs.com/leaflet-0.7.3/leaflet.css" />
		<script src="http://cdn.leafletjs.com/leaflet-0.7.3/leaflet.js"></script>
		
<style>
#map { 
	height: 400px; 
	width: 400px;
	margin: 0; 
	padding: 0; 
}
	
.add_marker_control {
	background-image: url(https://jsbin-user-assets.s3.amazonaws.com/cjseeger/add_marker.png);
	width: 20px;
    height: 20px;
    background-position: 50% 50%;
    background-repeat: no-repeat;
    display: block;
    padding: 3px;
    margin-bottom: 5px;
    border-radius: 4px;
    -webkit-border-radius: 4px;
    -moz-border-radius: 4px;
    box-shadow: 0 1px 7px rgba(0, 0, 0, 0.65);
    cursor: auto;
    text-align: center;
    background-color: #ffffff;
}

.add_marker_control:hover {
	background-color: #f4f4f4;
}

.del_marker_control {
	background-image: url(https://jsbin-user-assets.s3.amazonaws.com/cjseeger/del_marker.png);
	width: 20px;
    height: 20px;
    background-position: 50% 50%;
    background-repeat: no-repeat;
    display: block;
    padding: 3px;
    border-radius: 4px;
    -webkit-border-radius: 4px;
    -moz-border-radius: 4px;
    box-shadow: 0 1px 7px rgba(0, 0, 0, 0.65);
    cursor: auto;
    text-align: center;
    background-color: #ffffff;
}

.del_marker_control:hover {
	background-color: #f4f4f4;
}
</style>
		
</head>
    
<body>
<div id="map"></div>
		
<script >
L.Control.SimpleMarkers = L.Control.extend({
    options: {
        position: 'topleft'
    },
    
    onAdd: function () {
        var marker_container = L.DomUtil.create('div', 'marker_controls');
        var add_marker_div = L.DomUtil.create('div', 'add_marker_control', marker_container);
        var del_marker_div = L.DomUtil.create('div', 'del_marker_control', marker_container);
        add_marker_div.title = 'Add a marker';
        del_marker_div.title = 'Delete a marker';
        
        L.DomEvent.addListener(add_marker_div, 'click', L.DomEvent.stopPropagation)
            .addListener(add_marker_div, 'click', L.DomEvent.preventDefault)
            .addListener(add_marker_div, 'click', (function () { this.enterAddMarkerMode(); }).bind(this));
     
        L.DomEvent.addListener(del_marker_div, 'click', L.DomEvent.stopPropagation)
            .addListener(del_marker_div, 'click', L.DomEvent.preventDefault)
            .addListener(del_marker_div, 'click', (function () { this.enterDelMarkerMode(); }).bind(this));
        
        return marker_container;
    },
    
    enterAddMarkerMode: function () {
        if (markerList !== '') {
            for (var marker = 0; marker < markerList.length; marker++) {
                if (typeof(markerList[marker]) !== 'undefined') {
                    markerList[marker].removeEventListener('click', this.onMarkerClickDelete);
                } 
            }
        }
        document.getElementById('map').style.cursor = 'crosshair';
        map.addEventListener('click', this.onMapClickAddMarker);
    },
    
    enterDelMarkerMode: function () {
        for (var marker = 0; marker < markerList.length; marker++) {
            if (typeof(markerList[marker]) !== 'undefined') {
                markerList[marker].addEventListener('click', this.onMarkerClickDelete);
            }
        }
    },
    
    onMapClickAddMarker: function (e) {
        map.removeEventListener('click'); 
        document.getElementById('map').style.cursor = 'auto';
        
        var popupContent =  "You clicked on the map at " + e.latlng.toString();
        var the_popup = L.popup({maxWidth: 160, closeButton: true});
        the_popup.setContent(popupContent);
		
   
        var marker = L.marker(e.latlng);
        marker.addTo(map);
        marker.bindPopup(the_popup).openPopup();
        markerList.push(marker);
		
			
      
        console.log("Total Markers Created: "+markerList.length);
		
		//I want to put in the console the index of this marker when it is later clicked
        
        return false;    
    },

    onMarkerClickDelete: function (e) {
        map.removeLayer(this);
        var marker_index = markerList.indexOf(this);
		console.log("deleted marker_index: "+marker_index);
        delete markerList[marker_index];
        
        for (var marker = 0; marker < markerList.length; marker++) {
            if (typeof(markerList[marker]) !== 'undefined') {
                markerList[marker].removeEventListener('click', arguments.callee);
            } 
        }
        return false;  
    }
});

var markerList = [];



/////////////Map///////////

var map = L.map('map').setView([43, -93], 10);

		  // add an OpenStreetMap tile layer
		  L.tileLayer('http://{s}.tile.osm.org/{z}/{x}/{y}.png', {
		      attribution: 'The map contributors',
		      maxZoom: 18
		  }).addTo(map);


// Create the marker controls
        var marker_controls = new L.Control.SimpleMarkers();
        map.addControl(marker_controls);
</script>
		
</body>
</html>

Open in new window

lvmllcAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

lvmllcAuthor Commented:
Anybody have any ideas on this?

All I am really looking for is how to get the index value of the marker  when clicked to appear in the console.log
0
Slick812Commented:
greetings  lvmllc, , not sure I really understand the overall purpose of your viewing the Array Index in the console? BUT just for your Informations, when you do -
  markerList.push(marker);
The Index for the last added "marker" is the length of the Array (markerList) Minus One as in

marker.addTo(map);
 marker.bindPopup(the_popup).openPopup();
 markerList.push(marker);
 console.log("Total Markers Created: "+markerList.length+", Index of last marker: "+(markerList.length - 1));

Open in new window

But just seeing the index in the console does not give you a way to use that index later in code?
0
Slick812Commented:
You can add Properties to any JS object, so you MIGHT could an Array Index property to the marker -

marker.addTo(map);
 marker.bindPopup(the_popup).openPopup();
marker.mlAryIndex = markerList.length;
 markerList.push(marker);


and Maybe use -

onMarkerClickDelete: function (e) {
map.removeLayer(this);
var marker_index = this.mlAryIndex;
markerList.splice(this.mlAryIndex, 1);
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
lvmllcAuthor Commented:
Not completely what I needed but helped me out. Thanks.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.