Article · 2024-06-23

Using Amap Geolocation in Python: Forward and Reverse Geocoding

Applications often need to convert between addresses and geographic coordinates. Amap's REST API provides two core operations: forward geocoding (address → latitude/longitude) and reverse geocoding (coordinates → address). This guide shows how to implement both in Python.

Getting Started

Register for an API Key

Amap's geolocation services require a valid API key from their developer platform:

Install the requests library

Python's requests library handles HTTP communication with the Amap API:

pip install requests

Forward Geocoding: Address to Coordinates

Forward geocoding converts an address string into latitude and longitude.

import requests

def geocode(address, api_key):
    url = "https://restapi.amap.com/v3/geocode/geo"
    params = {
        'address': address,  # 要地理编码的地址
        'key': api_key  # 申请的API Key
    }
    
    response = requests.get(url, params=params)
    
    if response.status_code == 200:
        result = response.json()
        if result['status'] == '1' and result['geocodes']:
            location = result['geocodes'][0]['location']
            lat, lon = location.split(',')
            print(f"地址: {address},经纬度: {lat}, {lon}")
        else:
            print("未找到匹配的地址。")
    else:
        print("请求失败,状态码:", response.status_code)

# 示例调用
api_key = "YOUR_API_KEY"  # 替换为你的高德API Key
address = "北京市朝阳区望京SOHO"
geocode(address, api_key)

The code sends a GET request to Amap's geocoding endpoint with the address and API key. The response contains a geocodes array; extract the first match to retrieve coordinates.

Address accuracy issues: Amap's results depend on database completeness. Partial or ambiguous addresses may return incorrect coordinates or multiple candidates. Provide more context (city, district) to narrow results, or iterate through candidates if available.

Rate limits: Amap's free tier restricts API calls. Frequent requests may be throttled; consider batching operations or upgrading your plan.

Reverse Geocoding: Coordinates to Address

Reverse geocoding converts latitude and longitude into a human-readable address.

import requests

def reverse_geocode(latitude, longitude, api_key):
    url = "https://restapi.amap.com/v3/geocode/regeo"
    params = {
        'location': f"{longitude},{latitude}",  # 经度和纬度
        'key': api_key,  # 申请的API Key
        'radius': 1000,  # 查询半径,单位为米
        'extensions': 'all'  # 返回更多扩展信息
    }
    
    response = requests.get(url, params=params)
    
    if response.status_code == 200:
        result = response.json()
        if result['status'] == '1' and result['regeocode']:
            address = result['regeocode']['formatted_address']
            print(f"坐标: {latitude}, {longitude},地址: {address}")
        else:
            print("未找到匹配的地址。")
    else:
        print("请求失败,状态码:", response.status_code)

# 示例调用
api_key = "YOUR_API_KEY"  # 替换为你的高德API Key
latitude = 39.908
longitude = 116.3972
reverse_geocode(latitude, longitude, api_key)

The code passes coordinates in longitude,latitude format. The radius parameter (in meters) defines the search area, and extensions: all returns detailed address data and nearby points of interest.

Coordinate system mismatches: Amap's reverse geocoding operates on the GCJ-02 coordinate system. If you have WGS-84 coordinates (from GPS receivers or international APIs), convert them to GCJ-02 before querying; otherwise, results will be displaced or inaccurate.

Precision limits: Remote areas often lack fine-grained address data. Reverse geocoding may return only city or district level. If precision is critical, increase radius or check the returned POI list for nearby landmarks.

Managing High Request Volume

Under heavy load, frequent geocoding calls become a bottleneck and risk exceeding rate limits:

© 2026 Yuxu Ge ·