Here is the template:
<div id="mapid" style="height: 500px"></div>
After installing Leaflet and the typings for Leaflet, I encountered an error stating that the map container was not found. To solve this, I added its import.
This is the controller code:
import { Component, OnInit, EventEmitter, Output } from
'@angular/core';
import * as L from 'leaflet';
import { Map } from 'leaflet';
@Component({
selector: 'app-leafletmap',
templateUrl: './leafletmap.component.html',
styleUrls: ['./leafletmap.component.css']
})
export class LeafletmapComponent implements OnInit {
mymap = L.map('mapid').setView([29.6516, -82.3248], 13);
L.tileLayer('https://api.tiles.mapbox.com/v4/{id}/{z}/{x}/{y}.png?access_token={accessToken}',
{
attribution: 'Map data © <a href="http://openstreetmap.org">OpenStreetMap</a> contributors,
<a href="http://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>, Imagery © <a href="http://mapbox.com">Mapbox</a>',
maxZoom: 18,
id: 'mapbox.streets',
accessToken: '*****************************'
}).addTo(mymap);
popup = L.popup();
marker: any;
onMapClick(e) {
if (marker != undefined)
mymap.removeLayer(marker)
marker = new L.Marker(e.latlng, { draggable: true });
mymap.addLayer(marker);
popup.setLatLng(e.latlng).setContent("You clicked the map at " + e.latlng.toString()).openOn(mymap);
}
mymap.on('zoomend', function() {
console.log(mymap.getZoom());
})
mymap.on('click', onMapClick);
constructor() { }
ngOnInit() {
}
}
I am uncertain about whether I am correctly passing access tokens and initializing variables in TypeScript, as I wrote this code based on a tutorial that used regular JavaScript.