简体   繁体   English

加载 GeoJson Google API - CORS 政策

[英]Loading GeoJson Google API - CORS policy

I am trying to load some JSON with Google maps API.我正在尝试使用 Google 地图 API 加载一些 JSON。

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

This is my code on fiddle:这是我的小提琴代码:

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

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

I have tried to load as geojsonp like in the documentation我试图像文档中一样加载为 geojsonp

I am still not able to load into my map.我仍然无法加载到我的地图中。 What's the easy solution here to avoid the CORS policy?这里避免 CORS 政策的简单解决方案是什么?

As per Google's documentation :根据谷歌的文档

Note: In order to load a json file from another domain, that domain must have enabled Cross-origin resource sharing .注意:为了从另一个域加载 json 文件,该域必须启用跨域资源共享

Otherwise, load it from the same domain.否则,从同一个域加载它。 Eg check out this working codesandbox that loads Google's GeoJSON sample from a file ( myjson.json ) located at the same root-level as index.html .例如,查看这个工作代码和框,它从与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");
}

Hope this helps!希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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