加载GeoJson Google API-CORS政策 [英] Loading GeoJson Google API - CORS policy
问题描述
我正在尝试使用Google Maps API加载一些JSON.
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>
我曾尝试像文档
我仍然无法加载到我的地图中.避免CORS政策的简单解决方案是什么?
I am still not able to load into my map. What's the easy solution here to avoid the CORS policy?
推荐答案
根据Google的
注意:为了从另一个域加载json文件,该域
必须启用跨源资源
共享.
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 ( 希望这会有所帮助! 这篇关于加载GeoJson Google API-CORS政策的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
myjson.json
)加载Google的GeoJSON示例与index.html
位于同一根目录.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");
}