我正在尝试使用 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/