当前位置:首页 > 技术分享

地图经纬度转换工具(天地图,高德地图,百度地图,腾讯地图)

admin3年前 (2023-02-01)技术分享5779

uniapp 中不同地图坐标系经纬度转换(天地图,高德地图,百度地图,腾讯地图)

WGS-84:是国际标准,GPS坐标(Google Earth使用、或者GPS模块、天地图) 

 GCJ-02:中国坐标偏移标准,Google Map、高德、腾讯使用 

 BD-09:百度坐标偏移标准,Baidu Map使用 

 使用演示:https://tool.bitefu.net/geo/maptool.html


// 坐标系转换工具类
// WGS-84:是国际标准,GPS坐标(Google Earth使用、或者GPS模块、天地图)
// GCJ-02:中国坐标偏移标准,Google Map、高德、腾讯使用
// BD-09:百度坐标偏移标准,Baidu Map使用
function MapTool() {  
	this.x_PI = 3.14159265358979324 * 3000.0 / 180.0;  
	this.PI = 3.1415926535897932384626;  
	this.ee = 0.00669342162296594323;  
	this.a = 6378245.0;  
}  

// GCJ-02 转 WGS-84 高德、腾讯转天地图GPS
MapTool.prototype.gcj02towgs84 = function(lng, lat) {  
	if (this.out_of_china(lng, lat)) {
		return [lng, lat];  
	} else {
		let dlat = this.transformlat(lng - 105.0, lat - 35.0);  
		let dlng = this.transformlng(lng - 105.0, lat - 35.0);  
		let radlat = lat / 180.0 * this.PI;  
		let magic = Math.sin(radlat);  
		magic = 1 - this.ee * magic * magic;  
		let sqrtmagic = Math.sqrt(magic);  
		dlat = (dlat * 180.0) / ((this.a * (1 - this.ee)) / (magic * sqrtmagic) * this.PI);  
		dlng = (dlng * 180.0) / (this.a / sqrtmagic * Math.cos(radlat) * this.PI);  
		let mglat = lat + dlat;
		let mglng = lng + dlng;  
		return [lng * 2 - mglng, lat * 2 - mglat];  
	}  
};  

// GCJ-02 转 BD-09 高德、腾讯转百度
MapTool.prototype.gcj02tobd09 = function(lng, lat) {  
	let z = Math.sqrt(lng * lng + lat * lat) + 0.00002 * Math.sin(lat * this.x_PI);  
	let theta = Math.atan2(lat, lng) + 0.000003 * Math.cos(lng * this.x_PI);  
	let bd_lng = z * Math.cos(theta) + 0.0065;  
	let bd_lat = z * Math.sin(theta) + 0.006;  
	return [bd_lng, bd_lat];  
};  

// WGS-84 转 GCJ-02 天地图GPS转高德、腾讯
MapTool.prototype.wgs84togcj02 = function(lng, lat) {  
	if (this.out_of_china(lng, lat)) {  
		return [lng, lat];  
	} else {  
		let dlat = this.transformlat(lng - 105.0, lat - 35.0);  
		let dlng = this.transformlng(lng - 105.0, lat - 35.0);  
		let radlat = lat / 180.0 * this.PI;  
		let magic = Math.sin(radlat);  
		magic = 1 - this.ee * magic * magic;  
		let sqrtmagic = Math.sqrt(magic);  
		dlat = (dlat * 180.0) / ((this.a * (1 - this.ee)) / (magic * sqrtmagic) * this.PI);  
		dlng = (dlng * 180.0) / (this.a / sqrtmagic * Math.cos(radlat) * this.PI);  
		let mglat = lat + dlat;  
		let mglng = lng + dlng;  
		return [mglng, mglat];  
	}  
};  

// BD-09 转 GCJ-02  百度转高德、腾讯
MapTool.prototype.bd09togcj02 = function(bd_lng, bd_lat) {  
	let x = bd_lng - 0.0065;  
	let y = bd_lat - 0.006;  
	let z = Math.sqrt(x * x + y * y) - 0.00002 * Math.sin(y * this.x_PI);  
	let theta = Math.atan2(y, x) - 0.000003 * Math.cos(x * this.x_PI);  
	let gg_lng = z * Math.cos(theta);  
	let gg_lat = z * Math.sin(theta);  
	return [gg_lng, gg_lat];  
};  

// BD-09 转 WGS-84	百度转天地图GPS
MapTool.prototype.bd09towgs84 = function(lng, lat) {  
	const gcj = this.bd09togcj02(lng, lat);  
	return this.gcj02towgs84(gcj[0], gcj[1]);  
};  

// WGS-84 转 BD-09	天地图GPS转百度
MapTool.prototype.wgs84tobd09 = function(lng, lat) {  
	const gcj = this.wgs84togcj02(lng, lat);  
	return this.gcj02tobd09(gcj[0], gcj[1]);  
};  

// 内部转换方法
MapTool.prototype.transformlat = function(lng, lat) {  
	let ret = -100.0 + 2.0 * lng + 3.0 * lat + 0.2 * lat * lat + 0.1 * lng * lat + 0.2 * Math.sqrt(Math.abs(lng));  
	ret += (20.0 * Math.sin(6.0 * lng * this.PI) + 20.0 * Math.sin(2.0 * lng * this.PI)) * 2.0 / 3.0;  
	ret += (20.0 * Math.sin(lat * this.PI) + 40.0 * Math.sin(lat / 3.0 * this.PI)) * 2.0 / 3.0;  
	ret += (160.0 * Math.sin(lat / 12.0 * this.PI) + 320 * Math.sin(lat * this.PI / 30.0)) * 2.0 / 3.0;  
	return ret;  
};  

MapTool.prototype.transformlng = function(lng, lat) {  
	let ret = 300.0 + lng + 2.0 * lat + 0.1 * lng * lng + 0.1 * lng * lat + 0.1 * Math.sqrt(Math.abs(lng));  
	ret += (20.0 * Math.sin(6.0 * lng * this.PI) + 20.0 * Math.sin(2.0 * lng * this.PI)) * 2.0 / 3.0;  
	ret += (20.0 * Math.sin(lng * this.PI) + 40.0 * Math.sin(lng / 3.0 * this.PI)) * 2.0 / 3.0;  
	ret += (150.0 * Math.sin(lng / 12.0 * this.PI) + 300.0 * Math.sin(lng / 30.0 * this.PI)) * 2.0 / 3.0;  
	return ret;  
};  

// 判断坐标是否在中国境内
MapTool.prototype.out_of_china = function(lng, lat) {  
	return (lng < 72.004 || lng > 137.8347) || (lat < 0.8293 || lat > 55.8271);  
};  

// 初始化转换工具
const mapTool = new MapTool();

// 坐标转换函数
function convertCoordinates(lng,lat,sourceType,targetType) {
	var ret = {status:0,msg:''};
	if (isNaN(lng) || isNaN(lat)) {
		ret.msg = '请输入有效的经纬度数值';
		return ret;
	}
    lng = parseFloat(lng)
    lat = parseFloat(lat)
	let result;
	const originalCoords = [lng, lat];
	
	try {
		if (sourceType === targetType) {
			result = [lng, lat];
		} else if (sourceType === 'gcj02' && targetType === 'wgs84') {
			result = mapTool.gcj02towgs84(lng, lat);
		} else if (sourceType === 'gcj02' && targetType === 'bd09') {
			result = mapTool.gcj02tobd09(lng, lat);
		} else if (sourceType === 'wgs84' && targetType === 'gcj02') {
			result = mapTool.wgs84togcj02(lng, lat);
		} else if (sourceType === 'wgs84' && targetType === 'bd09') {
			result = mapTool.wgs84tobd09(lng, lat);
		} else if (sourceType === 'bd09' && targetType === 'gcj02') {
			result = mapTool.bd09togcj02(lng, lat);
		} else if (sourceType === 'bd09' && targetType === 'wgs84') {
			result = mapTool.bd09towgs84(lng, lat);
		}
        
        ret.status=1;
		ret.lng = result[0].toFixed(6);
		ret.lat = result[1].toFixed(6);
		return ret;
	} catch (e) {
		ret.msg = '转换失败: ' + e.message;
		return ret;
	}
}

php类

<?php
/**
 * @name Coordinate
 * @desc 坐标转换
 */
class Coordinate
{
    const x_PI = 52.35987755982988;
    const PI = 3.1415926535897932384626;
    const a = 6378245.0;
    const ee = 0.00669342162296594323;
 
    /**
     * 百度坐标系(BD-09) 转 火星坐标系(GCJ-02)
     * @param 
     * @return 
     **/
    public static function bd09ToGcj02($bd_lon, $bd_lat)
    {
        $x = $bd_lon - 0.0065;
        $y = $bd_lat - 0.006;
        $z = sqrt($x * $x + $y * $y) - 0.00002 * sin($y * self::x_PI);
        $theta = atan2($y, $x) - 0.000003 * cos($x * self::x_PI);
        $g_lon = $z * cos($theta);
        $g_lat = $z * sin($theta);
        return array('lon' => $g_lon, 'lat' => $g_lat);
    }
 
    /**
     * 火星坐标系(GCJ-02) 转 百度坐标系(BD-09)
     * 即谷歌、高德 转 百度
     * @param 
     * @return 
     **/
    public static function gcj02Tobd09($g_lon, $g_lat)
    {
        $z = sqrt($g_lon * $g_lon + $g_lat * $g_lat) + 0.00002 * sin($g_lat * self::x_PI);
        $theta = atan2($g_lat, $g_lon) + 0.000003 * cos($g_lon * self::x_PI);
        $bd_lon = $z * cos($theta) + 0.0065;
        $bd_lat = $z * sin($theta) + 0.006;
        return array('lon' => $bd_lon, 'lat' => $bd_lat);
    }
 
    /**
     * WGS84 转 GCj02
     * @param 
     * @return 
     **/
    public static function wgs84ToGcj02($w_lon, $w_lat)
    {
        $dlat = self::transFormLat($w_lon - 105.0, $w_lat - 35.0);
        $dlon = self::transFormLon($w_lon - 105.0, $w_lat - 35.0);
        $radlat = $w_lat / 180.0 * self::PI;
        $magic = sin($radlat);
        $magic = 1 - self::ee * $magic * $magic;
        $sqrtmagic = sqrt($magic);
        $dlat = ($dlat * 180.0) / ((self::a * (1 - self::ee)) / ($magic * $sqrtmagic) * self::PI);
        $dlon = ($dlon * 180.0) / (self::a / $sqrtmagic * cos($radlat) * self::PI);
        $g_lat = $w_lat + $dlat;
        $g_lon = $w_lon + $dlon;
        return array('lon' => $g_lon, 'lat' => $g_lat);
    }
 
    /**
     * GCJ02 转换为 WGS84
     * @param 
     * @return 
     **/
    public static function gcj02ToWgs84($g_lon, $g_lat)
    {
        $dlat = self::transFormLat($g_lon - 105.0, $g_lat - 35.0);
        $dlon = self::transFormLon($g_lon - 105.0, $g_lat - 35.0);
        $radlat = $g_lat / 180.0 * self::PI;
        $magic = sin($radlat);
        $magic = 1 - self::ee * $magic * $magic;
        $sqrtmagic = sqrt($magic);
        $dlat = ($dlat * 180.0) / ((self::a * (1 - self::ee)) / ($magic * $sqrtmagic) * self::PI);
        $dlon = ($dlon * 180.0) / (self::a / $sqrtmagic * cos($radlat) * self::PI);
        $w_lat = $g_lat + $dlat;
        $w_lon = $g_lon + $dlon;
        return array('lon' => $g_lon * 2 -$w_lon, 'lat' => $g_lat * 2 -$w_lat);
    }
 
    /**
     * BD09 转换为 WGS84
     * @param 
     * @return 
     **/
    public static function bd09ToWgs84($bd_lon, $bd_lat)
    {
        $gcj02 = self::bd09ToGcj02($bd_lon, $bd_lat);
        $g_lon = $gcj02['lon'];
        $g_lat = $gcj02['lat'];
        $wgs84 = self::gcj02ToWgs84($g_lon, $g_lat);
        return $wgs84;
    }
 
    /**
     * WGS84 转换为 BD09
     * @param 
     * @return 
     **/
    public static function wgs84ToBd09($w_lon,$w_lat){
        $gcj02 = self::wgs84ToGcj02($w_lon,$w_lat);
        $g_lon = $gcj02['lon'];
        $g_lat = $gcj02['lat'];
        $bd09 = self::gcj02Tobd09($g_lon,$g_lat);
        return $bd09;
    } 
 
    /**
     * 转换纬度
     * @param 
     * @return 
     **/
    protected static function transFormLat($lon, $lat)
    {
        $ret = -100.0 + 2.0 * $lon + 3.0 * $lat + 0.2 * $lat * $lat + 0.1 * $lon * $lat + 0.2 * sqrt(abs($lon));
        $ret += (20.0 * sin(6.0 * $lon * self::PI) + 20.0 * sin(2.0 * $lon * self::PI)) * 2.0 / 3.0;
        $ret += (20.0 * sin($lat * self::PI) + 40.0 * sin($lat / 3.0 * self::PI)) * 2.0 / 3.0;
        $ret += (160.0 * sin($lat / 12.0 * self::PI) + 320 * sin($lat * self::PI / 30.0)) * 2.0 / 3.0;
        return $ret;
    }
 
    /**
     * 转换经度
     * @param 
     * @return 
     **/
    protected static function transFormLon($lon, $lat)
    {
        $ret = 300.0 + $lon + 2.0 * $lat + 0.1 * $lon * $lon + 0.1 * $lon * $lat + 0.1 * sqrt(abs($lon));
        $ret += (20.0 * sin(6.0 * $lon * self::PI) + 20.0 * sin(2.0 * $lon * self::PI)) * 2.0 / 3.0;
        $ret += (20.0 * sin($lon * self::PI) + 40.0 * sin($lon / 3.0 * self::PI)) * 2.0 / 3.0;
        $ret += (150.0 * sin($lon / 12.0 * self::PI) + 300.0 * sin($lon / 30.0 * self::PI)) * 2.0 / 3.0;
        return $ret;
    }
}
?>


扫描二维码推送至手机访问。

版权声明:本文由小刚刚技术博客发布,如需转载请注明出处。

本文链接:https://blog.bitefu.net/post/461.html

分享给朋友:

“地图经纬度转换工具(天地图,高德地图,百度地图,腾讯地图)” 的相关文章

微软版Ghost Win10:FFU映像备份和还原

微软版Ghost Win10:FFU映像备份和还原

在日常的维护中,系统的备份和还原是大家经常需要操作的事情。虽然Windows 10已经提供很多的工具,如系统还原、WIM备份/还原,VHD备份等。不过这些工具大多是基于文件的备份/还原。我们以前经常的使用的Ghost则是基于扇区的备份/还原…

超高性比的斐讯盒子T1,刷第三方YYF固件机教程超级详细版

超高性比的斐讯盒子T1,刷第三方YYF固件机教程超级详细版

家里面买了斐讯盒子T1,必不可少的就是刷机,刷机一直爽,一直刷机一直爽,这样的快乐一般人体会不到。原来斐讯盒子N1,T1,还有斐讯K2P路由器也变成了性价比超高的东东,而且众多大神也带来了超多可玩性非常高的固件和破解。楼主今天扒到了相关超高…

centos 配置Let's Encrypt 泛域名https证书

centos 配置Let's Encrypt 泛域名https证书

前言2018年1月份Letsencrypt可以申请泛域名证书,这让我们部署多域名、多站点https省了很多功夫,终于可以不用维护多个域名的https证书。笔者以acme.sh为例,手把手教你配置https证书~本教程适用于centos 6.…

贾氏鸣天鼓健耳养肾操

贾氏鸣天鼓健耳养肾操

《贾氏鸣天鼓健耳养肾操》鸣天鼓是健耳强肾治耳病的古法,贾氏越云自创的鸣天鼓健耳养肾操是在古法的基础上创建。顺序:1静坐挺胸。2双手放心脏位置的胸口,左手掌盖住右手掌。3闭目静心,深呼吸19下。4双手相互搓揉,让手掌发热。5用发热的双手手掌严…

用CMD命令查询域名的DNS解析记录:A,NS,MX,CNAME,TXT

1、查询域名的A记录nslookup -qt=A bitefu.net当然查询A记录你直接用ping命令来ping域名也可以获得A记录。2、查询域名的NS记录nslookup -qt=NS bitefu.net3、查询域名的MX记录nslo…

python3 selenium webdriver.Chrome php 爬取汽车之家所有车型详情数据[开源版]

介绍本接口是车型库api的补充,用于爬取汽车之家所有车型详情数据开源地址:https://gitee.com/web/CarApi/tree/master/python软件架构python3 selenium webdriver.Chrom…

发表评论

访客

看不清,换一张

◎欢迎参与讨论,请在这里发表您的看法和观点。