Create ol.proj.Projection instances with additional properties

This commit is contained in:
Andreas Hocevar
2015-03-26 10:50:52 +01:00
parent 338901774b
commit a920cd94d9
2 changed files with 26 additions and 17 deletions

View File

@@ -2,7 +2,7 @@ goog.require('ol.Graticule');
goog.require('ol.Map'); goog.require('ol.Map');
goog.require('ol.View'); goog.require('ol.View');
goog.require('ol.layer.Vector'); goog.require('ol.layer.Vector');
goog.require('ol.proj'); goog.require('ol.proj.Projection');
goog.require('ol.source.GeoJSON'); goog.require('ol.source.GeoJSON');
@@ -11,11 +11,12 @@ proj4.defs('ESRI:53009', '+proj=moll +lon_0=0 +x_0=0 +y_0=0 +a=6371000 ' +
// Configure the Sphere Mollweide projection object with an extent, // Configure the Sphere Mollweide projection object with an extent,
// and a world extent. These are required for the Graticule. // and a world extent. These are required for the Graticule.
var sphereMollweideProjection = ol.proj.get('ESRI:53009'); var sphereMollweideProjection = new ol.proj.Projection({
sphereMollweideProjection.setExtent([ code: 'ESRI:53009',
-9009954.605703328, -9009954.605703328, extent: [-9009954.605703328, -9009954.605703328,
9009954.605703328, 9009954.605703328]); 9009954.605703328, 9009954.605703328],
sphereMollweideProjection.setWorldExtent([-179, -90, 179, 90]); worldExtent: [-179, -90, 179, 90]
});
var map = new ol.Map({ var map = new ol.Map({
keyboardEventTarget: document, keyboardEventTarget: document,

View File

@@ -5,21 +5,29 @@ goog.require('ol.control');
goog.require('ol.control.ScaleLine'); goog.require('ol.control.ScaleLine');
goog.require('ol.layer.Image'); goog.require('ol.layer.Image');
goog.require('ol.proj'); goog.require('ol.proj');
goog.require('ol.proj.Projection');
goog.require('ol.source.ImageWMS'); goog.require('ol.source.ImageWMS');
// Transparent Proj4js support: ol.proj.get() creates and returns a projection // Transparent Proj4js support:
// known to Proj4js if it is unknown to OpenLayers, and registers functions to //
// transform between all registered projections.
// EPSG:21781 is known to Proj4js because its definition was loaded in the html. // EPSG:21781 is known to Proj4js because its definition was loaded in the html.
// Note that we are getting the projection object here to set the extent. If // Now when we create an ol.proj.Projection instance with the 'EPSG:21781' code,
// you do not need this, you do not have to use ol.proj.get(); simply use the // OpenLayers will pick up parameters like units and transform functions from
// string code in the view projection below and the transforms will be // Proj4js.
// registered transparently. //
var projection = ol.proj.get('EPSG:21781'); // Note that we are setting the projection's extent here, which is used to
// The extent is used to determine zoom level 0. Recommended values for a // determine the view resolution for zoom level 0. Recommended values for a
// projection's validity extent can be found at http://epsg.io/. // projection's validity extent can be found at http://epsg.io/.
projection.setExtent([485869.5728, 76443.1884, 837076.5648, 299941.7864]); //
// If you use Proj4js only to transform coordinates, you don't even need to
// create an ol.proj.Projection instance. ol.proj.get() will take care of it
// internally.
var projection = new ol.proj.Projection({
code: 'EPSG:21781',
extent: [485869.5728, 76443.1884, 837076.5648, 299941.7864]
});
var extent = [420000, 30000, 900000, 350000]; var extent = [420000, 30000, 900000, 350000];
var layers = [ var layers = [
@@ -67,7 +75,7 @@ var map = new ol.Map({
target: 'map', target: 'map',
view: new ol.View({ view: new ol.View({
projection: projection, projection: projection,
center: ol.proj.transform([8.23, 46.86], 'EPSG:4326', 'EPSG:21781'), center: ol.proj.transform([8.23, 46.86], 'EPSG:4326', projection),
extent: extent, extent: extent,
zoom: 2 zoom: 2
}) })