我对这个问题有点绝望。我想在我的地图标记中显示来自 GeoJSON(一个有效的 GeoJson,它在 geojson.io 中工作)。这似乎很容易,但我看不出我的错误在哪里。我正在使用ngx-leaflet
,我的代码是这样的:
map.component.html
<div leaflet
[leafletOptions]="leafletOptions"
[leafletBaseLayers]="baseLayers"
[leafletLayersControlOptions]="layersControlOptions"
(leafletMapReady)="onMapReady($event)">
</div>
map.component.ts
import { Component, OnInit } from '@angular/core';
import { MapService } from './services';
import * as L from 'leaflet';
import { Alteracion } from './dto/alteracion';
const layOsm: L.TileLayer = L.tileLayer('http://{s}.tile.osm.org/{z}/{x}/{y}.png', {
maxZoom: 19,
attribution: false,
detectRetina: true
});
@Component({
selector: 'map',
templateUrl: './map.component.html',
styleUrls: ['./map.component.scss']
})
export class MapComponent implements OnInit {
leafletOptions: L.MapOptions = {
zoom: 6,
center: L.latLng(40.4166395, -3.7046087)
};
baseLayers: {[layerName: string]: L.Layer} = {
'OSM': layOsm
};
layersControlOptions: L.ControlOptions = { position: 'topright' };
constructor(private mapService: MapService) { }
ngOnInit() { }
onMapReady(map: L.Map) {
// L.Icon.Default.imagePath = 'assets/img/theme/vendor/leaflet/';
setTimeout(() => {
map.invalidateSize();
}, 0);
let alteraciones: any[];
this.mapService.getListAlteraciones(11, 30).subscribe(
result => {
alteraciones = result;
},
err => console.log(err)
);
L.geoJSON(alteraciones, {pointToLayer: function (feature, latlng) {
return L.marker([latlng]);
}
}).addTo(map);
}
}
我在编译或 console.log 中没有收到任何错误,但我不知道我做错了什么。我尝试复制我在官方仓库中找到的一些示例,但我无法做到。任何帮助,将不胜感激。提前致谢!
编辑 这是来自服务器的 GeoJson 的一部分:
{"type":"FeatureCollection","features":[{"type":"Feature","geometry":{"type":"Point","coordinates":[-6.40463,36.680111]},"properties":{"id_alteracion":"11_3210","tipo_alteracion":"11"}},{"type":"Feature","geometry":{"type":"Point","coordinates":[-6.374478,36.658933]},"properties":{"id_alteracion":"11_3127","tipo_alteracion":"14"}},{"type":"Feature","geometry":{"type":"Point","coordinates":[-6.359779,36.619801]},"properties":{"id_alteracion":"11_3172","tipo_alteracion":"11"}}...]}