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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM