OpenLayers学习笔记2——坐标转换问题

参照别人的添加marker的demo来改造时,发现无论怎样更改经纬度,都是停留在同一个位置。过了一两天突然想起可能是坐标参考的问题,尝试搜了一下,果然是这个问题。问题是这样子的:

WMTS中地图的坐标参考系是102100(具体是哪个不清楚),如下图所示:

而我在初始化地图时设置的参数如下图所示:projection属性设置的是‘EPSG:102100’,displayProjection属性设置的是‘EPSG:4326’,也即经纬度显示。

 

projection

{String} Set in the map options to specify the default projection for layers added to this map.  When using a projection other than EPSG:4326 (CRS:84, Geographic) or EPSG:3857 (EPSG:900913, Web Mercator), also set maxExtent, maxResolution or resolutions.  Default is “EPSG:4326”.  Note that the projection of the map is usually determined by that of the current baseLayer (see baseLayer and getProjectionObject).

displayProjection

{OpenLayers.Projection} Requires proj4js support for projections other than EPSG:4326 or EPSG:900913/EPSG:3857.  Projection used by several controls to display data to user.  If this property is set, it will be set on any control which has a null displayProjection property at the time the control is added to the map.


 

因此如果要利用经纬度进行定位标记的话,需要进行坐标转换,即EPSG:4326—>EPSG:102100,主要用到OpenLayers.LonLat的transform函数,代码如下:

 

[javascript]  view plain  copy
 
 print?
  1. //坐标转换  
  2. function corTransform(lon, lat) {  
  3.     var proj = new OpenLayers.Projection("EPSG:4326");  
  4.     var lonlat = new OpenLayers.LonLat(lon, lat);  
  5.     lonlat.transform(proj, this.map.getProjectionObject());  
  6.     return lonlat;  
  7.   
  8. }  
OpenLayers.LonLat的transform函数代码:

 

 

[javascript]  view plain  copy
 
 print?
  1. /** 
  2.  * APIMethod: transform 
  3.  * Transform the LonLat object from source to dest. This transformation is 
  4.  *    *in place*: if you want a *new* lonlat, use .clone() first. 
  5.  * 
  6.  * Parameters: 
  7.  * source - {<OpenLayers.Projection>} Source projection. 
  8.  * dest   - {<OpenLayers.Projection>} Destination projection. 
  9.  * 
  10.  * Returns: 
  11.  * {<OpenLayers.LonLat>} Itself, for use in chaining operations. 
  12.  */  
  13. transform : function (source , dest) {  
  14.     var point = OpenLayers.Projection.transform(  
  15.         {'x' : this.lon , 'y' : this.lat} , source , dest);  
  16.     this.lon = point.x;  
  17.     this.lat = point.y;  
  18.     return this;  
  19. } ,  
通过查看源代码会发现它调用的是OpenLayers.Projection的transform方法,有兴趣的可以一步一步看到转换的源码,这里就不赘述了

 

 
2

转载于:https://www.cnblogs.com/telwanggs/p/6775168.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值