简体   繁体   中英

Can't load geojson file

I'm trying to load a geoJson file on my map but it isn't working.
Here is my code :

{
  "type": "Feature",
  "geometry": {
    "type": "Polygon",
    "coordinates": [
      [
        [57.089460, -2.082101],
        [56.744047, -2.464766],
        [56.977826, -2.720955],
        [57.089460, -2.082101]
      ]
    ]
  }
}

In an initialize function :

var mapCanvas = document.getElementById('map');
var mapOptions = {
   center: new google.maps.LatLng(56.975012, -2.343829),
   zoom: 9,
   mapTypeId: google.maps.MapTypeId.ROADMAP
}
var map = new google.maps.Map(mapCanvas, mapOptions);

var localLayer = new google.maps.Data();
localLayer.loadGeoJson('js/coordinates.json');
localLayer.setMap(map);

Can you tell me what I am doing wrong if you see the mistake please. (the path to my json file is correct)

If I run your geoJSON through geojsonlint.com, it is invalid:

Invalid GeoJSON Line 1: "properties" property required

This validates (but the coordinates are backwards):

{
  "type": "Feature",
"properties": {},
  "geometry": {
    "type": "Polygon",
    "coordinates": [
      [
        [57.089460, -2.082101],
        [56.744047, -2.464766],
        [56.977826, -2.720955],
        [57.089460, -2.082101]
      ]
    ]
  }
};

with corrected coordinates:

proof of concept fiddle

code snippet:

 function initialize() { var mapCanvas = document.getElementById('map'); var mapOptions = { center: new google.maps.LatLng(56.975012, -2.343829), zoom: 9, mapTypeId: google.maps.MapTypeId.ROADMAP } var map = new google.maps.Map(mapCanvas, mapOptions); var localLayer = new google.maps.Data(); localLayer.addGeoJson(geoJson); localLayer.setMap(map); } google.maps.event.addDomListener(window, "load", initialize); var geoJson = { "type": "Feature", "properties": {}, "geometry": { "type": "Polygon", "coordinates": [ [ [-2.082101, 57.089460], [-2.464766, 56.744047], [-2.720955, 56.977826], [-2.082101, 57.089460] ] ] } }; 
 html, body, #map { height: 100%; width: 100%; margin: 0px; padding: 0px } 
 <script src="https://maps.googleapis.com/maps/api/js"></script> <div id="map"></div> 

A feature object must have a member with the name "properties". feature-objects .

So it is nullable and you can add "properties":null to your feature object.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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