I'm experimenting with creating a map with Leaflet and displaying a marker on the map. Does anyone have any thoughts on the following error I receive when displaying a marker on the map?
I can initiate a map onload of the page:
var map;
var ajaxRequest;
var plotlist;
var plotlayers=[];
function initmap() {
// set up the map
map = new L.Map('map');
// create the tile layer with correct attribution
var osmUrl='https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png';
var osmAttrib='Map data © <a href="https://openstreetmap.org">OpenStreetMap</a> contributors';
var osm = new L.TileLayer(osmUrl, {minZoom: 8, maxZoom: 20, attribution: osmAttrib});
// start the map over Holland
map.setView(new L.LatLng(52.36994, 4.90788),8);
map.addLayer(osm);
}
I created a marker:
var marker=L.marker([52.63275, 4.75175]).bindPopup('testmarker');
When adding the marker to the map, I get an error "Uncaught TypeError: Cannot read property 'addLayer' of undefined":
marker.addTo(map);
When including this line within the initmap() function, it works fine. So appearantly the map instance is not reachable or something outside the function? Which is odd since the variable is created at the start of the script, outside the function?
Thanks!