【发布时间】:2014-05-16 23:54:10
【问题描述】:
我正在尝试在 Google 地图中显示一些 JSON 数据。我复制了 Google 的默认 JSON 文件进行测试,因此两个文件完全相同。
当我从 Google 的服务器上加载文件时,数据会正确显示:
map.data.loadGeoJson('https://storage.googleapis.com/maps-devrel/google.json');
但是,当我从本地主机加载数据时,它不会显示:
map.data.loadGeoJson('http://localhost/WebApp/test.json');
完整应用:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>testMap</title>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no">
<meta charset="utf-8">
<style>
html, body, #map-canvas
{
height: 100%;
margin: 0px;
padding: 0px;
}
</style>
<script>
function initialize() {
var mapOptions = {
zoom: 8,
center: new google.maps.LatLng(-34.397, 150.644)
};
var map = new google.maps.Map(document.getElementById('map-canvas'),
mapOptions);
// map.data.loadGeoJson('https://storage.googleapis.com/maps-devrel/google.json');
map.data.loadGeoJson('http://localhost/WebApp/test.json');
}
function loadScript() {
var script = document.createElement('script');
script.type = 'text/javascript';
script.src = 'https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false&' +
'callback=initialize';
document.body.appendChild(script);
}
window.onload = loadScript;
</script>
</head>
<body>
<div id="map-canvas"></div>
</body>
</html>
【问题讨论】:
-
如果您使用文件的相对路径而不是完整的 url (
http://localhost/...) 会怎样? -
是的,我也试过了,也没用。
-
您确定使用的路径吗?你的调试控制台说什么?
-
100% 确定路径,我可以在浏览器中浏览到它。
标签: javascript json iis google-maps-api-3 geojson