Monday, 11 March 2019

javascript - Add GeoJSON layer to OpenLayers 3


I have a GeoJSON file called mygeojson.json and I want to add it as a layer in OpenLayers 3 on top of an openstreetmap layer. So far I could display the openstreetmap world including zoom etc. but for some reason I can't get the mygeojson.json on it.


The geojson contains many polygons and looks like this:


{

"type": "FeatureCollection",
"crs": { "type": "name", "properties": { "name": "urn:ogc:def:crs:OGC:1.3:CRS84" } },

"features": [
{ "type": "Feature", "properties": { "DN": 2 }, "geometry": { "type": "Polygon", "coordinates": [ [ [ 13.559093915055664, 52.545214330050563 ], [ 13.559633429050496, 52.545205649772548 ], [ 13.559633415380715, 52.545214636296755 ], [ 13.559093915055664, 52.545214330050563 ] ] ] } }
]
}

my main.html:








OpenLayers 3 example




My Map








I also tried removing the projection info but no use.



Answer



When you define your vector source, put the projection setting pointing to the target coordinate reference system (see the docs):


new ol.layer.Vector({
title: 'added Layer',
source: new ol.source.GeoJSON({
projection : 'EPSG:3857',
url: 'mygeojson.json'
})
})


Look at this example (using your sample data): http://jsfiddle.net/zzahmbff/4/


Perhaps this resource can help you to see different ways to load vector data: http://acanimal.github.io/thebookofopenlayers3/chapter03_03_vector_source.html


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...