1

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>
MrUpsidown
  • 21,592
  • 15
  • 77
  • 131
DanBrum
  • 419
  • 1
  • 7
  • 18
  • What about if you use a relative path to your file, instead of the full url (`http://localhost/...`)? – MrUpsidown Apr 04 '14 at 15:20
  • Yeh I tried that also, that didn't work either. – DanBrum Apr 04 '14 at 15:28
  • Are you sure of the path you are using? What does your debugging console say? – MrUpsidown Apr 04 '14 at 15:29
  • 100% sure of the path, I can browse to it in my browser. – DanBrum Apr 04 '14 at 15:37
  • Anything in the console? Any error? – MrUpsidown Apr 04 '14 at 15:37
  • I haven't got debugging working properly yet, I've never done javascript before and I'm using visual studio so the output at the moment is just the microsoft dlls loading. – DanBrum Apr 04 '14 at 15:38
  • Run your code in Chrome or Firefox. Chrome has a good debugging console. Or download Firebug for firefox. – MrUpsidown Apr 04 '14 at 15:39
  • It says Failed to load resource: the server responded with a status of 404 (Not Found) – DanBrum Apr 04 '14 at 15:49
  • But if I go to http://localhost/WebApp/ in the browser the file test.json is there, however if I click on it I get HTTP Error 404.3 - Not Found The page you are requesting cannot be served because of the extension configuration. If the page is a script, add a handler. If the file should be downloaded, add a MIME map. – DanBrum Apr 04 '14 at 15:50
  • Do you use any server side language? ASP? IIS server? – MrUpsidown Apr 04 '14 at 15:54
  • I'm running it on IIS Server but I'm not actually programming anything on the server – DanBrum Apr 04 '14 at 15:57
  • 1
    Try check this: http://stackoverflow.com/questions/19516829/allow-loading-of-json-files-in-visual-studio-express-2013-for-web – MrUpsidown Apr 04 '14 at 15:57
  • Yes, that worked, thanks! How can I credit you in my answer? – DanBrum Apr 04 '14 at 16:05

1 Answers1

2

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

Community
  • 1
  • 1
MrUpsidown
  • 21,592
  • 15
  • 77
  • 131