后台坐标是百度地图标记的,小程序是腾讯地图,客户发现一个楼盘在河里,觉得很奇怪,再看看其他的楼盘,位置都不对,之后才发现百度自成体系。
由于是提供的api接口,所以自己写了一个类
/// <summary>
/// 地图坐标转换
/// 百度地图坐标是一个单独体系,需要转换
/// </summary>
public static class MapHelper
{
static double x_pi = 3.14159265358979324 * 3000.0 / 180.0;
/// <summary>
/// 百度地图坐标转腾讯地图坐标
/// </summary>
/// <param name="coordinate"></param>
public static string BaiduMapTransTengxunMap(this string coordinate)
{
double lng = 0, lat = 0;
var Arr = coordinate.Split(",");
if (Arr.Length >= 2)
{
lng = Arr[0].ToDouble();
lat = Arr[1].ToDouble();
}
double x = lng - 0.0065;
double y = lat - 0.006;
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);
double lngs = z * Math.Cos(theta);
double lats = z * Math.Sin(theta);
return lngs + "," + lats;
}
/// <summary>
/// 腾讯地图坐标转百度地图坐标
/// </summary>
/// <param name="coordinate"></param>
/// <returns></returns>
public static string TengxunMapTransBaiduMap(this string coordinate)
{
double lng = 0, lat = 0;
var Arr = coordinate.Split(",");
if (Arr.Length >= 2)
{
lng = Arr[0].ToDouble();
lat = Arr[1].ToDouble();
}
double x = lng;
double y = lat;
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);
double lngs = z * Math.Cos(theta) + 0.0065;
double lats = z * Math.Sin(theta) + 0.006;
return lngs + "," + lats;
}
}
终于正常了。位置大差不差的。。。。
该博客介绍了一个问题,即在使用百度地图API获取的坐标在腾讯地图小程序上显示异常,因为百度地图使用了自己的坐标体系。为解决这个问题,作者提供了一个C#类`MapHelper`,包含了将百度地图坐标转换为腾讯地图坐标的函数`BaiduMapTransTengxunMap`和反之的函数`TengxunMapTransBaiduMap`。通过这些转换方法,坐标位置的偏差得以修正,使得在两个平台上展示的位置接近准确。
https://blog.youkuaiyun.com/weixin_42218986/article/details/95061829
733

被折叠的 条评论
为什么被折叠?



