【问题标题】:Leaflet - import Geojson - Angular 6传单 - 导入 Geojson - Angular 6
【发布时间】:2019-02-24 23:35:59
【问题描述】:

我尝试将 GeoJson 文件导入 Angular 应用程序 6 中的传单。

使用这个解决方案,我的 geojson 被绘制在小册子地图中,但我有这个错误,我无法构建我的应用程序。有人知道一种解决方案吗?

ERROR TS2345 类型参数 '{"type": string;"features":({"type": 细绳; “几何”:{“类型:字符串:”坐标“:num ...”不是 GeoJsonObject 类型的可赋值参数

模型.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

组件.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);
    }
}

也许,我可以隐藏错误?有什么办法?

【问题讨论】:

    标签: javascript angular leaflet geojson ngx-leaflet


    【解决方案1】:

    由于您使用的是 ngx-leaflet,因此您需要以“角度方式”进行操作

    在我个人看来,通过导入导入 json 是一场噩梦,所以我会在加载地图时使用 get 请求获取它,然后获取对地图对象的引用并添加 geojson。

    模板:

    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);
        });
    }
    

    模板

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

    Demo

    【讨论】:

    • 我试试这个,但现在我没有错误,但我的地图上没有 geojson。你能穿上stackblitz吗?
    • 我附上了一个 github repo 的链接。
    • 非常感谢你,我没有使用 ngx-leaflet 只是传单,所以我会去安装 ngx 传单
    • 没问题。并且要小心。不要发布您的 mapboxAccessToken,因为其他人可以使用它。我编辑了您的问题并将其删除。
    • 我一直在寻找一个简单的解决方案来解决这个问题,就是这样......你不会相信我见过多少愚蠢复杂的方法!
    猜你喜欢
    • 1970-01-01
    • 2017-06-21
    • 2015-08-11
    • 2018-09-23
    • 1970-01-01
    • 2019-07-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多