Documente Academic
Documente Profesional
Documente Cultură
Technology
FEATURED
GETTING STARTED WITH POSTS
NGX-LEAFLET
ADD MAPS TO ANGULAR CLI PROJECTS The
NOV 21, 2017
Pitfalls
of
In our last post, we introduced @asymmetrik/ngx-leaflet – a PowerPoint
compact and extensible library for integrating Leaflet maps
Lessons
into Angular.io applications. Now, we’ll show you how you
can integrate it into your @angular/cli projects. This tutorial from the
now supports Angular v7. Columbia
Disaster
GETTING AND RUNNING THIS
TUTORIAL
If you just want to see a working example, you can find the
code for this tutorial on Github. All you need to do is clone
CHC
the repo, install the dependencies, and run the application 2018
using the following commands: Recap
Apps
# Clone the Repository and
$ git clone git@github.com:Asymmetrik/ngx-leafl
BBQ in
# Install @angular/cli Kansas
$ npm install -g @angular/cli
City
# Install the dependencies with npm
$ cd ngx-leaflet-tutorial-ngcli
$ npm install
# Run the tutorial application It’s a
$ ng serve
Crab
Feast!
For a complete walkthrough, read on! The following sections Or is it
walk you through installing the ngx-leaflet plugin, creating a
simple map, and adding customer layers and markers.
a
Fest?
A
USING NGX-LEAFLET WITH ANGULAR
Recap
CLI
of Our
The following walkthrough demonstrates how to create a 2018
new project, import and configure the @asymmetrik/ngx- Family
leaflet plugin, and use the plugin to add maps. As previously
mentioned, you can find the code for this tutorial on Github.
Picnic
Intern
Spotlight:
Alexi
Doak
Q&A
with an
Asymmetrik
Software
Intern
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
],
LeafletModule.forRoot()
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
For the map to render correctly, you have to add the Leaflet
stylesheet to the Angular CLI global stylesheet configuration.
To do this, add the following to angular.json:
{
...
"styles": [
"src/styles.css",
"./node_modules/leaflet/dist/leaflet.css"
],
...
}
{
...
"assets": [
{
"glob": "**/*",
"input": "./node_modules/leaflet/dist/ima
"output": "leaflet/"
},
"src/assets",
"src/favicon.ico"
],
...
}
Now that all that setup is out of the way, you can get to the
fun part. The next step is to add a map to the application.
First, replace the contents of
src/app/app.component.html with a single div containing
the leaflet directive:
<div class="map"
leaflet
[leafletOptions]="options">
</div>
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
options = {
layers: [
tileLayer('https://{s}.tile.openstreetmap
attribution: '© OpenStreetMap cont
})
],
zoom: 7,
center: latLng([ 46.879966, -121.726909 ])
};
The last thing you need to do to get this all working is add
some styles to make sure the map expands to fit the whole
screen. It’s actually pretty simple, but you need to modify
two different stylesheets because some of the styles are
local to the component and some are global to the
application.
.map {
height: 100%;
padding: 0;
}
html, body {
height: 100%;
margin: 0;
padding: 0;
}
<div class="map"
leaflet
[leafletOptions]="options"
[leafletLayersControl]="layersControl"></div>
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
...
onMapReady(map: Map) {
map.fitBounds(this.route.getBounds(), {
padding: point(24, 24),
maxZoom: 12,
animate: true
});
}
}
<div class="map"
leaflet
(leafletMapReady)="onMapReady($event)"
[leafletOptions]="options"
[leafletLayersControl]="layersControl"></d
LEARN MORE