当前位置:网站首页>C# 百度地图,高德地图,Google地图(GPS) 经纬度转换
C# 百度地图,高德地图,Google地图(GPS) 经纬度转换
2022-07-02 06:32:00 【清山博客】
using System;
namespace PhotoHelper
{
/// <summary>
/// 位置坐标转换类
/// WGS-84:是国际标准,GPS坐标(Google Earth使用、或者GPS模块)
/// GCJ-02:中国坐标偏移标准,Google Map、高德、腾讯使用
/// BD-09:百度坐标偏移标准,Baidu Map使用
/// </summary>
public class LocationUtil
{
public const double Pi = 3.14159265358979324 * 3000.0 / 180.0;
private static readonly double pi = 3.14159265358979324;
private static readonly double a = 6378245.0;
private static readonly double ee = 0.00669342162296594323;
/// <summary>
/// 国际标准->百度标准(gps坐标转换成百度坐标,小数点前4位为准确坐标)
/// </summary>
/// <param name="lat">纬度</param>
/// <param name="lon">经度</param>
/// <returns></returns>
public static double[] Wgs2Bd(double lat, double lon)
{
var wgs2Gcj = Wgs2Gcj(lat, lon);
var gcj2Bd = Gcj2Bd(wgs2Gcj[0], wgs2Gcj[1]);
return gcj2Bd;
}
public static double[] Gcj2Bd(double lat, double lon)
{
double x = lon, y = lat;
var z = Math.Sqrt(x * x + y * y) + 0.00002 * Math.Sin(y * Pi);
var theta = Math.Atan2(y, x) + 0.000003 * Math.Cos(x * Pi);
var cos = z * Math.Cos(theta) + 0.0065;
var sin = z * Math.Sin(theta) + 0.006;
return new[] { sin, cos };
}
public static double[] Bd2Gcj(double lat, double lon)
{
double x = lon - 0.0065, y = lat - 0.006;
var z = Math.Sqrt(x * x + y * y) - 0.00002 * Math.Sin(y * Pi);
var theta = Math.Atan2(y, x) - 0.000003 * Math.Cos(x * Pi);
var cos = z * Math.Cos(theta);
var sin = z * Math.Sin(theta);
return new[] { sin, cos };
}
public static double[] Wgs2Gcj(double lat, double lon)
{
var dLat = TransformLat(lon - 105.0, lat - 35.0);
var dLon = TransformLon(lon - 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);
dLon = dLon * 180.0 / (a / sqrtMagic * Math.Cos(radLat) * pi);
var mgLat = lat + dLat;
var mgLon = lon + dLon;
double[] loc = { mgLat, mgLon };
return loc;
}
private static double TransformLat(double lat, double lon)
{
var ret = -100.0 + 2.0 * lat + 3.0 * lon + 0.2 * lon * lon + 0.1 * lat * lon + 0.2 * Math.Sqrt(Math.Abs(lat));
ret += (20.0 * Math.Sin(6.0 * lat * pi) + 20.0 * Math.Sin(2.0 * lat * pi)) * 2.0 / 3.0;
ret += (20.0 * Math.Sin(lon * pi) + 40.0 * Math.Sin(lon / 3.0 * pi)) * 2.0 / 3.0;
ret += (160.0 * Math.Sin(lon / 12.0 * pi) + 320 * Math.Sin(lon * pi / 30.0)) * 2.0 / 3.0;
return ret;
}
private static double TransformLon(double lat, double lon)
{
var ret = 300.0 + lat + 2.0 * lon + 0.1 * lat * lat + 0.1 * lat * lon + 0.1 * Math.Sqrt(Math.Abs(lat));
ret += (20.0 * Math.Sin(6.0 * lat * pi) + 20.0 * Math.Sin(2.0 * lat * 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 += (150.0 * Math.Sin(lat / 12.0 * pi) + 300.0 * Math.Sin(lat / 30.0 * pi)) * 2.0 / 3.0;
return ret;
}
}
}
边栏推荐
猜你喜欢
File upload and download performance test based on the locust framework
commands out of sync. did you run multiple statements at once
Hcia - Application Layer
kubernetes部署loki日志系统
Minecraft空岛服开服
C language custom type enumeration, Union (clever use of enumeration, calculation of union size)
Tcp/ip - transport layer
Chrome debugging
ICMP Protocol
DWORD ptr[]
随机推荐
[blackmail virus data recovery] suffix Crylock blackmail virus
[flask] ORM one-to-one relationship
HCIA - application layer
Judge whether it is Sudoku
Valin cable: BI application promotes enterprise digital transformation
Linux二进制安装Oracle Database 19c
Web security -- core defense mechanism
Nacos 下载启动、配置 MySQL 数据库
When a custom exception encounters reflection
Hcia - Application Layer
Implementation of bidirectional linked list (simple difference, connection and implementation between bidirectional linked list and unidirectional linked list)
libusb的使用
kubernetes部署loki日志系统
Minecraft群組服開服
Loadbalancer dynamically refreshes Nacos server
Makefile Fundamentals
程序猿学英语-Learning C
Programming ape learning English - imperative programming
c语言自定义类型——结构体,位段(匿名结构体,结构体的自引用,结构体的内存对齐)
Jz-061-serialized binary tree