Skip to content

Instantly share code, notes, and snippets.

@FlashSoft
Last active September 25, 2023 04:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save FlashSoft/8d86ac65a2802501047e6ad5762acc2d to your computer and use it in GitHub Desktop.
Save FlashSoft/8d86ac65a2802501047e6ad5762acc2d to your computer and use it in GitHub Desktop.
Home Assistant wgs84ToGcj02

这是一键在homeasstant里使用高德的瓦片的替换程序,支持高德地图和谷歌地图(以及卫星),支持高德路况

使用docker安装的homeassistant可以使用如下命令一键替换

docker exec home-assistant bash -c "curl https://ha-map.maidou.cloud |bash"

如果不是docker的homeasstant,可以考虑修改worker.js里的内容,并且重新自己部署到cloudflare的worker里后进行操作

function getChinaMapContent() {
return `(function (L, oMap) {
const GaodeMap = L.tileLayer("https://wprd0{s}.is.autonavi.com/appmaptile?lang=zh_cn&size=1&scale=1&style=7&x={x}&y={y}&z={z}", {
subdomains: "1234",
minZoom: 3,
maxZoom: 18
})
const GoogleMap = L.tileLayer("https://www.google.com/maps/vt?lyrs=m@189&gl=cn&x={x}&y={y}&z={z}", {
minZoom: 0,
maxZoom: 18
})
const GaodeEarth = L.tileLayer("https://wprd0{s}.is.autonavi.com/appmaptile?lang=zh_cn&size=1&scale=1&style=6&x={x}&y={y}&z={z}", {
subdomains: "1234",
minZoom: 0,
maxZoom: 18
})
const GoogleEarth = L.tileLayer("https://www.google.com/maps/vt?lyrs=s@189&gl=us&x={x}&y={y}&z={z}", {
minZoom: 0,
maxZoom: 18
})
L.control.layers({
'高德': GaodeMap, '谷歌': GoogleMap, '高德卫星': GaodeEarth, '谷歌卫星': GoogleEarth,
}, {
'高德(实时路况)': L.tileLayer("https://tm.amap.com/trafficengine/mapabc/traffictile?v=1.0&x={x}&y={y}&z={z}&t=1")
}).addTo(oMap);
L.GridLayer.include({
_setZoomTransform(level, center, zoom) {
center = center !== undefined ? wgs84ToGcj02(center.lng, center.lat) : center;
const viewHalf = this._map.getSize().divideBy(2);
const mapProject = this._map.project(center, zoom);
const mapPanePos = this._map._mapPane._leaflet_pos;
const newPixelOrigin = mapProject.subtract(viewHalf)._add(mapPanePos).round();
const scale = this._map.getZoomScale(zoom, level.zoom);
const translate = level.origin.multiplyBy(scale).subtract(newPixelOrigin).round();
L.Browser.any3d ? L.DomUtil.setTransform(level.el, translate, scale) : L.DomUtil.setPosition(level.el, translate);
},
_getTiledPixelBounds(center) {
center = center !== undefined ? wgs84ToGcj02(center.lng, center.lat) : center;
const map = this._map;
const mapZoom = map._animatingZoom ? Math.max(map._animateToZoom, map.getZoom()) : map.getZoom();
const scale = map.getZoomScale(mapZoom, this._tileZoom);
const pixelCenter = map.project(center, this._tileZoom).floor();
const halfSize = map.getSize().divideBy(scale * 2);
return new L.Bounds(pixelCenter.subtract(halfSize), pixelCenter.add(halfSize));
},
});
function wgs84ToGcj02(lng, lat) {
const a = 6378245.0;
const ee = 0.00669342162296594323;
const pi = Math.PI;
if (oMap.hasLayer(GoogleEarth) || lng < 72.004 || lng > 137.8347 || lat < 0.8293 || lat > 55.8271) return { lng, lat };
let dlat = transformLat(lng - 105.0, lat - 35.0);
let dlng = transformLon(lng - 105.0, lat - 35.0);
const radlat = (lat / 180.0) * pi;
let magic = Math.sin(radlat);
magic = 1 - ee * magic * magic;
const sqrtmagic = Math.sqrt(magic);
dlat = (dlat * 180.0) / ((a * (1 - ee)) / (magic * sqrtmagic) * pi);
dlng = (dlng * 180.0) / (a / sqrtmagic * Math.cos(radlat) * pi);
const mglat = lat + dlat;
const mglng = lng + dlng;
return { lng: mglng, lat: mglat };
}
function transformLat(x, y) {
const pi = Math.PI;
let lat = -100.0 + 2.0 * x + 3.0 * y + 0.2 * y * y + 0.1 * x * y + 0.2 * Math.sqrt(Math.abs(x));
lat += (20.0 * Math.sin(6.0 * x * pi) + 20.0 * Math.sin(2.0 * x * pi)) * 2.0 / 3.0;
lat += (20.0 * Math.sin(y * pi) + 40.0 * Math.sin(y / 3.0 * pi)) * 2.0 / 3.0;
lat += (160.0 * Math.sin(y / 12.0 * pi) + 320 * Math.sin(y * pi / 30.0)) * 2.0 / 3.0;
return lat;
}
function transformLon(x, y) {
const pi = Math.PI;
let lon = 300.0 + x + 2.0 * y + 0.1 * x * x + 0.1 * x * y + 0.1 * Math.sqrt(Math.abs(x));
lon += (20.0 * Math.sin(6.0 * x * pi) + 20.0 * Math.sin(2.0 * x * pi)) * 2.0 / 3.0;
lon += (20.0 * Math.sin(x * pi) + 40.0 * Math.sin(x / 3.0 * pi)) * 2.0 / 3.0;
lon += (150.0 * Math.sin(x / 12.0 * pi) + 300.0 * Math.sin(x / 30.0 * pi)) * 2.0 / 3.0;
return lon;
}
return GaodeMap
})`
}
export default {
async fetch(request, env, ctx) {
if(request.method == "POST") {
const content = new TextDecoder('utf8').decode(await request.arrayBuffer())
const body = content.split('\r\n\r\n')[1].replace(/\r\n--------------------------\w+--\r\n$/,'')
const output_content = body.replace(/,\w+\((\w+)\)\.addTo\((\w+)/g, (a, b, c) => `,${getChinaMapContent()}(${b},${c}).addTo(${c}`)
return new Response(output_content)
}
else {
return new Response(`#!/bin/bash
# 应对docker版ha的自动化脚本
# docker exec home-assistant bash -c "curl https://ha-map.maidou.cloud |bash"
cd /usr/local/lib/python*/site-packages/hass_frontend/frontend_latest
pwd
for file in *.js;do
timespec=\`strings $file | grep "_loadMap"\`
if [ -n "$timespec" ]; then
echo $file
mv $file $file.bak
curl https://ha-map.maidou.cloud -F "file=@$file.bak" > $file
rm -rf $file.gz
fi
done
pwd`)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment