[英]Error when Calling Local GeoJSON File in Google Maps API
Since Google Fusion Tables has been shut down, I'm trying to recreate a data layer (with US Counties) using a JSON file.由于 Google Fusion Tables 已关闭,我正在尝试使用 JSON 文件重新创建数据层(与美国县)。
Here is my JS:这是我的JS:
var map;
function initMap() {
map = new google.maps.Map(document.getElementById('map'), {
//center on the US
center: {lat: 39.8283, lng: -98.5795},
zoom: 5
});
var script = document.createElement('script');
//call json file of US counties
script.src = 'assets/sheet/places.json';
document.getElementsByTagName('head')[0].appendChild(script);
}
</script>
When I run the page, the map works but I get an error that says: "Uncaught SyntaxError: Unexpected token ':'" at only the second line of the JSON file.当我运行页面时,地图可以工作,但我只在 JSON 文件的第二行收到一条错误消息:“Uncaught SyntaxError: Unexpected token ':'”。
Does anyone know what I can do to fix this?有谁知道我能做些什么来解决这个问题? I've looked through pages of Google and haven't been able to find a solution.
我浏览了谷歌的页面,但没有找到解决方案。 FYI, I basically have no experience with JSON but haven't found another way to convey a large dataset easily with Google Maps API.
仅供参考,我基本上没有使用 JSON 的经验,但还没有找到另一种使用 Google Maps API 轻松传达大型数据集的方法。
The JSON file is here, in case anyone wants to see: https://www.estherloui.se/projects/sheet/gz_2010_us_050_00_20m.json JSON 文件在这里,以防万一有人想看: https : //www.estherloui.se/projects/sheet/gz_2010_us_050_00_20m.json
You can add GeoJSON data to your map using loadGeoJson
.您可以使用
loadGeoJson
将GeoJSON数据添加到您的地图。 See the code below:请参阅下面的代码:
<script>
var map;
function initMap() {
map = new google.maps.Map(document.getElementById('map'), {
//center on the US
center: { lat: 39.8283, lng: -98.5795 },
zoom: 5
});
map.data.loadGeoJson('assets/sheet/places.json');
}
</script>
Screenshot:截屏:
Hope this helps!希望这可以帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.