Alright so I have an array of 9,000 objects being returned through HttpClient from my django rest api. These objects are Geojson format already. How would I turn this into a layer to map using ngx-leaflet. I was easily able to figure this out with basic javascript but I am confused doing it inside of angular. I previously had done it all inside of GeoDjango but now I have seperated the front and the back.
Here is my code for my component:
import { Component, OnInit } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Layer, tileLayer, geoJSON, LayerOptions } from 'leaflet';
import 'leaflet-providers';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {
layers: Layer[];
layersControl: any;
center = [47.215282, -109.483294];
fitBounds = [[49.378264, -116.492570], [44.757856, -103.528704]];
constructor(private http: HttpClient) { }
ngOnInit() {
this.http.get<any>('http://127.0.0.1:8000/data/people')
.subscribe(geo1 => {
this.http.get<any>('http://127.0.0.1:8000/data/places')
.subscribe(geo2 => {
let defaultBaseLayer = tileLayer.provider('OpenStreetMap.Mapnik');
let defaultOverlay = geoJSON(geo1);
this.layers = [
defaultBaseLayer,
defaultOverlay
];
this.layersControl = {
baseLayers: {
'OpenStreetMap Mapnik': defaultBaseLayer,
'OpenStreetMap BlackAndWhite': tileLayer.provider('OpenStreetMap.BlackAndWhite')
},
overlays: {
'Overlay One': defaultOverlay,
'Overlay Two': geoJSON(geo2)
}
};
console.log(geo1)
});
});
}
I borrowed the code from a previous response on this forum.
Edit: If i do people/1/ for id 1 I get the error "Invalid GeoJSON object." I am testing this using Postman and I am using cors-headers for django. I'm using a PostGIS database and used the geojson serializers in djangorestframework-gis
If I get rid of the geoJSON in front of (geo1) it says "The provided object is not a Layer."
EDIT 2: Does anybody know an alternative to Leaflet that works well with Angular 6?