博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Gps坐标转换成gcj 02坐标的js代码
阅读量:6983 次
发布时间:2019-06-27

本文共 2281 字,大约阅读时间需要 7 分钟。

由于最近在开发相关地图应用,所以不可必免的就会遇到GPS坐标(WGS-84)转换成国家测绘局标准要求的GCJ-02(所谓的火星坐标)

关于WGS-84GCJ-02可以参照这篇文章,.

下面的代码摘直网上,特收藏下

//World Geodetic System ==> Mars Geodetic System//translate from https://on4wp7.codeplex.com/SourceControl/changeset/view/21483#353936var WGS84_to_GCJ02 = function() {}WGS84_to_GCJ02.prototype.a = 6378245.0;WGS84_to_GCJ02.prototype.ee = 0.00669342162296594323;WGS84_to_GCJ02.prototype.transform = function(wgLat, wgLon) {
if (this.outOfChina(wgLat, wgLon)) {
return [wgLat, wgLon]; } dLat = this.transformLat(wgLon - 105.0, wgLat - 35.0); dLon = this.transformLon(wgLon - 105.0, wgLat - 35.0); radLat = wgLat / 180.0 * Math.PI; magic = Math.sin(radLat); magic = 1 - this.ee * magic * magic; sqrtMagic = Math.sqrt(magic); dLat = (dLat * 180.0) / ((this.a * (1 - this.ee)) / (magic * sqrtMagic) * Math.PI); dLon = (dLon * 180.0) / (this.a / sqrtMagic * Math.cos(radLat) * Math.PI); mgLat = wgLat + dLat; mgLon = wgLon + dLon; return [mgLat, mgLon];};WGS84_to_GCJ02.prototype.outOfChina = function(lat, lon) {
if (lon < 72.004 || lon > 137.8347) return true; if (lat < 0.8293 || lat > 55.8271) return true; return false;};WGS84_to_GCJ02.prototype.transformLat = function(x, y) {
var ret = -100.0 + 2.0 * x + 3.0 * y + 0.2 * y * y + 0.1 * x * y + 0.2 * Math.sqrt(Math.abs(x)); ret += (20.0 * Math.sin(6.0 * x * Math.PI) + 20.0 * Math.sin(2.0 * x * Math.PI)) * 2.0 / 3.0; ret += (20.0 * Math.sin(y * Math.PI) + 40.0 * Math.sin(y / 3.0 * Math.PI)) * 2.0 / 3.0; ret += (160.0 * Math.sin(y / 12.0 * Math.PI) + 320 * Math.sin(y * Math.PI / 30.0)) * 2.0 / 3.0; return ret;};WGS84_to_GCJ02.prototype.transformLon = function(x, y) {
var ret = 300.0 + x + 2.0 * y + 0.1 * x * x + 0.1 * x * y + 0.1 * Math.sqrt(Math.abs(x)); ret += (20.0 * Math.sin(6.0 * x * Math.PI) + 20.0 * Math.sin(2.0 * x * Math.PI)) * 2.0 / 3.0; ret += (20.0 * Math.sin(x * Math.PI) + 40.0 * Math.sin(x / 3.0 * Math.PI)) * 2.0 / 3.0; ret += (150.0 * Math.sin(x / 12.0 * Math.PI) + 300.0 * Math.sin(x / 30.0 * Math.PI)) * 2.0 / 3.0; return ret;};

使用例子如下:

new WGS84_to_GCJ02().transform(31.283814, 121.502191) // -> [31.28181188043995, 121.50661885748906] (该坐标是上海同济大学)

最后附上一个在线转换接口,

转载地址:http://kaxpl.baihongyu.com/

你可能感兴趣的文章
Site Definition和Web Template的区别
查看>>
细说linux挂载
查看>>
阿里资深系统架构师九峰谈云计算
查看>>
Android的多任务之路
查看>>
Autochk program not found - skipping auocheck
查看>>
☆聊聊Spring系列_Index
查看>>
我的友情链接
查看>>
不用软件,手动修复双系统引导进win7,xp的多种方法
查看>>
python 访问需要HTTP Basic Authentication认证的资源
查看>>
java中比较字符串的大小用String的compareTo()
查看>>
plist使用
查看>>
Linux RAR 安装和使用
查看>>
【OC】【一秒就会】【collectionView 头部吸住功能】
查看>>
51CTO下载 好资料分享
查看>>
linux 下转换UTC到本地时间
查看>>
Linux的起源与各发行版的基本知识
查看>>
单播包、广播包、组播包、洪泛包
查看>>
23种设计模式之解释器模式
查看>>
iptables命令结构之命令
查看>>
RabbitMQ之Exchange分类
查看>>