我正在使用 ngx-leaflet 演示尝试从获取请求中获取 GeoJson 以显示在 LeafLet Map 上。我已经使用 http://geojson.io/ 构建了一个有效的 GeoJson,并使用http://geojsonlint.com/仔细检查了它 (感谢那些工具家伙)
我们在编译或控制台日志中显示没有错误。服务很好,但我们的 geojson 对象无处可寻。
我只是想在地图上显示 geojson 数据。任何帮助或建议表示赞赏。
Angular CLI: 1.6.5
Node: 8.3.0
OS: darwin x64
Angular: 5.2.1
... common, compiler, compiler-cli, core, forms, http
... language-service, platform-browser, platform-browser-dynamic
... router
@angular/animations: 5.2.2
@angular/cdk: 5.1.1
@angular/cli: 1.6.5
@angular/material: 5.1.1
@angular-devkit/build-optimizer: 0.0.41
@angular-devkit/core: 0.0.28
@angular-devkit/schematics: 0.0.51
@ngtools/json-schema: 1.1.0
@ngtools/webpack: 1.9.5
@schematics/angular: 0.1.16
typescript: 2.5.3
webpack: 3.10.0
<div leaflet style="height: 600px;"
[leafletOptions]="options"
[leafletLayers]="layers"
[leafletLayersControl]="layersControl">
</div>
状态组件
import { Component, OnInit } from '@angular/core';
import { StateService } from "../state.service";
import {tileLayer, Layer, latLng} from "leaflet";
import {HttpClient} from "@angular/common/http";
import 'rxjs/add/operator/catch';
import 'rxjs/add/operator/map';
import { LeafletLayersDemoModel } from './layers-demo.model';
import * as L from 'leaflet';
@Component({
selector: 'app-state',
templateUrl: './state.component.html',
styleUrls: ['./state.component.css']
})
export class StateComponent implements OnInit {
public geo_json_data;
constructor(private state_service:StateService, public http:HttpClient) {
this.apply();
}
LAYER_OSM = {
id: 'openstreetmap',
name: 'Open Street Map',
enabled: false,
layer: tileLayer('http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
maxZoom: 18,
attribution: 'Open Street Map'
})
};
geoJSON = {
id: 'geoJSON',
name: 'Geo JSON Polygon',
enabled: true,
layer: L.geoJSON(this.geo_json_data)
};
model = new LeafletLayersDemoModel(
[ this.LAYER_OSM],
this.LAYER_OSM.id,
[ this.geoJSON ]
);
layers: Layer[];
layersControl = {
baseLayers: {
'Open Street Map': this.LAYER_OSM.layer
},
overlays: {
GeoJSON: this.geoJSON.layer
}
};
options = {
zoom: 6,
center: latLng(41.2033, -74.2179)
};
apply() {
// Get the active base layer
const baseLayer = this.model.baseLayers.find((l: any) => (l.id === this.model.baseLayer));
// Get all the active overlay layers
const newLayers = this.model.overlayLayers
.filter((l: any) => l.enabled)
.map((l: any) => l.layer);
newLayers.unshift(baseLayer.layer);
this.layers = newLayers;
return false;
}
ngOnInit() {
console.log(this.state_service.state_id);
this.http.get('http://localhost:4200/assets/data/pa.geojson')
.subscribe((data) => {
this.geo_json_data = data;
console.log(this.geo_json_data);
},
error => {
console.log(error.text());
alert('GEO JSON GET FAILED');
});
}
}