I am using the template from the ngx-leaflet tutorial for this example. I am a total beginner to Angular and just barely know leaflet.js, for reference.
I have a large multipolygon geoJSON file that I want to display on a map and eventually add interactivity to. I've saved the file in the assets
directory as china_adm1.geojson
.
app.component.html
looks like:
<div class="map"
leaflet
[leafletOptions]="options"
[leafletLayers]="china"
[leafletLayersControl]="layersControl"
>
</div>
and app.component.ts
looks like:
import { Component } from '@angular/core';
import { geoJSON, latLng, tileLayer } from 'leaflet';
import { adm1 } from '../assets/china_adm1';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
china = geoJSON(adm1);
// Define our base layers so we can reference them multiple times
googleMaps = tileLayer('http://{s}.google.com/vt/lyrs=m&x={x}&y={y}&z={z}', {
maxZoom: 20,
subdomains: ['mt0', 'mt1', 'mt2', 'mt3'],
detectRetina: true
});
googleHybrid = tileLayer('http://{s}.google.com/vt/lyrs=s,h&x={x}&y={y}&z={z}', {
maxZoom: 20,
subdomains: ['mt0', 'mt1', 'mt2', 'mt3'],
detectRetina: true
});
options = {
layers: [ this.googleMaps ],
zoom: 3,
center: latLng(0, 0)
};
layersControl = {
baseLayers: {
"Google": this.googleMaps,
"Hybrid": this.googleHybrid
},
overlays: {
"China Polygon": this.china
}
};
}
When I run the app with the above code, the Google basemap displays nicely, but the layersControl panel is absent. When I remove the entire overlays
portion of the layersControl code, the control panel shows up as expected. There's something I'm missing about loading/displaying the China regions geoJSON. Note that in import { adm1 } from '../assets/china_adm1
, I've arbitrarily defined adm1
, so maybe that's the issue. I've also tried including this.china
in the layers options bside this.googleMaps
.
In leaflet.js, it's as simple as L.geoJson(adm1).addTo(map);
where I've defined adm1
with
var adm1 = {"type": "FeatureCollection",
"name": "china_adm1", ...}
in the geojson file. I'm not sure how to properly define and reference the geojson file in ngx-leaflet.