# 使用高德API def geocodeG(address): par = {'address': address, 'key': 'cb649a25c1f81c1451adbeca73623251'} base = 'http://restapi.amap.com/v3/geocode/geo' response = requests.get(base, par) answer = response.json() if 'geocodes' in answer: GPS = answer['geocodes'][0]['location'].split(",") return GPS[0], GPS[1] else: return None, None # 使用百度API def geocodeB(address): base = url = "http://api.map.baidu.com/geocoder?address=" + address \ + "&output=json&key=f247cdb592eb43ebac6ccd27f796e2d2" response = requests.get(base) answer = response.json() if 'result' in answer: return answer['result']['location']['lng'], answer['result']['location']['lat'] else: return None, None def utl_get_lng_lat(address): try: longitude, latitude = geocodeB(address) if not longitude: longitude, latitude = geocodeG(address) # print(longitude, latitude) return longitude, latitude except Exception as e: print(repr(e)) return 0, 0
百度和高德地图接口取经纬度都有失效的时候,把两者结合起来用,才是解决之道。
最新推荐文章于 2021-03-23 14:57:02 发布