OpenLayers中Map对象的投影参数初始化与坐标转换

OpenLayers中Map中默认使用的经纬度坐标系(EPSG:4326),如果我们地图 图片采用了墨卡托投影 时,我们就需要在初始化Map对象时对相关的参数进行配置,以使用正确的投影方式;

下面代码演示了这个使用:

SimpleMap = OpenLayers.Class(OpenLayers.Map,{ /*按地图引擎的地图参数初始画地图*/ initialize: function (div, options){ OpenLayers.Map.prototype.initialize.apply(this,[div,options]); //当前地图采用坐标系(墨卡托坐标) this.projection=new OpenLayers.Projection("EPSG:900913"); //数据 采用的坐标系 this.displayProjection=new OpenLayers.Projection("EPSG:4326"); this.units="m"; //this.numZoomLevels=16; //this.maxResolution=78271.51695; //this.numZoomLevels=15; //this.maxResolution=39135.758475; this.numZoomLevels=14; //Resolution=20037508*2/256*2^(zoom+1),zoom取值0~15 this.maxResolution= 19567.8792375; //对应经纬度(-180,-90,180*2,90) this.maxExtent=new OpenLayers.Bounds(-20037508, -20037508,20037508*2,20037508); this.restrictedExtent=new OpenLayers.Bounds(6679169.333,0,20037508*2,13358338.667); this.restrictedExtent=new OpenLayers.Bounds(-20037508, -20037508,20037508*2,13358338.667); this.addLayer(new SimpleTileCache("baseMap",TILE_PIV_URL,{isBaseLayer:true})); this.div.oncontextmenu = function () { return false;}; if(CENTER_LONLAT){ this.zoomToLonLat(new OpenLayers.LonLat(CENTER_LONLAT[0],CENTER_LONLAT[1]),DEFAULT_ZOOM); } }, /** * 定位到坐标点 输入点的采用的坐标系与显示坐标系相同 * lonlat 与显示坐标系相同的点 { OpenLayers.LonLat } * zoom 缩放级数 { Integer } */ zoomToLonLat: function(lonlat,zoom){ var p=lonlat.transform(this.displayProjection,this.getProjectionObject()); this.setCenter(p,zoom,false,true); }, CLASS_NAME:"SimpleMap" });


我们CENTER_LONLAT是一个经纬度坐标数组。
zoomToLonLat()函数展示了坐标转换。坐标转换需要Proj4j.js包,需要下载后才能进行做坐标转换。
zoomToLonLat函数就是把经纬度坐标转换为墨卡托坐标,this.getProjectionObject()返回的就是 this.projection。因为现在地图采用墨卡托坐标系,所有经纬度坐标都要调用Openlayers.LonLat对象的transform函数进行坐标转换。

你可能感兴趣的:(GIS)