在Openlayers中使用非内置的地图投影类型时,需要设置投影参数并注册该投影。如下代码给出了怎样使用EPSG:21781 。示例代码出处
import Map from 'ol/Map';
import View from 'ol/View';
import proj4 from 'proj4';
import {register} from 'ol/proj/proj4';
import {get as getProjection} from 'ol/proj';
// To use other projections, you have to register the projection in OpenLayers.
// This can easily be done with [http://proj4js.org/](proj4)
//
// By default OpenLayers does not know about the EPSG:21781 (Swiss) projection.
// So we create a projection instance for EPSG:21781 and pass it to
// register to make it available to the library for lookup by its
// code.
proj4.defs('EPSG:21781',
'+proj=somerc +lat_0=46.95240555555556 +lon_0=7.439583333333333 +k_0=1 ' +
'+x_0=600000 +y_0=200000 +ellps=bessel ' +
'+towgs84=660.077,13.551,369.344,2.484,1.783,2.939,5.66 +units=m +no_defs');
register(proj4);
const swissProjection = getProjection('EPSG:21781');
// we can now use the projection:
const map = new Map({
view: new View({
projection: swissProjection
// other view properties like map center etc.
})
// other properties for your map like layers etc.
});
如果只知道epsg代码,proj4.defs中的投影参数怎么确定?所有EPSG代码的信息都可在epsg.io网站查到。
下面给出查询EPSG:21781定义的过程。
第一步:进入EPSG.io网站,搜索EPSG代码
在查到的结果下方,点击Transform coordinates。
第二步:进入详情
点击More details.
第三步:查看proj4js定义
在新进入的页面中向下滚动,找到Export,Proj4js是用于Openlayers的投影语句。