且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何解析geojson文件

更新时间:2023-09-21 20:58:46

我认为你需要 JSON.parse() - JavaScript | MDN [ ^ ]。

Hi,
I have to parse a geojson file in java/javascript and put the coordinate on a google maps.
My problem is to parse the file.
Can anyone help me?
Below i posted part of file.
Thanks a lot.

{"crs": {"type": "name", "properties": {"name": "urn:ogc:def:crs:EPSG::4326"}}, "type": "FeatureCollection", "features": [{"geometry": {"type": "Polygon", "coordinates": [[[9.0114910478323, 45.35880131440966], [9.014491488013135, 45.35880097314403], [9.0144909480813, 45.35668565341486], [9.011490619692509, 45.356685994655464], [9.0114910478323, 45.35880131440966]]]}, "type": "Feature", "id": 0, "properties": {"cellId": 1}}, {"geometry": {"type": "Polygon", "coordinates": [[[9.014491488013135, 45.35880097314403], [9.017491928134044, 45.358800553060284], [9.017491276410173, 45.35668523336193], [9.0144909480813, 45.35668565341486], [9.014491488013135, 45.35880097314403]]]}, "type": "Feature", "id": 1, "properties": {"cellId": 2}}, {"geometry": {"type": "Polygon", "coordinates": [[[9.017491928134044, 45.358800553060284], [9.02049236818262, 45.35880005415845], [9.020491604666724, 45.356684734496675], [9.017491276410173, 45.35668523336193], [9.017491928134044, 45.358800553060284]]]}, "type": "Feature", "id": 2, "properties": {"cellId": 3}}, {"geometry": {"type": "Polygon", "coordinates": [[[9.02049236818262, 45.35880005415845], [9.023492808146456, 45.35879947643852], [9.023491932838542, 45.35668415681913], [9.020491604666724, 45.356684734496675], [9.02049236818262, 45.35880005415845]]]}, "type": "Feature", "id": 3, "properties": {"cellId": 4}}, {"geometry": {"type": "Polygon", "coordinates": [[[9.023492808146456, 45.35879947643852], [9.026493248013145, 45.35879881990051], [9.02649226091323, 45.35668350032926], [9.023491932838542, 45.35668415681913], [9.023492808146456, 45.35879947643852]]]}, 

......

What I have tried:

i would have something like this:
(this example works but it's different the source file)

<!DOCTYPE html>
<html>
  <head>
    
      /* Always set the map height explicitly to define the size of the div
       * element that contains the map. */
      #map {
        height: 100%;
      }
      /* Optional: Makes the sample page fill the window. */
      html, body {
        height: 100%;
        margin: 0;
        padding: 0;
      }
    
  </head>
  <body>
    <div id="map"></div>
    <script>
      var map;
      function initMap() {
        map = new google.maps.Map(document.getElementById('map'), {
          zoom: 2,
          center: new google.maps.LatLng(2.8,-187.3),
          mapTypeId: 'terrain'
        });

        // Create a <script> tag and set the USGS URL as the source.
        var script = document.createElement('script');
        // This example uses a local copy of the GeoJSON stored at
        // http://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/2.5_week.geojsonp
        script.src = 'https://developers.google.com/maps/documentation/javascript/examples/json/earthquake_GeoJSONP.js';
        document.getElementsByTagName('head')[0].appendChild(script);
      }

      // Loop through the results array and place a marker for each
      // set of coordinates.
      window.eqfeed_callback = function(results) {
        for (var i = 0; i < results.features.length; i++) {
          var coords = results.features[i].geometry.coordinates;
          var latLng = new google.maps.LatLng(coords[1],coords[0]);
          var marker = new google.maps.Marker({
            position: latLng,
            map: map
          });
        }
      }
    </script>
    <script async defer

    src="https://maps.googleapis.com/maps/api/js?key=AIzaSyC48RmJ9oeHDnU3CkggNiR7T4tbKeVEiUU&callback=initMap">
    </script>
     </body>
</html>

I think you need JSON.parse() - JavaScript | MDN[^].