Python使用高德地图API判断给定地址是否在地理围栏/区域内

一、目前还是雏形,只写了几个功能函数,且功能函数未添加错误处理,
二、围栏范围有限制,围栏范围稍微大一点就不能创建,而且官方也没有围栏范围大小限制的说明。
import requests,json

key='5d47448e3c3aceff5db10319829c****'#申请的高德地图API KEY,申请地址:http://lbs.amap.com/dev/key/app

def get_boundary_gpsinfo(location='芙蓉区',key):#获取行政区域边界围栏经纬度信息
    parameters = { 'key': key,'address': location,'city':'changsha','subdistrict':0,'extensions':'all'}  
    base = 'http://restapi.amap.com/v3/config/district'
    response = requests.get(base, parameters)
    answer = response.json()
    return answer['districts'][0]["polyline"]#字符串类型


def address_gpsinfo(location='保利麓谷林语',key):#获取单个地址经纬度信息
    parameters = { 'key': key,'address': location,'city':'changsha'}  
    base = 'http://restapi.amap.com/v3/geocode/geo'  
    response = requests.get(base, parameters)
    answer = response.json()#{'geocodes': [{'level': '兴趣点', 'citycode': '0731', 'street': [], 'township': [], 'building': {'type': [], 'name': []}, 'formatted_address': '湖南省长沙市岳麓区保利麓谷林语', 'location': '112.873718,28.220873', 'number': [], 'province': '湖南省', 'adcode': '430104', 'city': '长沙市', 'district': '岳麓区', 'neighborhood': {'type': [], 'name': []}}], 'count': '1', 'status': '1', 'info': 'OK', 'infocode': '10000'}
    if answer['status'] == '1':
        if answer['geocodes']:
            return answer['geocodes'][0]['location']#字符串类型
    else:
        return '无法识别'

        
def creat_wl(key):#创建围栏
    parameters = {
    "name":"测试围栏",
    "points":"112.827784,28.248466;112.90486,28.238788;112.896448,28.203394;112.870527,28.192501;112.828127,28.18887",#实践当中使用变量
    "repeat": "Mon,Tues,Wed,Thur,Fri,Sat,Sun",
    "enable":"true",#创建围栏后同时启动围栏
    "valid_time":"2018-06-08",
    }
    base = 'http://restapi.amap.com/v4/geofence/meta?key='+key
    answer = requests.post(base, json=parameters).json()#{'data': {'gid': 'de90e05b-291c-4ad2-aa39-d5cb165cf3c6', 'id': '0', 'message': '成功', 'status': '0'}, 'errcode': 0, 'errdetail': None, 'errmsg': 'OK'}
    return answer['data']['gid']#字符串类型


#def cl_wl(key):#查询围栏状态,用处不大
#   parameters = {
#   'key':key,
#   'gid':'de90e05b-291c-4ad2-aa39-d5cb165cf3c6'
#   }
#   base = 'http://restapi.amap.com/v4/geofence/meta'
#   response = requests.get(base, parameters)
#   answer = response.json()#{'data': {'page_no': 1, 'page_size': 20, 'rs_list': [{'adcode': '0', 'alert_condition': '', 'center': '112.811004,28.327334', 'create_time': '2018-06-07 20:46:25', 'enable': True, 'fixed_date': '', 'gid': 'de90e05b-291c-4ad2-aa39-d5cb165cf3c6', 'id': '0', 'key': '5d47448e3c3aceff5db10319829c****', 'name': '测试围栏', 'points': '112.794052,28.342633;112.829071,28.341122;112.828728,28.313319;112.792164,28.312261', 'radius': 2492.03, 'repeat': 'Mon,Tues,Wed,Thur,Fri,Sat,Sun', 'time': '00:00,23:59', 'valid_time': '2018-06-08'}], 'total_record': 1}, 'errcode': 0, 'errdetail': None, 'errmsg': 'OK'}
#   return answer

    
def is_in(key,location='112.810618,28.324955,1528377482'):#判断是否在围栏
    imei = '865166027325128'
    parameters = {
    'key':key,
    'gid':'de90e05b-291c-4ad2-aa39-d5cb165cf3c6',
    'diu':imei,
    'locations':location
    }
    base = 'http://restapi.amap.com/v4/geofence/status'
    response = requests.get(base, parameters)
    answer = response.json()#{'data': {'fencing_event_list': [{'client_action': 'enter', 'client_status': 'in', 'enter_time': '2018-06-07 21:18:02', 'fence_info': {'fence_center': '112.811005,28.327333', 'fence_gid': 'de90e05b-291c-4ad2-aa39-d5cb165cf3c6', 'fence_name': '测试围栏'}}], 'status': 0}, 'errcode': 0, 'errdetail': None, 'errmsg': 'OK'}
    if answer['data']['fencing_event_list'][0]['client_status'] == 'in':
        print ('给定坐标在围栏里')
    else:
        print ('给定坐标不在围栏里')

你可能感兴趣的:(Python使用高德地图API判断给定地址是否在地理围栏/区域内)