2015-03-05 2 views
0

Im немного новичок в workng с OpenLayers 3.OpenLayers 3 Попробуйте загрузить GeoJSON объект

им пытаются загрузить объект GeoJSON в OpenLayers 3, но отображать центр только точка [0,0], которые даже не в моем объекте geojson, где im i идет не так.

код ниже:

//CREATE A BASE LAYER 
     var raster = new ol.layer.Tile({ 
      source: new ol.source.OSM() 
     }); 

     // create a source from the feature 
     var source = new ol.source.GeoJSON(

      ({ 
        object: 
         { 
          'type': 'FeatureCollection', 
          'crs': { 
           'type': 'name', 
           'properties': {'name': 'EPSG:3857'} 
          }, 
          'features': [ 
           { 
            'type': 'Feature', 
            //'properties': {'id': 63}, 
            'geometry': { 
             'type': 'Point', 
             'coordinates': [12, 12] 
            } 
           }, 
           { 
            'type': 'Feature', 
            'properties': {'id': 62}, 
            'geometry': { 
             'type': 'Point', 
             'coordinates': [35.0, 1.0]} 
           }, 
           { 
            'type': 'Feature', 
            'properties': {'id': 61}, 
            'geometry': { 
             'type': 'Point', 
             'coordinates': [34.0, 0.0]} 
           }, 
           { 
            'type': 'Feature', 
            'properties': { 
             'id': 56 
            }, 
            'geometry': { 
             'type': 'Point', 
             'coordinates': [33.0, 33.0] 
            } 
           } 
          ] 
         } 
      }) 
     ); 


     // CREATE THE LAYER WITH THE DATA 
     var vectorLayer = new ol.layer.Vector({ 
      source: source 
     }); 

     // create an openlayers map object 
     var map = new ol.Map({ 
      target: attrs.id, 
      layers: [raster,vectorLayer], 
      view: new ol.View({ 
       //projection:'EPSG:4326', 
       center:[0,0], 
       zoom:2 
      }) 
     }); 
    } 

ответ

2

я понял это, я должен был сделать перепроецирование моего объекта GeoJSON,

openlayers3 по умолчанию использует EPSG: 3857, так что делает мою базовую карту, которая является OpenStreetMap т.е. проектируется система координат, поэтому я должен был повернуться в формате GeoJSON к проектируемой системе, поскольку она была наступающем при помощи долготы и широты, т.е. географической системы координат (GeoJSON всегда espg: 4326).

var source = new ol.source.GeoJSON({ 
        object:{ 
         'type': 'FeatureCollection', 
         'crs': { 
          'type': 'name', 
          'properties': {'name': 'EPSG:4326'} 
         }, 
         'features': [ 
          { 
           'type': 'Feature', 
           //'properties': {'id': 63}, 
           'geometry': { 
            'type': 'Point', 
            'coordinates': [12, 12] 
           } 
          }, 
          { 
           'type': 'Feature', 
           'properties': {'id': 62}, 
           'geometry': { 
            'type': 'Point', 
            'coordinates': [35.0, 1.0]} 
          }, 
          { 
           'type': 'Feature', 
           'properties': {'id': 61}, 
           'geometry': { 
            'type': 'Point', 
            'coordinates': [34.0, 0.0]} 
          }, 
          { 
           'type': 'Feature', 
           'properties': { 
            'id': 56 
           }, 
           'geometry': { 
            'type': 'Point', 
            'coordinates': [33.0, 33.0] 
           } 
          } 
         ] 
        }, 
        projection: 'EPSG:3857' 
      } 
     ); 
+0

Спасибо за это, помог мне понять, что у меня была та же проблема, повторная проекция ** обрабатывается автоматически для источника URL, но не для источника объекта **. То есть для 'var vectorSource = new ol.source.Vector ({url:" ./data_inventory.geojson ", format: new ol.format.GeoJSON()})', но не 'var vectorSource = new ol.source.Vector ({ Функции: (новый ol.format.GeoJSON()). readFeatures (data)}); 'где данные просто' ./data_inventory.geojson "" вставлены. –

Смежные вопросы