I'm trying to display three maps on one page with leaflet.js and mapquests API. I've created a jQuery for-loop
to read a unique postal code, then convert it (via mapquest) to latitude and longitude, and lastly create three maps, and add the matching latitude and longitude to every map. This would then result in three different maps, displayed on one page.
I've somewhat achieved this with the code below. Every "card" has its own card number (from 1 to 3), postal code, and corresponding map and map ID.
Edit I also get this jQuery error:
jQuery.Deferred exception: t is undefined addTo@https://unpkg.com/leaflet@1.6.0/dist/leaflet.js:5:64070
MYWEBSITEURL/map.js:38:6
e@https://code.jquery.com/jquery-3.5.1.min.js:2:30005
l/</t<@https://code.jquery.com/jquery-3.5.1.min.js:2:30307
undefined
The issue I now have is that when my jQuery runs I get this console error: TypeError: t is undefined
. I've narrowed it down to it having something to do with either the L.tileLayer()
and/or L.circle()
functions. I've added the latest jQuery and leaflet.js to the page.
Edit Changed the question after @ivansanchez answer, but still the same issues.
Edit Changed the leaflet.js
to leaflet-sr.js
and I now get the error: TypeError: map is undefined
map.js:
$(document).ready(function(){
var postalCodes = [];
var latLng = [];
var lat = [];
var lng = [];
var maps = [];
for(var n = 0; n < 3; n++) {
console.log('Maps for-loop count: '+n);
postalCodes.push($('.card'+[n]+' .card__info--postal > span').html());
$.ajax({ // Get lat & long from postal code
async: false,
url: 'https://www.mapquestapi.com/geocoding/v1/address?key=MYAPIKEY&postalCode='+postalCodes[n]+'&country=finland',
success: function(obj) {
latLng[n] = obj.results[0].locations[0].latLng;
lat[n] = latLng[n].lat;
lng[n] = latLng[n].lng;
},
error: function(xhr) {
console.log('Map error', xhr);
}
});
console.log(latLng[n], lat[n], lng[n]);
var map = L.map('mapid'+[n]).setView([lat[n], lng[n]], 13);
L.tileLayer('https://api.mapbox.com/styles/v1/{id}/tiles/{z}/{x}/{y}?access_token={accessToken}', {
maxZoom: 18,
id: 'mapbox/streets-v11',
tileSize: 512,
zoomOffset: -1,
accessToken: 'MYAPIKEY'
}).addTo(maps[n]);
L.circle([lat[n], lng[n]], { // Map out area of the postal code
color: 'rgb(218, 65, 103)',
fillColor: 'rgb(218, 65, 103)',
fillOpacity: 0.5,
radius: 500
}).addTo(maps[n]);
maps.push(map);
}
});