Angular 400尝试在Google地图项目中加载GeoJSON时出错
尝试将GeoJSON加载到我的地图时出现400错误。 该文件位于我的src/assets文件夹中。 如果我在那个文件夹中放了一个不同的文件,我可以在启动ng服务器时通过http请求访问它,这意味着我应该可以访问这些文件。 但是对于我在loadGeoJson方法调用中指定的文件,如果我尝试使用Angular 400尝试在Google地图项目中加载GeoJSON时出错,angular,google-maps-api-3,geojson,http-status-code-400,agm,Angular,Google Maps Api 3,Geojson,Http Status Code 400,Agm,尝试将GeoJSON加载到我的地图时出现400错误。 该文件位于我的src/assets文件夹中。 如果我在那个文件夹中放了一个不同的文件,我可以在启动ng服务器时通过http请求访问它,这意味着我应该可以访问这些文件。 但是对于我在loadGeoJson方法调用中指定的文件,如果我尝试使用 http://localhost:4200/assets/sample-farms.geojson浏览器显示我的应用程序的主屏幕 所以有一些奇怪的重定向发生了 这是我的密码: import { analyz
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)
非常感谢您的帮助。您应该首先尝试将该url放在地址栏中,以检查文件是否存在。如果是这样的话,我猜这是一个maps API如何导入数据的问题(API for ref:)
我建议使用angular自己的HTTP客户端(或任何其他首选客户端)导入该文件,然后直接通过
addGeoJson()
方法传递该数据。Yes由于某种原因,即使路径正确,该文件实际上并不存在。。。现在使用复制并重命名的同一文件。如果我发现它无法解析该url的原因,将进行更新!真的很奇怪,这可能是一个大小写问题——如果文件名中存在不匹配的大写字符,或者可能没有在git中正确跟踪,这也会导致问题。但很好,至少你能让事情顺利进行!也许是因为没有在git中跟踪。。。谢谢你的帮助,克里斯!