参照别人的添加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函数,代码如下:

  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. }

OpenLayers.LonLat的transform函数代码:

  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

OpenLayers学习笔记2——坐标转换问题的更多相关文章

  1. OpenLayers学习笔记4——使用jQuery UI实现測量对话框

    OpenLayers学习最好的方式就是跟着其自带的演示样例进行学习,另外对web前端的开发设计要了解,慢慢积累,这样在一般的小项目中应该是足够用了. 本篇參照量測demo实现对话框形式的量測,抛砖引玉 ...

  2. openlayers 学习笔记一

    1. 创建地图,加载控件 var map = new OpenLayers.Map("map", { projection: new OpenLayers.Projection(& ...

  3. OpenLayers学习笔记5——使用jQuery UI实现查询并标注(UI篇)

    近期事情非常多,老板给的压力也非常大.经常出差,另外项目和个人研究还都要跟上,本月要交论文,还要写专利,仅仅能抽时间来学习其它的东西了. 关于OpenLayers的在博客中不会写太多详细的实现(网上有 ...

  4. OpenLayers学习笔记3——使用jQuery UI美化界面设计

    PC端软件在开发是有较多的界面库能够选择,比方DevExpress.BCG.DotNetBar等,能够非常方便快捷的开发出一些炫酷的界面,近期在学习OpenLayers.涉及到web前端开发,在设计界 ...

  5. openlayers 学习笔记之1

    1. 为Web Gis客户端开发的javascript 框架 百度文库中的教程:入门经典> 1) 初始化map: map = new OpenLayers.Map(mapContainerNam ...

  6. OpenLayers学习笔记(十)— 动态加载JSON数据模拟航迹线

    在openlayers 3 上,加载本地json数据,动态绘制航迹线,以飞机当前位置为地图中心,此例子是模拟DEMO 本文链接:动态加载JSON数据模拟航迹线 作者:狐狸家的鱼 GitHub:八至 前 ...

  7. OpenLayers学习笔记(九)— 限制地图显示范围

    openlayers 3 地图上限制地图显示及拖动范围,坐标系是4326转3857,中心经纬度精确到小数点后六位,减少误差 GitHub:八至 作者:狐狸家的鱼 本文链接:ol3-限制地图显示及拖动范 ...

  8. OpenLayers学习笔记(八)— 类似比例尺的距离环(二)

    openlayers 3 地图上创建一个距离环,始终以地图中心为中心,每个环之间的距离类似比例尺,随地图缩放而变化. 添加具有覆盖整个范围的特征的虚拟层,其可以被设置为围绕地图中心的环. 这篇是上一篇 ...

  9. OpenLayers学习笔记(七)— 类似比例尺的距离环(一)

    openlayers 3 地图上创建一个距离环,始终以地图中心为中心,每个环之间的距离类似比例尺,随地图缩放而变化. 添加具有覆盖整个范围的特征的虚拟层,其可以被设置为围绕地图中心的环.注意,根据地图 ...

随机推荐

  1. The Knuth-Morris-Pratt Algorithm in my own words(转)

    origianl For the past few days, I’ve been reading various explanations of the Knuth-Morris-Pratt str ...

  2. struts2学习笔记——常见报错及解决方法汇总(持续更新)

    操作环境:(1)Tomcat 7.0.72.0 (2)OS Name: Windows 7  (3)JVM Version:  1.8.0_25-b18  (4)eclipse Version: Ke ...

  3. 2017-10-12 NOIP模拟赛

      斐波那契 /* 相同颜色的节点与父亲节点的差相等,且是一个小于它的最大斐波那契数 所以降两个点同时减去小与它的最大斐波那契数,直到两点相等 */ #include<cstdio> ; ...

  4. 对不起,给pandas配表情包太难了,pandas你该这么学,No.6

    如果图片无法观看,请移步 https://blog.csdn.net/hihell 标题起的长,才能引起你的注意呢 昨天,有个家伙,留言给我说 嫌我不好好写博客 就知道给文章配表情包 在这里,郑重的回 ...

  5. POJ2388-Who's in the Middle

    题目链接:点击打开链接 Who's in the Middle Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 45683   ...

  6. Sublime编写React必备插件

    我终于看到了我要的攻略,sublime自带的format实在是不能看不能看不能看. 攻略地址:http://www.jianshu.com/p/ecf6c802fdc5?open_source=wei ...

  7. 牛客假日团队赛1 D.Promotion Counting

    链接: https://ac.nowcoder.com/acm/contest/918/D 题意: Bessie the cow is helping Farmer John run the USA ...

  8. Linux--5 mariadb和redis的安装

    一.MYSQL(mariadb) MariaDB数据库管理系统是MySQL的一个分支,主要由开源社区在维护,采用GPL授权许可. 开发这个分支的原因之一是:甲骨文公司收购了MySQL后,有将MySQL ...

  9. Centos 6.5 修改默认分辨率

    需要两步: 第一步: 编辑/etc/grub.conf文件,删除“nomodeset” 单词 ,翻到该页最后一行,就可以看到该词: 第二步: 删除文件/etc/X11/xorg.conf , Inte ...

  10. 《Head First 设计模式》之模板方法模式——冲泡咖啡和茶

    模板方法模式(Template) ——在一个方法中定义了一个算法的骨架,而将一些步骤延迟到子类中.模板方法使得子类可以在不改变算法结构的情况下,重新定义算法中的某些步骤. 好莱坞原则:别调用(打电话给 ...