In the below screen shot you can see a marker labeled Mother Earth Brewery, but it should read Dogfish Head:
My code for adding my map box markers is:
var lat;
var lng;
var markers = [];
var markerTemp;
for (i = 0; i < data.length; i++) {
lat = parseFloat(data[i].lat);
lng = parseFloat(data[i].lng);
if (lat == "" || lng == "") {
} else {
L.marker([lat, lng]).addTo(map).bindPopup(data[i].brewery);
markerTemp = L.marker([lat, lng]);
markers.push(markerTemp);
alert("Brewery: " + data[i].brewery + "Lat: " + lat + "Long: " + lng );
}
var group = L.featureGroup(markers);
map.fitBounds(group.getBounds());
}
the variable data is json that is being parsed below:
[{"brewery":"East Coast Beer Company","lat":"40.091779","lng":"-74.049265","breweryID":"0BRjKa"},{"brewery":"Ballast Point Brewing Company","lat":"32.766785","lng":"-117.195246","breweryID":"1d90Lc"},{"brewery":"Fulton Brewing Company","lat":"44.9849423","lng":"-93.2790206","breweryID":"5GoGSi"},{"brewery":"Stone Brewing Co.","lat":"33.115682","lng":"-117.119552","breweryID":"709vEK"},{"brewery":"The Lost Abbey","lat":"33.141417","lng":"-117.150076","breweryID":"9x7wNn"},{"brewery":"Dogfish Head Craft Brewery","lat":"38.770622","lng":"-75.310893","breweryID":"g0jHqt"},{"brewery":"Latitude 33 Brewing Co.","lat":"33.1362843","lng":"-117.2244859","breweryID":"GpPbOw"},{"brewery":"Coronado Brewing Company","lat":"32.6977264","lng":"-117.1731838","breweryID":"gVynTw"},{"brewery":"Iron Fist Brewing Company","lat":"33.145823","lng":"-117.238546","breweryID":"hjb9rn"},{"brewery":"Heritage Brewing","lat":"38.7523173","lng":"-77.4887259","breweryID":"LWv6MU"},{"brewery":"Karl Strauss Brewing Company","lat":"32.832461","lng":"-117.2318045","breweryID":"mtUjck"},{"brewery":"Bagby Beer Company","lat":"33.189225","lng":"-117.374257","breweryID":"Nl2U0R"},{"brewery":"Mother Earth Brew Company","lat":"38.770622","lng":"-75.310893","breweryID":"NwFxny"},{"brewery":"Pizza Port Brewing Company","lat":"33.1596505","lng":"-117.3478111","breweryID":"QSVVxD"},{"brewery":"Burley Oak Craft Brewery","lat":"38.3338561","lng":"-75.2169099","breweryID":"rKXfsB"},{"brewery":"Belching Beaver Brewery","lat":"33.1450383","lng":"-117.2285575","breweryID":"rVzPCl"},{"brewery":"San Marcos Brewery & Grill","lat":"33.134539","lng":"-117.190813","breweryID":"SEibYT"},{"brewery":"Union Craft Brewing Company","lat":"39.331938","lng":"-76.641971","breweryID":"t6Gyij"},{"brewery":"Rinn Duin Brewing","lat":"40.123373","lng":"-74.24754","breweryID":"T90yOZ"},{"brewery":"Victory Brewing Company","lat":"40.0061038","lng":"-75.6941546","breweryID":"VoKbnS"},{"brewery":"Heavy Seas","lat":"39.230827","lng":"-76.6751309","breweryID":"wTFQaf"},{"brewery":"Booze Brothers Brewing Co.","lat":"33.148172","lng":"-117.218047","breweryID":"xaBLno"},{"brewery":"Kane Brewing","lat":"40.2368095","lng":"-74.0442545","breweryID":"XqWTM7"},{"brewery":"AleSmith Brewing Company","lat":"32.892034","lng":"-117.144379","breweryID":"ygAzC9"},{"brewery":"Bay City Brewing","lat":"32.7579949","lng":"-117.2118382","breweryID":"yOH4Du"}]
In the code you can see I added this alert("Brewery: " + data[i].brewery + "Lat: " + lat + "Long: " + lng );
to make sure the right values were going into the
markerTemp = L.marker([lat, lng]);
markers.push(markerTemp);
I verified the correct data was going in one by one via the alert and comparing to the output of the JSON. All looks correct and I am stuck, a second set of eyes would greatly be appreciated. Thanks!
Last two lines of the code should be outside the for loop. You intent to add all the markers as a group into the map, however what you do is actually seems to be programatically incorrect.
I noticed that comparison of a float number with a string is not a good programming practice.
if (lat == "" || lng == "")
Here I have modified the code accordingly: