Skip to content

Commit

Permalink
增加腾讯地图逆地理接口选项
Browse files Browse the repository at this point in the history
  • Loading branch information
dscao authored Nov 5, 2023
1 parent c236749 commit 4658940
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 10 deletions.
3 changes: 2 additions & 1 deletion custom_components/autoamap/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,8 +174,9 @@ async def async_step_user(self, user_input=None):
options=[
{"value": "none", "label": "none"},
{"value": "free", "label": "free"},
{"value": "gaode", "label": "gaode"},
{"value": "baidu", "label": "baidu"},
{"value": "gaode", "label": "gaode"}
{"value": "tencent", "label": "tencent"}
],
multiple=False,translation_key=CONF_ADDRESSAPI
)
Expand Down
33 changes: 30 additions & 3 deletions custom_components/autoamap/device_tracker.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,15 @@ async def async_update(self):
else:
self._address = addressdata['info']
self._coords_old = self._coords
elif self._addressapi == "tencent" and self._api_key:
_LOGGER.debug("tencent:"+self._api_key)
gcjdata = wgs84togcj02(self._coords[0], self._coords[1])
addressdata = await self._hass.async_add_executor_job(self.get_tencent_geocoding, gcjdata[1], gcjdata[0])
if addressdata['status'] == 0:
self._address = addressdata['result']['formatted_addresses']['recommend']
else:
self._address = addressdata['message']
self._coords_old = self._coords
elif self._addressapi == "free":
_LOGGER.debug("free")
gcjdata = wgs84togcj02(self._coords[0], self._coords[1])
Expand Down Expand Up @@ -256,6 +265,19 @@ def get_free_geocoding(self, lat, lng):
response = self.get_data(url)
_LOGGER.debug(response)
return response

def get_tencent_geocoding(self, lat, lng):
api_url = 'https://apis.map.qq.com/ws/geocoder/v1/'
location = str("{:.6f}".format(lat))+','+str("{:.6f}".format(lng))
sk = ''
if self._private_key:
params = '/ws/geocoder/v1/?get_poi=1&key='+self._api_key+'&location='+location+'&output=json'
sig = self.tencent_sk(params, self._private_key)
url = api_url+'?key='+self._api_key+'&output=json&get_poi=1&location='+location+'&sig='+sig
_LOGGER.debug(url)
response = self.get_data(url)
_LOGGER.debug(response)
return response

def get_baidu_geocoding(self, lat, lng):
api_url = 'https://api.map.baidu.com/reverse_geocoding/v3/'
Expand Down Expand Up @@ -292,6 +314,11 @@ def generate_signature(self, params, private_key):

def baidu_sn(self, params, private_key):
param_str = urllib.parse.quote(params, safe="/:=&?#+!$,;'@()*[]")
param_str += private_key # 加私钥
signature = hashlib.md5(urllib.parse.quote_plus(param_str).encode()).hexdigest() # 计算MD5摘要
return signature #根据私钥计算出web服务数字签名
param_str += private_key
signature = hashlib.md5(urllib.parse.quote_plus(param_str).encode()).hexdigest()
return signature

def tencent_sk(self, params, private_key):
param_str = params + private_key
signature = hashlib.md5(param_str.encode()).hexdigest()
return signature
5 changes: 3 additions & 2 deletions custom_components/autoamap/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"gps_conver": "conver gps from GJC02 to GPS84",
"update_interval_seconds": "Update_interval(10-3600 seconds)",
"sensors": "Sensors",
"addressapi": "Address acquisition interface, please register before using the API: [Amap](https://lbs.amap.com/dev/key) , [Baidu](https://lbsyun.baidu.com/) 。",
"addressapi": "Address acquisition interface, please register before using the API: [Amap](https://lbs.amap.com/dev/key) , [Baidu](https://lbsyun.baidu.com/) , [Tencent WebServiceAPI Key](https://lbs.qq.com/dev/console/application/mine) ",
"api_key": "Interface key, if it is empty, the address will not be obtained.",
"private_key": "Private key"
},
Expand All @@ -49,8 +49,9 @@
"options": {
"none": "none",
"free": "free",
"gaode": "gaode",
"baidu": "baidu",
"gaode": "gaode"
"tencent": "tencent"
}
}
},
Expand Down
9 changes: 5 additions & 4 deletions custom_components/autoamap/translations/zh-Hans.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@
"gps_conver": "原始数据为GCJ02(火星坐标系),坐标转化为GPS84",
"update_interval_seconds": "更新间隔时间(10-3600秒),建议设为90",
"sensors": "传感器",
"addressapi": "地址获取接口,使用 API 前请您先注册: [高德账号web服务key](https://lbs.amap.com/dev/key) , [百度账号服务端AK](https://lbsyun.baidu.com/apiconsole/key) 。",
"api_key": "接口密钥,为空时不获取地址",
"private_key": "数字签名时填写,否则留空。高德为安全密钥的私钥值,百度为sn校验方式SK值。"
"addressapi": "地址获取接口,使用 API 前请您先注册: [高德账号web服务key](https://lbs.amap.com/dev/key) , [百度账号服务端AK](https://lbsyun.baidu.com/apiconsole/key) , [腾讯WebServiceAPI Key](https://lbs.qq.com/dev/console/application/mine) ",
"api_key": "接口密钥,除免api_key接口外,为空时不获取地址",
"private_key": "数字签名时填写,否则留空。高德为安全密钥的私钥值,百度为sn校验方式SK值,腾讯为签名校验SK"
},
"description": "更多可选设置"
}
Expand All @@ -49,8 +49,9 @@
"options": {
"none": "不获取地址",
"free": "免api_key获取基础地理信息",
"gaode": "高德地图逆地理接口",
"baidu": "百度地图逆地理接口",
"gaode": "高德地图逆地理接口"
"tencent": "腾讯地图逆地理接口"
}
}
},
Expand Down

0 comments on commit 4658940

Please sign in to comment.