.NET 火星坐标转百度坐标算法
程序员文章站
2022-04-04 16:47:20
...
.NET
using GPS_VEHICLE.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace GPS_VEHICLE.Common
{
public class LatLngPoint
{
public double LonX { get; set; }
public double LatY { get; set; }
}
public class VehicleCom
{
const double x_pi = 3.14159265358979324 * 3000.0 / 180.0;
const double pi= 3.1415926535897932384626;
const double ee = 0.00669342162296594323;
const double a = 6378245.0;
/// <summary>
/// 火星坐标系转百度坐标系
/// </summary>
/// <param name="gcjPoint"></param>
/// <returns></returns>
public static LatLngPoint GCJ02ToBD09(LatLngPoint gcjPoint)
{
LatLngPoint _bdPoint = new LatLngPoint();
double _x = gcjPoint.LonX;
double y = gcjPoint.LatY;
double _z = Math.Sqrt(_x * _x + y * y) + 0.00002 * Math.Sin(y * x_pi);
double _theta = Math.Atan2(y, _x) + 0.000003 * Math.Cos(_x * x_pi);
_bdPoint.LonX = _z * Math.Cos(_theta) + 0.0065;
_bdPoint.LatY = _z * Math.Sin(_theta) + 0.006;
return _bdPoint;
}
/// <summary>
/// WGS84大地坐标转GCJ02火星坐标
/// </summary>
/// <param name="point"></param>
/// <returns></returns>
public static LatLngPoint wgs84togcj02(LatLngPoint point) {
if (out_of_china(point))
{
return point;
}
else
{
LatLngPoint point2 = new LatLngPoint()
{
LonX = point.LonX - 105.0,
LatY = point.LatY - 35.0
};
var dlat = transformlat(point2);
var dlng = transformlng(point2);
var radlat = point.LatY / 180.0 * pi;
var magic = Math.Sin(radlat);
magic = 1 - ee * magic * magic;
var sqrtmagic = Math.Sqrt(magic);
dlat = (dlat * 180.0) / ((a * (1 - ee)) / (magic * sqrtmagic) * pi);
dlng = (dlng * 180.0) / (a / sqrtmagic * Math.Cos(radlat) * pi);
var mglat = point.LatY + dlat;
var mglng = point.LonX + dlng;
return new LatLngPoint() { LatY = mglat, LonX = mglng };
}
}
public static double transformlat(LatLngPoint point)
{
var ret = -100.0 + 2.0 * point.LonX + 3.0 * point.LatY + 0.2 * point.LatY * point.LatY + 0.1 * point.LonX * point.LatY + 0.2 * Math.Sqrt(Math.Abs(point.LonX));
ret += (20.0 * Math.Sin(6.0 * point.LonX * pi) + 20.0 * Math.Sin(2.0 * point.LonX * pi)) * 2.0 / 3.0;
ret += (20.0 * Math.Sin(point.LatY * pi) + 40.0 * Math.Sin(point.LatY / 3.0 * pi)) * 2.0 / 3.0;
ret += (160.0 * Math.Sin(point.LatY / 12.0 * pi) + 320 * Math.Sin(point.LatY * pi / 30.0)) * 2.0 / 3.0;
return ret;
}
public static double transformlng(LatLngPoint point)
{
var ret = 300.0 + point.LonX + 2.0 * point.LatY + 0.1 * point.LonX * point.LonX + 0.1 * point.LonX * point.LatY + 0.1 * Math.Sqrt(Math.Abs(point.LonX));
ret += (20.0 * Math.Sin(6.0 * point.LonX * pi) + 20.0 * Math.Sin(2.0 * point.LonX * pi)) * 2.0 / 3.0;
ret += (20.0 * Math.Sin(point.LonX * pi) + 40.0 * Math.Sin(point.LonX / 3.0 * pi)) * 2.0 / 3.0;
ret += (150.0 * Math.Sin(point.LonX / 12.0 * pi) + 300.0 * Math.Sin(point.LonX / 30.0 * pi)) * 2.0 / 3.0;
return ret;
}
/// <summary>
/// 判断是否在国内,不在国内则不做偏移
/// </summary>
/// <param name="point"></param>
/// <returns></returns>
public static bool out_of_china(LatLngPoint point)
{
return (point.LonX < 72.004 || point.LonX > 137.8347) || ((point.LatY < 0.8293 || point.LatY > 55.8271) || false);
}
}
}
JS
/**
* Created by Wandergis on 2015/7/8.
* 提供了百度坐标(BD09)、国测局坐标(火星坐标,GCJ02)、和WGS84坐标系之间的转换
*/
//定义一些常量
var x_PI = 3.14159265358979324 * 3000.0 / 180.0;
var PI = 3.1415926535897932384626;
var a = 6378245.0;
var ee = 0.00669342162296594323;
/**
* 百度坐标系 (BD-09) 与 火星坐标系 (GCJ-02)的转换
* 即 百度 转 谷歌、高德
* @param bd_lon
* @param bd_lat
* @returns {*[]}
*/
function bd09togcj02(bd_lon, bd_lat) {
var x_pi = 3.14159265358979324 * 3000.0 / 180.0;
var x = bd_lon - 0.0065;
var y = bd_lat - 0.006;
var z = Math.sqrt(x * x + y * y) - 0.00002 * Math.sin(y * x_pi);
var theta = Math.atan2(y, x) - 0.000003 * Math.cos(x * x_pi);
var gg_lng = z * Math.cos(theta);
var gg_lat = z * Math.sin(theta);
return [gg_lng, gg_lat]
}
/**
* 火星坐标系 (GCJ-02) 与百度坐标系 (BD-09) 的转换
* 即谷歌、高德 转 百度
* @param lng
* @param lat
* @returns {*[]}
*/
function gcj02tobd09(lng, lat) {
var z = Math.sqrt(lng * lng + lat * lat) + 0.00002 * Math.sin(lat * x_PI);
var theta = Math.atan2(lat, lng) + 0.000003 * Math.cos(lng * x_PI);
var bd_lng = z * Math.cos(theta) + 0.0065;
var bd_lat = z * Math.sin(theta) + 0.006;
return [bd_lng, bd_lat]
}
/**
* WGS84转GCj02
* @param lng
* @param lat
* @returns {*[]}
*/
function wgs84togcj02(lng, lat) {
if (out_of_china(lng, lat)) {
return [lng, lat]
}
else {
var dlat = transformlat(lng - 105.0, lat - 35.0);
var dlng = transformlng(lng - 105.0, lat - 35.0);
var radlat = lat / 180.0 * PI;
var magic = Math.sin(radlat);
magic = 1 - ee * magic * magic;
var sqrtmagic = Math.sqrt(magic);
dlat = (dlat * 180.0) / ((a * (1 - ee)) / (magic * sqrtmagic) * PI);
dlng = (dlng * 180.0) / (a / sqrtmagic * Math.cos(radlat) * PI);
var mglat = lat + dlat;
var mglng = lng + dlng;
return [mglng, mglat]
}
}
/**
* GCJ02 转换为 WGS84
* @param lng
* @param lat
* @returns {*[]}
*/
function gcj02towgs84(lng, lat) {
if (out_of_china(lng, lat)) {
return [lng, lat]
}
else {
var dlat = transformlat(lng - 105.0, lat - 35.0);
var dlng = transformlng(lng - 105.0, lat - 35.0);
var radlat = lat / 180.0 * PI;
var magic = Math.sin(radlat);
magic = 1 - ee * magic * magic;
var sqrtmagic = Math.sqrt(magic);
dlat = (dlat * 180.0) / ((a * (1 - ee)) / (magic * sqrtmagic) * PI);
dlng = (dlng * 180.0) / (a / sqrtmagic * Math.cos(radlat) * PI);
mglat = lat + dlat;
mglng = lng + dlng;
return [lng * 2 - mglng, lat * 2 - mglat]
}
}
function transformlat(lng, lat) {
var 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 * PI) + 20.0 * Math.sin(2.0 * lng * PI)) * 2.0 / 3.0;
ret += (20.0 * Math.sin(lat * PI) + 40.0 * Math.sin(lat / 3.0 * PI)) * 2.0 / 3.0;
ret += (160.0 * Math.sin(lat / 12.0 * PI) + 320 * Math.sin(lat * PI / 30.0)) * 2.0 / 3.0;
return ret
}
function transformlng(lng, lat) {
var 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 * PI) + 20.0 * Math.sin(2.0 * lng * PI)) * 2.0 / 3.0;
ret += (20.0 * Math.sin(lng * PI) + 40.0 * Math.sin(lng / 3.0 * PI)) * 2.0 / 3.0;
ret += (150.0 * Math.sin(lng / 12.0 * PI) + 300.0 * Math.sin(lng / 30.0 * PI)) * 2.0 / 3.0;
return ret
}
/**
* 判断是否在国内,不在国内则不做偏移
* @param lng
* @param lat
* @returns {boolean}
*/
function out_of_china(lng, lat) {
return (lng < 72.004 || lng > 137.8347) || ((lat < 0.8293 || lat > 55.8271) || false);
}