简体   繁体   English

宣传单张 - 导入Geojson - Angular 6

[英]Leaflet - import Geojson - Angular 6

i try to import GeoJson file to leaflet in angular's app 6. 我尝试将GeoJson文件导入angular的应用程序6中的传单。

With this solution my geojson is drawn in leafletmap but i have this error and i can't build my app. 使用此解决方案我的geojson是在leafletmap中绘制但我有这个错误,我无法构建我的应用程序。 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 错误TS2345类型'{“类型”的参数::string;“features”:( {“type”:string;“geometry”:{“type:string:”coordinates“:num ...'不是可分配的类型参数GeoJsonObject

Model.ts Model.ts

export const Pdl = [ 'my geojson' ];

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

Component.ts 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.component.html',
   styleUrls: ['./geo.component.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.com/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 ? 是什么方式?

You need to do it the 'angular way' since you are using ngx-leaflet 因为你正在使用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. 通过导入导入json是我个人认为的噩梦,所以我要做的是在加载地图时使用get请求获取它,然后获取对地图对象的引用并添加geojson。

template: 模板:

import { HttpClient } from '@angular/common/http';
import * as L from 'leaflet';
..
map: L.Map;
json;
options = {
    layers: [
        L.tileLayer('http://{s}.tile.openstreetmap.org/{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 演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM