简体   繁体   English

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

[英]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 .您可以使用loadGeoJsonGeoJSON数据添加到您的地图。 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.

相关问题 加载本地GeoJSON文件,并将其与Google Maps Javascript API v3数据层一起使用 - loading a local GeoJSON file and using it with the Google Maps Javascript API v3 data layer 将本地主机上存储的GeoJSON文件加载到Google Maps JavaScript SDK中 - Loading a GeoJSON file stored on the local host into google maps javascript sdk 在谷歌地图 api 上加载 geojson - Load geojson on google maps api 谷歌地图api geoJSON - 没有出现描述框 - google maps api geoJSON - no description box appearing 在Google Maps API和GeoJSON上圈出“降雨”动画? - Circles 'rainfall' animation on Google Maps API and GeoJSON? Google Maps JS API / GeoJSON导入-插入API密钥时未显示信息窗口 - Google Maps JS API / GeoJSON import - infowindows not showing up when API Key is inserted 将图片从GeoJSON文件添加到信息窗口? Google Maps JavaScript API - Adding images to an info window from a GeoJSON file? Google maps javascript API 使用Google Maps API v3访问嵌套的geojson文件中的坐标以绘制多边形 - Accessing Coordinates in a nested geojson file to draw polygons with Google Maps API v3 在叠加层geojson图层上单击时,Google Maps API Map Click事件不起作用 - Google Maps API Map Click Event doesn't work when clicked on an overlay geojson layer 谷歌地图Geojson infowindow - Google maps Geojson infowindow
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM