![](/img/trans.png)
[英]loading a local GeoJSON file and using it with the Google Maps Javascript API v3 data layer
[英]Error when Calling Local GeoJSON File in Google Maps API
由於 Google Fusion Tables 已關閉,我正在嘗試使用 JSON 文件重新創建數據層(與美國縣)。
這是我的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>
當我運行頁面時,地圖可以工作,但我只在 JSON 文件的第二行收到一條錯誤消息:“Uncaught SyntaxError: Unexpected token ':'”。
有誰知道我能做些什么來解決這個問題? 我瀏覽了谷歌的頁面,但沒有找到解決方案。 僅供參考,我基本上沒有使用 JSON 的經驗,但還沒有找到另一種使用 Google Maps API 輕松傳達大型數據集的方法。
JSON 文件在這里,以防萬一有人想看: https : //www.estherloui.se/projects/sheet/gz_2010_us_050_00_20m.json
您可以使用loadGeoJson
將GeoJSON數據添加到您的地圖。 請參閱下面的代碼:
<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>
截屏:
希望這可以幫助!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.