Loading GeoJson Google API - CORS policy

若如初见. 提交于 2020-01-16 19:33:10

问题


I am trying to load some JSON with Google maps API.

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

This is my code on fiddle:

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

I am still not able to load into my map. What's the easy solution here to avoid the CORS policy?


回答1:


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.

Otherwise, load it from the same domain. E.g. 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.

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!



来源:https://stackoverflow.com/questions/59499454/loading-geojson-google-api-cors-policy

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!