Files
openlayers/examples/geojson.js
2013-11-20 11:41:54 +01:00

91 lines
2.1 KiB
JavaScript

goog.require('ol.Map');
goog.require('ol.RendererHint');
goog.require('ol.View2D');
goog.require('ol.layer.Tile');
goog.require('ol.layer.Vector');
goog.require('ol.source.GeoJSON');
goog.require('ol.source.OSM');
goog.require('ol.style.DefaultStyleFunction');
var styleFunction = function(feature) {
switch (feature.getGeometry().getType()) {
case ol.geom.GeometryType.MULTI_LINE_STRING:
return {
stroke: {
color: 'green',
width: 1
}
};
default:
return ol.style.DefaultStyleFunction(feature);
}
};
var geoJSONSource = new ol.source.GeoJSON(
/** @type {ol.source.GeoJSONOptions} */ ({
geoJSON: {
'type': 'FeatureCollection',
features: [
{
'type': 'Feature',
'geometry': {
'type': 'Point',
'coordinates': [0, 0]
}
},
{
'type': 'Feature',
'geometry': {
'type': 'LineString',
'coordinates': [[-1e7, -1e7], [1e7, 1e7]]
}
},
{
'type': 'Feature',
'geometry': {
'type': 'LineString',
'coordinates': [[-1e7, 1e7], [1e7, -1e7]]
}
},
{
'type': 'Feature',
'geometry': {
'type': 'Polygon',
'coordinates': [[[-5e6, -5e6], [0, 5e6], [5e6, -5e6]]]
}
},
{
'type': 'Feature',
'geometry': {
'type': 'MultiLineString',
'coordinates': [
[[-1e6, -7.5e5], [-1e6, 7.5e5]],
[[1e6, -7.5e5], [1e6, 7.5e5]],
[[-7.5e5, -1e6], [7.5e5, -1e6]],
[[-7.5e5, 1e6], [7.5e5, 1e6]]
]
}
}
]
}
}));
var map = new ol.Map({
layers: [
new ol.layer.Tile({
source: new ol.source.OSM()
}),
new ol.layer.Vector({
source: geoJSONSource,
styleFunction: styleFunction
})
],
renderer: ol.RendererHint.CANVAS,
target: 'map',
view: new ol.View2D({
center: [0, 0],
zoom: 2
})
});