javascript - 加载 GeoJson Google API - CORS 政策

标签 javascript google-maps google-api

我正在尝试使用 Google map API 加载一些 JSON。

https://developers.google.com/maps/documentation/javascript/datalayer

这是我在 fiddle 上的代码:

https://jsfiddle.net/simonrenauld/3z7peqdv/6/

<script src="https://storage.cloud.google.com/jsontestdemp/testdata.jsonp"></script>

我尝试像文档中那样加载为 geojsonp

我仍然无法加载到我的 map 中。避免 CORS 政策的简单解决方案是什么?

最佳答案

根据 Google 的 documentation :

Note: In order to load a json file from another domain, that domain must have enabled Cross-origin resource sharing.

否则,从同一域加载它。例如。看看这个工作codesandbox从与 index.html 位于同一根级别的文件 (myjson.json) 加载 Google 的 GeoJSON 示例。

var map;
function initMap() {
    map = new google.maps.Map(document.getElementById("map"), {
        zoom: 4,
        center: { lat: -28, lng: 137 }
    });

    map.data.loadGeoJson("myjson.json");
}

希望这有帮助!

关于javascript - 加载 GeoJson Google API - CORS 政策,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59499454/

相关文章:

javascript - 想要阻止在 jQuery UI AutoComplete 中选择特定项目

json - 使用 VBA 为 Google Calendar API V3 构建 JSON

reactjs - react native MapViewDirections : force entire route to remain in view?

javascript - 如何在谷歌地图上绘制数百万个点?

google-maps - 相机动画和用户相机移动之间的区别

google-api - youtube api 和 google people api 和品牌帐户的问题

android - Google hangouts v2.3 增加语音通话功能

javascript - 可调整大小的 html 元素

javascript - Ember JS,在状态为 root.loaded.updated.uncommitted 的 Transformer 处理事件 `didCommit` 中解析数据时出错。”

javascript - React Native Samsung Universal Link 升级到 Android 12 后停止工作