Usage of Leaflet with Ionic2 typescript Usage of Leaflet with Ionic2 typescript typescript typescript

Usage of Leaflet with Ionic2 typescript


Ok..First install leaflet and its typings

npm install leaflet --savenpm install @types/leaflet --save

Then import leaflet to your Component or whatever with

import 'leaflet';

In the html-file add a div with id="map" and a pre-set size (better do it via css).

<div style="height: 180px" id="map"></div>

As styleUrls: [] is still buggy in Ionic2, you also have to add the leaflet styles to your html-file:

<link rel="stylesheet" href="https://unpkg.com/leaflet@1.0.1/dist/leaflet.css" />

After this preparing you can start with the leaflet tutorial like this:

ngOnInit(): void {   var map = L.map('map')      .setView([51.505, -0.09], 13);   L.tileLayer('https://api.tiles.mapbox.com/v4/mapbox.streets-basic/{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,      accessToken: 'xxx'    }).addTo(this.map);}