简体   繁体   English

将本地主机上存储的GeoJSON文件加载到Google Maps JavaScript SDK中

[英]Loading a GeoJSON file stored on the local host into google maps javascript sdk

I am trying to display some JSON data in Google maps. 我正在尝试在Google地图中显示一些JSON数据。 I copied Google's default JSON file for testing so both files are exactly the same. 我复制了Google的默认JSON文件进行测试,因此两个文件完全相同。

When I load the file off Google's server then the data displays correctly: 当我从Google的服务器上加载文件时,数据会正确显示:

 map.data.loadGeoJson('https://storage.googleapis.com/maps-devrel/google.json');

However, when I load the data off my local host it does not display: 但是,当我从本地主机加载数据时,它不会显示:

map.data.loadGeoJson('http://localhost/WebApp/test.json');

Full app: 完整的应用程序:

<!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>

IIS Server needs a specific mime type decalred: IIS Server需要将特定的MIME类型降级:

<system.webServer>
  <staticContent>
    <mimeMap fileExtension=".json" mimeType="application/json" />
  </staticContent>
</system.webServer>

Credits to original answer: Allow loading of JSON files in Visual Studio Express 2013 for Web 原始答案表示感谢: 允许在Visual Studio Express 2013 for Web中加载JSON文件

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

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