Вопрос

I am trying to display some JSON data in Google maps. I copied Google's default JSON file for testing so both files are exactly the same.

When I load the file off Google's server then the data displays correctly:

 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:

<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

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top