找回密码
 立即注册
楼主: left

[技术讨论] 安装墨澜地图增强版之后,汽车定位依旧偏移

[复制链接]
left 

1

主题

11

回帖

71

积分

注册会员

积分
71
金钱
59
HASS币
0
 楼主| 发表于 2025-1-19 19:28:08 | 显示全部楼层
更新!现在不偏移了!小白靠着大佬的代码和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
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|Hassbian

GMT+8, 2025-5-1 12:40 , Processed in 0.120063 second(s), 21 queries .

Powered by Discuz! X3.5

© 2001-2025 Discuz! Team.

快速回复 返回顶部 返回列表