admin管理员组

文章数量:1353265

i try to import GeoJson file to leaflet in angular's app 6.

With this solution my geojson is drawn in leafletmap but i have this error and i can't build my app. Someone know one solution ?

ERROR TS2345 Argument of type '{"type": string;"features":({"type": string; "geometry": { "type: string : "coordinates": num...' is not assignable parameter of type GeoJsonObject

Model.ts

export const Pdl = [ 'my geojson' ];

.geojson

Component.ts

import { LeafletModule } from '@asymmetrik/ngx-leaflet';
import * as L from 'leaflet';
import {Pdl} from "../models/pdl.model"; 

@Component({
   selector: 'app-geo',
   templateUrl: './geoponent.html',
   styleUrls: ['./geoponent.scss']
})
export class GeoComponent implements OnInit { 

    ngOnInit() {    
       var mapboxAccessToken = "...";

       const myfrugalmap = L.map('frugalmap').setView([47.482019, -1], 7);

       L.tileLayer('/{id}/{z}/{x}/{y}.png?access_token=' + mapboxAccessToken, {
          id: 'mapbox.light',
          attribution: 'SOS'
       }).addTo(myfrugalmap);

       L.geoJSON(Pdl).addTo(myfrugalmap);
    }
}

Maybe, i can hide the error ? what is the way ?

i try to import GeoJson file to leaflet in angular's app 6.

With this solution my geojson is drawn in leafletmap but i have this error and i can't build my app. Someone know one solution ?

ERROR TS2345 Argument of type '{"type": string;"features":({"type": string; "geometry": { "type: string : "coordinates": num...' is not assignable parameter of type GeoJsonObject

Model.ts

export const Pdl = [ 'my geojson' ];

https://raw.githubusercontent./alanent/france-geojson/master/regions/pays-de-la-loire/departements-pays-de-la-loire.geojson

Component.ts

import { LeafletModule } from '@asymmetrik/ngx-leaflet';
import * as L from 'leaflet';
import {Pdl} from "../models/pdl.model"; 

@Component({
   selector: 'app-geo',
   templateUrl: './geo.ponent.html',
   styleUrls: ['./geo.ponent.scss']
})
export class GeoComponent implements OnInit { 

    ngOnInit() {    
       var mapboxAccessToken = "...";

       const myfrugalmap = L.map('frugalmap').setView([47.482019, -1], 7);

       L.tileLayer('https://api.tiles.mapbox./v4/{id}/{z}/{x}/{y}.png?access_token=' + mapboxAccessToken, {
          id: 'mapbox.light',
          attribution: 'SOS'
       }).addTo(myfrugalmap);

       L.geoJSON(Pdl).addTo(myfrugalmap);
    }
}

Maybe, i can hide the error ? what is the way ?

Share Improve this question edited Feb 13, 2019 at 10:33 kboul 14.6k5 gold badges47 silver badges58 bronze badges asked Oct 16, 2018 at 9:50 al NTMal NTM 2571 gold badge6 silver badges15 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 8

You need to do it the 'angular way' since you are using ngx-leaflet

Importing a json via import is a nightmare in my personal opinion so what I would do would be to fetch it using a get request when the map is loaded and then get a reference to the map object and add the geojson.

template:

import { HttpClient } from '@angular/mon/http';
import * as L from 'leaflet';
..
map: L.Map;
json;
options = {
    layers: [
        L.tileLayer('http://{s}.tile.openstreetmap/{z}/{x}/{y}.png', { 
            maxZoom: 18, attribution: '...'
        })
    ],
    zoom: 7,
    center: L.latLng(47.482019, -1)
};

constructor(private http: HttpClient) { }

onMapReady(map: L.Map) {
    this.http.get('assets/departements.json').subscribe((json: any) => {
        console.log(json);
        this.json = json;
        L.geoJSON(this.json).addTo(map);
    });
}

template

<div leaflet style="height: 800px"
    [leafletOptions]="options"
    (leafletMapReady)="onMapReady($event)">
</div>

Demo

本文标签: javascriptLeafletimport GeojsonAngular 6Stack Overflow