-2

尝试将GeoJson 加载到我的地图时出现 400 错误。 该文件位于我的 src/assets 文件夹中。如果我在该文件夹中放置了一个不同的文件,我可以在启动 ng 服务器时通过 http 请求访问它,这意味着我应该可以访问这些文件。但是对于我在 loadGeoJson 方法调用中指定的文件,如果我尝试在浏览器中显示它,浏览 http://localhost:4200/assets/sample-farms.geojson器会显示我的应用程序的主屏幕......

所以发生了一些奇怪的重定向?

这是我的代码:

import { analyzeAndValidateNgModules } from '@angular/compiler';
import { Component } from '@angular/core';

@Component({
  selector: 'app-root',
  templateUrl: 'app.component.html',
  styleUrls: ['app.component.scss'],
})
export class AppComponent {
  title = 'simple-gmaps-demo';

  map: any; // important to declare the type of property map, so we can refer to it with this.map
  features: any;  // Do I need this too?
  

  onMapReady(map: google.maps.Map) {

    this.map = map;
    this.map.setCenter({lat:-32.910, lng:117.133});
    this.map.setZoom(10); 

    // Error in this method call?
    this.map.data.loadGeoJson('http://localhost:4200/assets/sample-farms.geojson', {}, 
    function (features: any) {
    console.log(features);
  });

  }
}

这是我在控制台中得到的错误:

zone-evergreen.js:2845 GET http://localhost:4200/assets/sample-farms.geojson 404 (Not Found)

非常感谢任何帮助。

4

1 回答 1

1

您应该先尝试将该网址放在地址栏中,以检查该文件是否存在。如果是这样,那么我猜这是地图 API 如何导入数据的问题(参考 API:https ://developers.google.com/maps/documentation/javascript/reference/data#Data.loadGeoJson )

我建议使用 Angular 自己的 HTTP 客户端 - https://angular.io/guide/http(或任何其他首选客户端)导入文件,然后直接通过该addGeoJson()方法传递该数据。

于 2020-12-13T07:30:42.253 回答