更新!现在不偏移了!小白靠着大佬的代码和AI更改了一下device_tracker.py的代码
主要改动就是引入一个数学函数,然后设置无论什么条件都用这个函数修改坐标
代码如下
import math
class MercedesMEDeviceTracker(MercedesMeEntity, TrackerEntity, RestoreEntity):
"""Representation of a Sensor."""
# 假设 GCJ2WGS 函数是一个静态方法或者是一个独立的工具函数
@staticmethod
def GCJ2WGS(lon, lat):
a = 6378245.0 # 克拉索夫斯基椭球参数长半轴a
ee = 0.00669342162296594323 # 克拉索夫斯基椭球参数第一偏心率平方
PI = 3.14159265358979324 # 圆周率
x = lon - 105.0
y = lat - 35.0
dLon = 300.0 + x + 2.0 * y + 0.1 * x * x + 0.1 * x * y + 0.1 * math.sqrt(abs(x))
dLon += (20.0 * math.sin(6.0 * x * PI) + 20.0 * math.sin(2.0 * x * PI)) * 2.0 / 3.0
dLon += (20.0 * math.sin(x * PI) + 40.0 * math.sin(x / 3.0 * PI)) * 2.0 / 3.0
dLon += (150.0 * math.sin(x / 12.0 * PI) + 300.0 * math.sin(x / 30.0 * PI)) * 2.0 / 3.0
dLat = -100.0 + 2.0 * x + 3.0 * y + 0.2 * y * y + 0.1 * x * y + 0.2 * math.sqrt(abs(x))
dLat += (20.0 * math.sin(6.0 * x * PI) + 20.0 * math.sin(2.0 * x * PI)) * 2.0 / 3.0
dLat += (20.0 * math.sin(y * PI) + 40.0 * math.sin(y / 3.0 * PI)) * 2.0 / 3.0
dLat += (160.0 * math.sin(y / 12.0 * PI) + 320 * math.sin(y * PI / 30.0)) * 2.0 / 3.0
radLat = lat / 180.0 * PI
magic = math.sin(radLat)
magic = 1 - ee * magic * magic
sqrtMagic = math.sqrt(magic)
dLat = (dLat * 180.0) / ((a * (1 - ee)) / (magic * sqrtMagic) * PI)
dLon = (dLon * 180.0) / (a / sqrtMagic * math.cos(radLat) * PI)
wgsLon = lon - dLon
wgsLat = lat - dLat
return [wgsLat, wgsLon]
@property
def latitude(self) -> float | None:
"""Return latitude value of the device, converted to WGS-84 if necessary."""
lat = self._get_car_value("location", "positionLat", "value", 0)
lng = self._get_car_value("location", "positionLong", "value", 0)
if lat is not None and lng is not None:
wgs_lat, _ = self.GCJ2WGS(lng, lat) # 注意:经度和纬度的顺序
return wgs_lat
return lat
@property
def longitude(self) -> float | None:
"""Return longitude value of the device, converted to WGS-84 if necessary."""
lat = self._get_car_value("location", "positionLat", "value", 0)
lng = self._get_car_value("location", "positionLong", "value", 0)
if lat is not None and lng is not None:
_, wgs_lng = self.GCJ2WGS(lng, lat) # 注意:经度和纬度的顺序
return wgs_lng
return lng
@property
def source_type(self):
"""Return the source type, eg gps or router, of the device."""
return SourceType.GPS # 假设 SourceType 是一个已经定义的枚举或常量
@property
def device_class(self):
"""Return the device class of the device tracker."""
return None
|