繁体   English   中英

在 Google Maps API 中调用本地 GeoJSON 文件时出错

[英]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

您可以使用loadGeoJsonGeoJSON数据添加到您的地图。 请参阅下面的代码:

<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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM