如何解析geojson文件 [英] How to parse geojson file

查看:727
本文介绍了如何解析geojson文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我必须解析java / javascript中的geojson文件并将坐标放在谷歌地图上。

我的问题是解析文件。

任何人都可以帮助我吗?

下面我发布了部分文件。

非常感谢。

 {  crs :{  type  name  properties:{  name  urn:ogc:def:crs:EPSG :: 4326}},  type  FeatureCollection  features:[{ 几何:{  type:  Polygon  coordinates:[[[ 9 。< span class =code-digit> 0114910478323 , 45  35880131440966 ],[< span class =code-digit> 9 。 014491488013135  45  35880097314403 ],[ 9  0144909480813  45  35668565341486 ],[ 9  011490619692509  45  356685994655464 ],[ 9  0114910478323  45  35880131440966 ]]]},  type 功能  id 0   properties:{  cellId 1 }},{  geometry:{  type 多边形  coordinates:[[[ 9  014491488013135  45  35880097314403 ],[ 9  017491928134044  45  358800553060284  ],[ 9  017491276410173  45  35668523336193 ],[ 9  0144909480813  45  35668565341486 ],[ 9  014491488013135  45  35880097314403 ]] ]},  type 功能  id:< span class =code-digit> 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 。< span class =code-digit> 017491928134044 , 45  358800553060284 ]]]} ,  type 功能  id 2   properties:{  cellId 3 }},{  geometry:{  type  Polygon  coo rdinates:[[[ 9  02049236818262  45  35880005415845 ],[ 9  023492808146456  45  35879947643852 ],[ 9  023491932838542  45  35668415681913 ],[ 9  020491604666724  45  356684734496675 ],[ 9  02049236818262  45  35880005415845 ]]]},  type  Feat ure  id 3   properties:{  cellId 4 }},{   geometry:{  type 多边形  coordinates:[[[ 9  023492808146456  45  35879947643852 ],[ 9  026493248013145  45  35879881990051  ],[ 9 。< span class =code-digit> 02649226091323 , 45  35668350032926 ],[< span class =code-digit> 9 。 023491932838542  45  35668415681913 ],[ 9  023492808146456  45  35879947643852 ]]]},

......



我尝试了什么:



i会有这样的东西:

(此示例有效但源文件不同)

 <  !DOCTYPE     html  >  
< html >
< head >

/ *始终明确设置地图高度以定义包含地图的div
*元素的大小。 * /
#map {
身高:100%;
}
/ *可选:使示例页面填满窗口。 * /
html,正文{
身高:100%;
保证金:0;
填充: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'
});

// 创建< script>标记并设置USGS URL作为源。
var script = document .createElement(' script');
// 此示例使用存储在
<的 // http://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/2.5_week.geojsonp
script.src = ' https://developers.google.com/maps/documentation/的JavaScript /示例/ JSON / earthquake_GeoJSONP.js' ;
document .getElementsByTagName(' head')[ 0 ]。appendChild(script);
}

// 遍历结果数组并为每个数组放置一个标记
// 坐标集。
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 延迟

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

解决方案

我认为你需要 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[^].


这篇关于如何解析geojson文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆