margorai
margorai

Reputation: 23

Creating Popup content for Leaftlet marker using js array

In my script for leaflet.js I have an array of locations which I have successfully created markers for. I would also like to use the bind.Popup function to display content stored in the array. To code the bind.Popup content, it seems to only work with an HTML paragraph tag, but I was hoping to reference the third item in my array. I'm not sure if this has to be done in HTML instead, but any help would be appreciated as I am new to Java! Here is the piece of code for reference, (I plan to add more locations and content, but want to first figure out the code):

var locations = [
["Warren", 43.059908,-89.442229, "Barn"],
["Brunsell", 43.058618,-89.442032, "House"],
["Jacobs", 43.058618,-89.441726, "Jacobs/Usonia 1"]
];

var marker, i
for (i = 0; i < locations.length; i++){

    var markerLocation = new L.LatLng(locations[i][1], locations[i][2]);
    marker = new L.Marker(markerLocation);
    westmorland.addLayer(marker);

};

Upvotes: 2

Views: 1040

Answers (1)

Doug
Doug

Reputation: 3312

Perhaps I'm misunderstanding you here, but I'm pretty sure that the following should work.

var marker, i;

for (i = 0; i < locations.length; i++){
  var markerLocation = new L.LatLng(locations[i][1], locations[i][2]);
  marker = new L.Marker(markerLocation);
  marker.bindPopup(locations[i][3]);
  westmorland.addLayer(marker);
};

Upvotes: 1

Related Questions