Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Leaflet Uncaught Error: Invalid LatLng object: (NaN, NaN)

I am trying to load map markers dynamically with leaflet and am getting this error:

Uncaught Error: Invalid LatLng object: (NaN, NaN)

I am using this javascript to load my markers:

            var lat;
            var lng;


            for (i = 0; i < data.length; i++) {
                lat = parseFloat(data[i].lat);
                lng = parseFloat(data[i].lng);

                L.marker([lat, lng]).addTo(map);
            }

My json I am reading is this:

[{"brewery":"21st Amendment Brewery","lat":"37.7824175","lng":"-122.3925921","breweryID":"EdRcIs"},{"brewery":"Abita Brewing Company","lat":"30.482408","lng":"-90.056605","breweryID":"BBE5WM"},{"brewery":"AleSmith Brewing Company","lat":"32.8924532","lng":"-117.1447157","breweryID":"ygAzC9"},{"brewery":"Allagash Brewing Company","lat":"43.702697","lng":"-70.317975","breweryID":"pdLPeS"},{"brewery":"Anchor Brewing Company","lat":"37.763487","lng":"-122.401367","breweryID":"6PBXvz"},{"brewery":"Anderson Valley Brewing Company","lat":"39.001384","lng":"-123.356081","breweryID":"L2dAEr"},{"brewery":"Anheuser-Busch","lat":"38.5986606","lng":"-90.208751","breweryID":"BznahA"},{"brewery":"Anheuser-Busch InBev","lat":"38.5985037","lng":"-90.2093428","breweryID":"BznahA"},{"brewery":"Asahi Breweries","lat":"35.709646","lng":"139.799797","breweryID":"tu8Rbm"},{"brewery":"Asia Pacific Breweries Ltd.","lat":"1.3361749","lng":"103.647081","breweryID":"AvTZ60"},{"brewery":"Ass Kisser Ales","lat":"32.729223","lng":"-97.415003","breweryID":"pqSkmD"},{"brewery":"Avery Brewing Company","lat":"40.016644","lng":"-105.218628","breweryID":"Jio9R0"},{"brewery":"Ayinger Brewery","lat":"","lng":"","breweryID":"QKdFk2"},{"brewery":"Ballast Point Brewing Company","lat":"32.7667586","lng":"-117.1952129","breweryID":"1d90Lc"},{"brewery":"Ballast Point Brewing Company","lat":"32.766785","lng":"-117.195246","breweryID":"1d90Lc"},{"brewery":"Baltimore-Washington Beer Works","lat":"39.167823","lng":"-76.5240722","breweryID":"dwroV3"},{"brewery":"Bass Brewery","lat":"52.8045853","lng":"-1.6281372","breweryID":"X2Qkw1"},{"brewery":"Bear Republic Brewery","lat":"38.6040452","lng":"-122.8599961","breweryID":"5tw2Iw"},{"brewery":"Belching Beaver Brewery","lat":"33.1450383","lng":"-117.2285575","breweryID":"rVzPCl"},{"brewery":"Bells Brewery, Inc.","lat":"42.2843486","lng":"-85.4538497","breweryID":"noGtDY"},{"brewery":"Big Muddy Brewing Co","lat":"37.7823381","lng":"-89.3321145","breweryID":"oBe8dQ"},{"brewery":"Birra Toccalmatto","lat":"44.866279","lng":"10.0615953","breweryID":"wAdTKf"},{"brewery":"Bootleggers Brewery","lat":"33.8682504","lng":"-117.933221","breweryID":"JQfyvi"},{"brewery":"Booze Brothers Brewing Co.","lat":"33.148172","lng":"-117.218047","breweryID":"xaBLno"},{"brewery":"Boston Beer Company","lat":"42.314449","lng":"-71.103365","breweryID":"1wSztN"},{"brewery":"Boulder Beer Company","lat":"40.0266779","lng":"-105.2479819","breweryID":"stdQLg"},{"brewery":"Boulevard Brewing Company","lat":"39.0821348","lng":"-94.5966532","breweryID":"VDMEV7"},{"brewery":"Brasserie des Franches-Montagnes","lat":"47.2542808","lng":"7.0025782","breweryID":"6DG1qh"},{"brewery":"Brasserie Dupont","lat":"50.5717946","lng":"3.6507079","breweryID":"o7aslJ"},{"brewery":"Breakside Brewery","lat":"45.571532","lng":"-122.6567911","breweryID":"bdjbTZ"},{"brewery":"Breckenridge Brewery","lat":"39.723597","lng":"-105.000614","breweryID":"IImUD9"},{"brewery":"BrewDog Ltd","lat":"57.64784","lng":"-1.934689","breweryID":"wfAwfx"},{"brewery":"Brewers Art","lat":"39.302771","lng":"-76.616387","breweryID":"YytkpO"},{"brewery":"Brewery Ommegang","lat":"42.6818046","lng":"-74.9254739","breweryID":"tCxPtR"},{"brewery":"Brewery Ommegang","lat":"42.6268786","lng":"-74.9452273","breweryID":"tCxPtR"},{"brewery":"Brooklyn Brewery","lat":"40.721535","lng":"-73.95745","breweryID":"4OBVPn"},{"brewery":"Brouwerij Artois","lat":"50.8870692","lng":"4.7025108","breweryID":"mIWMKP"},{"brewery":"Brouwerij Huyghe","lat":"50.99832","lng":"3.79404","breweryID":"8eyXN7"},{"brewery":"Brouwerij St. Bernardus","lat":"50.8425017","lng":"2.6362699","breweryID":"w9vR2u"},{"brewery":"Brouwerij Van Steenberge","lat":"51.176439","lng":"3.746045","breweryID":"shhuGO"},{"brewery":"Buffalo Bills Brewery","lat":"37.6738157","lng":"-122.0813029","breweryID":"P1I1gt"},{"brewery":"Burley Oak Craft Brewery","lat":"38.3338561","lng":"-75.2169099","breweryID":"rKXfsB"},{"brewery":"Butternuts Beer and Ale","lat":"42.6290648","lng":"-75.1854868","breweryID":"9BPs2d"},{"brewery":"Caldera Brewing","lat":"42.184409","lng":"-122.663147","breweryID":"iL3Juq"},{"brewery":"Center of the Universe Brewing","lat":"37.708939","lng":"-77.446687","breweryID":"B00bip"},{"brewery":"Cervecera Cuauhtmoc-Moctezuma","lat":"25.6375417","lng":"-100.2946801","breweryID":"UWBCmm"},{"brewery":"Clipper City Brewing Company","lat":"39.230827","lng":"-76.6751309","breweryID":"wTFQaf"},{"brewery":"Coors Brewing Company","lat":"39.759876","lng":"-105.219026","breweryID":"avMkil"},{"brewery":"Coronado Brewing Company","lat":"32.6977264","lng":"-117.1731838","breweryID":"gVynTw"},{"brewery":"Cricket Hill Brewing Company","lat":"40.872566","lng":"-74.296359","breweryID":"U0vh9j"},{"brewery":"D.G. Yuengling and Son","lat":"40.6999592","lng":"-76.1747169","breweryID":"pX8lES"},{"brewery":"DC Brau Brewing Company","lat":"38.929628","lng":"-76.960438","breweryID":"KRB0Bo"},{"brewery":"Devils Backbone Brewing Company","lat":"37.871161","lng":"-78.919124","breweryID":"iw1hDB"},{"brewery":"Dogfish Head Craft Brewery","lat":"38.770622","lng":"-75.310893","breweryID":"g0jHqt"},{"brewery":"DuClaw Brewing Company","lat":"39.496656","lng":"-76.329843","breweryID":"TVgBWg"},{"brewery":"Elysian Brewing Company","lat":"47.614033","lng":"-122.315716","breweryID":"hvWfx5"},{"brewery":"Erie Brewing Company","lat":"42.111185","lng":"-80.112978","breweryID":"LHQ79n"},{"brewery":"Evolution Craft Brewing Company","lat":"38.4622609","lng":"-75.5741469","breweryID":"Ysh6PO"},{"brewery":"Firestone Walker Brewing Company","lat":"35.595325","lng":"-120.693728","breweryID":"qghrkC"},{"brewery":"Firestone Walker Brewing Company","lat":"35.5954123","lng":"-120.6950281","breweryID":"qghrkC"},{"brewery":"Flying Dog Brewery","lat":"39.361926","lng":"-77.424677","breweryID":"jmGoBA"},{"brewery":"Flying Fish Brewing Company","lat":"39.912138","lng":"-74.970075","breweryID":"M5zcKb"},{"brewery":"Foothills Brewing Company","lat":"36.097341","lng":"-80.250882","breweryID":"ajREdT"},{"brewery":"Fordham Brewing Company","lat":"39.1540034","lng":"-75.4883626","breweryID":"D3A2mu"},{"brewery":"Founders Brewing Company","lat":"42.958468","lng":"-85.673516","breweryID":"Idm5Y5"},{"brewery":"Fulton Brewing Company","lat":"44.9849423","lng":"-93.2790206","breweryID":"5GoGSi"},{"brewery":"Goose Island Beer Company","lat":"41.887107","lng":"-87.6720969","breweryID":"APW1BC"},{"brewery":"Gordon Biersch Brewing","lat":"37.354139","lng":"-121.89286","breweryID":"P2xdU4"},{"brewery":"Grand Canyon Brewing Company","lat":"35.249954","lng":"-112.1891462","breweryID":"xcKkLh"},{"brewery":"Great Divide Brewing Company","lat":"39.753646","lng":"-104.9884698","breweryID":"I8WZv2"},{"brewery":"Green Flash Brewing Company","lat":"32.905722","lng":"-117.176517","breweryID":"Nj8cgD"},{"brewery":"Grupo Modelo S.A. de C.V.","lat":"19.422447","lng":"-99.209758","breweryID":"wadu38"},{"brewery":"Guinness","lat":"53.34309","lng":"-6.290799","breweryID":"HaPdSL"},{"brewery":"Hardywood Park Craft Brewery","lat":"37.5637197","lng":"-77.4603258","breweryID":"mftbkH"},{"brewery":"Harpoon Brewery","lat":"43.512997","lng":"-72.401527","breweryID":"RzvedX"},{"brewery":"Heavy Seas","lat":"39.230827","lng":"-76.6751309","breweryID":"wTFQaf"},{"brewery":"Heineken International","lat":"","lng":"","breweryID":"robMSl"},{"brewery":"Heritage Brewing","lat":"38.7523173","lng":"-77.4887259","breweryID":"LWv6MU"},{"brewery":"Hoppin Frog Brewery","lat":"41.0245928","lng":"-81.4677107","breweryID":"w5OOQ5"},{"brewery":"Innis & Gunn","lat":"55.951976","lng":"-3.211095","breweryID":"jC0TAa"},{"brewery":"Iron Fist Brewing Company","lat":"33.145823","lng":"-117.238546","breweryID":"hjb9rn"},{"brewery":"Jacob Leinenkugel Brewing Company","lat":"44.943321","lng":"-91.3953049","breweryID":"ZDghkK"},{"brewery":"Jester King Craft Brewery","lat":"30.233431","lng":"-97.995129","breweryID":"7kCNmJ"},{"brewery":"Kane Brewing","lat":"40.2368095","lng":"-74.0442545","breweryID":"XqWTM7"},{"brewery":"Karl Strauss Brewing Company","lat":"32.8320492","lng":"-117.2312344","breweryID":"mtUjck"},{"brewery":"Karl Strauss Brewing Company","lat":"32.832461","lng":"-117.2318045","breweryID":"mtUjck"},{"brewery":"Knee Deep Brewing Company","lat":"38.891802","lng":"-121.292128","breweryID":"iAJcs1"},{"brewery":"Kona Brewing Company","lat":"19.6419767","lng":"-155.9964298","breweryID":"8tlUI3"},{"brewery":"Lagunitas Brewing Company","lat":"38.272435","lng":"-122.662346","breweryID":"nLsoQ9"},{"brewery":"Lancaster Brewing Company","lat":"40.043733","lng":"-76.2980937","breweryID":"VGDfKl"},{"brewery":"Latitude 33 Brewing Co.","lat":"33.1362843","lng":"-117.2244859","breweryID":"GpPbOw"},{"brewery":"Latrobe Brewing Company","lat":"40.3197031","lng":"-79.3907376","breweryID":"MDO5n4"},{"brewery":"Left Hand Brewing Company","lat":"40.1587451","lng":"-105.1128246","breweryID":"Ro08YF"},{"brewery":"Lonerider Brewing Company","lat":"35.903672","lng":"-78.762493","breweryID":"d25euF"},{"brewery":"Magic Hat Brewing Company","lat":"44.428405","lng":"-73.213148","breweryID":"qIqpZc"},{"brewery":"Maine Beer Company","lat":"43.7057907","lng":"-70.3148766","breweryID":"xgrmyW"},{"brewery":"Maui Brewing Co.","lat":"20.972149","lng":"-156.677334","breweryID":"fwCFE4"},{"brewery":"Maui Brewing Company","lat":"20.972149","lng":"-156.677334","breweryID":"fwCFE4"},{"brewery":"Mendocino Brewing Company","lat":"38.9734335","lng":"-123.1162429","breweryID":"TR98tr"},{"brewery":"MillerCoors Brewing Company","lat":"43.044464","lng":"-87.962562","breweryID":"MWi5Kp"},{"brewery":"Moa Brewing Company","lat":"-41.542774","lng":"173.834236","breweryID":"McQx07"},{"brewery":"Mother Earth Brew Company","lat":"33.1682803","lng":"-117.2198359","breweryID":"NwFxny"},{"brewery":"Narragansett Brewing Company","lat":"41.8190249","lng":"-71.4097368","breweryID":"mGqkXE"},{"brewery":"Nelson Brewery","lat":"51.3943334","lng":"0.5297882","breweryID":"OYQA8m"},{"brewery":"Neshaminy Creek Brewing Company","lat":"40.085451","lng":"-74.902305","breweryID":"UbQHhM"},{"brewery":"New Belgium Brewing","lat":"40.593415","lng":"-105.066874","breweryID":"Jt43j7"},{"brewery":"New Holland Brewing Company","lat":"42.790315","lng":"-86.103885","breweryID":"AqEUBQ"},{"brewery":"Ninkasi Brewing Company","lat":"44.056855","lng":"-123.10988","breweryID":"V4I9FT"},{"brewery":"No Label Brewing Company","lat":"29.786632","lng":"-95.815891","breweryID":"4GJFCc"},{"brewery":"North Coast Brewing Company","lat":"39.446471","lng":"-123.806238","breweryID":"yLBNrD"},{"brewery":"Oak Creek Brewery","lat":"34.8663917","lng":"-111.7961404","breweryID":"9WPTNZ"},{"brewery":"Old Dominion Brewing Company","lat":"39.1540034","lng":"-75.4883626","breweryID":"xG9JyI"},{"brewery":"Oskar Blues Brewery","lat":"40.139056","lng":"-105.121887","breweryID":"q6vJUK"},{"brewery":"Oskar Blues Brewery","lat":"40.1390698","lng":"-105.1228796","breweryID":"q6vJUK"},{"brewery":"Pabst Brewing Company","lat":"29.4497864","lng":"-98.5386319","breweryID":"AKyyYN"},{"brewery":"Port Brewing Company","lat":"33.141417","lng":"-117.150076","breweryID":"ayEBYP"},{"brewery":"Port City Brewing Company","lat":"38.808003","lng":"-77.101365","breweryID":"9FwufS"},{"brewery":"Port City Brewing Company","lat":"38.8081958","lng":"-77.1014693","breweryID":"9FwufS"},{"brewery":"Refuge Brewery","lat":"33.5045422","lng":"-117.1585452","breweryID":"EswbVZ"},{"brewery":"Rinn Duin Brewing","lat":"40.123373","lng":"-74.24754","breweryID":"T90yOZ"},{"brewery":"Rip Current Brewing","lat":"33.1429833","lng":"-117.1925366","breweryID":"u2ExM0"},{"brewery":"River Horse Brewing Company","lat":"40.2763769","lng":"-74.796004","breweryID":"sYdp0k"},{"brewery":"RJ Rockers Brewing Company","lat":"34.948639","lng":"-81.935726","breweryID":"bWL816"},{"brewery":"Rock Art Brewery","lat":"44.557791","lng":"-72.608085","breweryID":"LpX3cU"},{"brewery":"Rogue Ales","lat":"44.620173","lng":"-124.0520565","breweryID":"X0l98q"},{"brewery":"Rough Draft Brewing Company","lat":"32.883619","lng":"-117.171625","breweryID":"gF2S17"},{"brewery":"Russian River Brewing Company","lat":"38.441415","lng":"-122.711872","breweryID":"BSsTGw"},{"brewery":"Rusty Beaver Brewery","lat":"38.0180585","lng":"-77.5151866","breweryID":"3RFxH6"},{"brewery":"Saint Archer Brewing Company","lat":"32.880493","lng":"-117.1631014","breweryID":"vOvOzV"},{"brewery":"San Marcos Brewery & Grill","lat":"33.134539","lng":"-117.190813","breweryID":"SEibYT"},{"brewery":"Schneider Weisse","lat":"48.91755","lng":"11.87451","breweryID":"FQLVgV"},{"brewery":"Scottish & Newcastle Breweries","lat":"55.954155","lng":"-3.202774","breweryID":"Qutakc"},{"brewery":"Shepherd Neame","lat":"51.3169414","lng":"0.8921279","breweryID":"c67gGy"},{"brewery":"Shipyard Brewing","lat":"43.6618784","lng":"-70.2489402","breweryID":"5N0usi"},{"brewery":"Shooting Creek Farm Brewery","lat":"36.907584","lng":"-80.218943","breweryID":"gop0Gz"},{"brewery":"Sierra Nevada Brewing Company","lat":"39.7246028","lng":"-121.8157154","breweryID":"nHLlnK"},{"brewery":"Sixpoint Brewery","lat":"40.6739317","lng":"-74.0119473","breweryID":"7VPQrN"},{"brewery":"Sixpoint Craft Ales","lat":"40.6739317","lng":"-74.0119473","breweryID":"7VPQrN"},{"brewery":"Sleeman Breweries Ltd.","lat":"43.4870365","lng":"-80.2067709","breweryID":"D61TcY"},{"brewery":"Smuttynose Brewing Co.","lat":"43.032502","lng":"-70.7947688","breweryID":"v0MKXA"},{"brewery":"Smuttynose Brewing Company","lat":"43.0323999","lng":"-70.7956193","breweryID":"v0MKXA"},{"brewery":"Somerville Brewing Company","lat":"42.402309","lng":"-71.120132","breweryID":"gwDTjS"},{"brewery":"Sonoran Brewing Company","lat":"33.448675","lng":"-112.016904","breweryID":"cuhBRm"},{"brewery":"Southern Tier Brewing Company","lat":"42.088931","lng":"-79.350342","breweryID":"x8kqVp"},{"brewery":"Spaten-Franziskaner-Br\u00e4u","lat":"48.14501","lng":"11.55419","breweryID":"o5vGy8"},{"brewery":"Spoetzl Brewery","lat":"29.434494","lng":"-97.166729","breweryID":"f35hQR"},{"brewery":"Staatliches Hofbr\u00e4uhaus (Hofbr\u00e4u M\u00fcnchen)","lat":"48.1373073","lng":"11.580357","breweryID":"4rlPFf"},{"brewery":"Stone Brewing Co.","lat":"33.1160006","lng":"-117.119811","breweryID":"709vEK"},{"brewery":"Stone Brewing Company","lat":"33.115682","lng":"-117.119552","breweryID":"709vEK"},{"brewery":"Stone Brewing Company","lat":"33.1160006","lng":"-117.119811","breweryID":"709vEK"},{"brewery":"Stoudts Brewery","lat":"40.2422441","lng":"-76.0540026","breweryID":"REDaIN"},{"brewery":"Sudwerk","lat":"","lng":"","breweryID":"bRKIdZ"},{"brewery":"Surly Brewing Company","lat":"45.04285","lng":"-93.324557","breweryID":"cPRfoj"},{"brewery":"SweetWater Brewing Company","lat":"33.808102","lng":"-84.38119","breweryID":"TMc6H2"},{"brewery":"Terrapin Beer Company","lat":"33.9804178","lng":"-83.3956826","breweryID":"DPLTAJ"},{"brewery":"The Bronx Brewery","lat":"40.80828","lng":"-73.930669","breweryID":"V0wvf7"},{"brewery":"The Bruery","lat":"33.8613833","lng":"-117.8800107","breweryID":"4MVtcc"},{"brewery":"The Duck-Rabbit Craft Brewery","lat":"35.600267","lng":"-77.597058","breweryID":"rBxNNF"},{"brewery":"The Lost Abbey","lat":"33.1416273","lng":"-117.1492671","breweryID":"9x7wNn"},{"brewery":"The Lost Abbey","lat":"33.141417","lng":"-117.150076","breweryID":"9x7wNn"},{"brewery":"The Saint Louis Brewery","lat":"38.632968","lng":"-90.209769","breweryID":"tNDKBY"},{"brewery":"Tr\u00f6egs Brewing Company","lat":"40.304","lng":"-76.643519","breweryID":"n5QFi2"},{"brewery":"Tsingtao","lat":"36.087509","lng":"120.34272","breweryID":"e2agAL"},{"brewery":"Unibroue","lat":"45.466125","lng":"-73.290924","breweryID":"llbEuB"},{"brewery":"Union Craft Brewing Company","lat":"39.331938","lng":"-76.641971","breweryID":"t6Gyij"},{"brewery":"Victory Brewing Company","lat":"40.0061038","lng":"-75.6941546","breweryID":"VoKbnS"},{"brewery":"Wells and Young&#39;s Brewing Company","lat":"52.132011","lng":"-0.481523","breweryID":"6oEP92"},{"brewery":"Westbrook Brewing Company","lat":"32.846477","lng":"-79.877235","breweryID":"HPfwPW"},{"brewery":"Weyerbacher Brewing Company","lat":"40.673181","lng":"-75.224926","breweryID":"a57dkm"},{"brewery":"Weyerbacher Brewing Company","lat":"40.673093","lng":"-75.225856","breweryID":"a57dkm"},{"brewery":"Widmer Brothers Brewing","lat":"45.540795","lng":"-122.675594","breweryID":"8wcv7h"},{"brewery":"Yards Brewing Company","lat":"39.962616","lng":"-75.136253","breweryID":"jwWiTH"}]
like image 301
Mike Avatar asked Aug 16 '15 02:08

Mike


1 Answers

Some pieces of your data have blank lat and lng causing parseFloat('') which returns NaN. The best fix would be to fix your data and avoid the blanks. If not, have your server-side logic catch those blanks. If not possible (because you're fetching from a third-party API or something), make your JS default to some number if it finds a blank string.

like image 191
Joseph Avatar answered Oct 17 '22 00:10

Joseph