Last active
December 24, 2025 15:06
-
-
Save tomac4t/9dfaee7f9761d2eb13d6c2388d4f991e to your computer and use it in GitHub Desktop.
使用高德API搜索满足条件的两个POI
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import requests | |
| import time | |
| import json | |
| # 高德API密钥(请替换为您自己的密钥) | |
| AMAP_KEY = "YOUR_API_KEY_HERE" | |
| def search_poi_by_keyword(city, keyword): | |
| """使用关键字搜索POI""" | |
| base_url = "https://restapi.amap.com/v3/place/text" | |
| all_pois = [] | |
| page = 1 | |
| while True: | |
| params = { | |
| "key": AMAP_KEY, | |
| "output": "JSON", | |
| "city": city, | |
| "keywords": keyword, | |
| "page": page, | |
| "offset": 20 # 每页20条结果 | |
| } | |
| try: | |
| response = requests.get(base_url, params=params) | |
| data = response.json() | |
| if data["status"] != "1": | |
| print(f"API请求失败: {data.get('info', '未知错误')}") | |
| break | |
| pois = data.get("pois", []) | |
| all_pois.extend(pois) | |
| # 检查是否还有更多结果 | |
| total_count = int(data.get("count", 0)) | |
| if len(all_pois) >= total_count: | |
| break | |
| page += 1 | |
| time.sleep(0.34) # 控制请求频率(每秒不超过3次) | |
| except Exception as e: | |
| print(f"请求过程中发生错误: {e}") | |
| break | |
| return all_pois | |
| def search_around_poi(location, radius, poi2_keyword): | |
| """搜索指定位置周围的POI""" | |
| base_url = "https://restapi.amap.com/v3/place/around" | |
| results = [] | |
| page = 1 | |
| while True: | |
| params = { | |
| "key": AMAP_KEY, | |
| "output": "JSON", | |
| "location": location, | |
| "radius": radius, | |
| "page": page, | |
| "offset": 20 # 每页20条结果 | |
| } | |
| try: | |
| response = requests.get(base_url, params=params) | |
| data = response.json() | |
| if data["status"] != "1": | |
| print(f"周边搜索API请求失败: {data.get('info', '未知错误')}") | |
| break | |
| pois = data.get("pois", []) | |
| # 筛选包含POI2关键词的结果 | |
| for poi in pois: | |
| if poi2_keyword in poi.get("name", ""): | |
| results.append(poi) | |
| # 检查是否还有更多结果 | |
| total_count = int(data.get("count", 0)) | |
| if page * 20 >= total_count: | |
| break | |
| page += 1 | |
| time.sleep(0.34) # 控制请求频率(每秒不超过3次) | |
| except Exception as e: | |
| print(f"周边搜索过程中发生错误: {e}") | |
| break | |
| return results | |
| def main(): | |
| # 用户输入 | |
| poi1_name = input("请输入POI1名称: ") | |
| poi2_name = input("请输入POI2名称: ") | |
| radius = input("请输入搜索半径(米): ") | |
| city = input("请输入搜索城市/代码: ") | |
| print(f"\n正在搜索{city}的{poi1_name}...") | |
| # 第一步:搜索POI1 | |
| poi1_list = search_poi_by_keyword(city, poi1_name) | |
| if not poi1_list: | |
| print(f"在{city}未找到{poi1_name}") | |
| return | |
| print(f"找到{len(poi1_list)}个{poi1_name}地点") | |
| # 存储匹配结果 | |
| matched_results = [] | |
| # 第二步:对每个POI1进行周边搜索 | |
| for idx, poi1 in enumerate(poi1_list, 1): | |
| print(f"正在处理第{idx}个地点: {poi1.get('name')}") | |
| location = poi1.get("location") | |
| if not location: | |
| continue | |
| # 周边搜索 | |
| around_pois = search_around_poi(location, radius, poi2_name) | |
| # 第三步:记录匹配结果 | |
| for poi2 in around_pois: | |
| result = { | |
| "poi1_name": poi1.get("name"), | |
| "poi1_address": poi1.get("address"), | |
| "poi1_location": poi1.get("location"), | |
| "poi2_name": poi2.get("name"), | |
| "poi2_address": poi2.get("address"), | |
| "poi2_location": poi2.get("location"), | |
| "distance": poi2.get("distance", "未知") | |
| } | |
| matched_results.append(result) | |
| # 输出结果 | |
| if not matched_results: | |
| print(f"\n在{poi1_name}周围{radius}米内未找到{poi2_name}") | |
| return | |
| print(f"\n找到{len(matched_results)}个匹配结果:") | |
| for i, result in enumerate(matched_results, 1): | |
| print(f"\n[#{i}]") | |
| print(f"POI1名称: {result['poi1_name']}") | |
| print(f"POI1地址: {result['poi1_address']}") | |
| print(f"POI1经纬度: {result['poi1_location']}") | |
| print(f"POI2名称: {result['poi2_name']}") | |
| print(f"POI2地址: {result['poi2_address']}") | |
| print(f"POI2经纬度: {result['poi2_location']}") | |
| print(f"两者距离: {result['distance']}米") | |
| if __name__ == "__main__": | |
| # 替换为您的高德API密钥 | |
| AMAP_KEY = "YOUR_API_KEY_HERE" | |
| if AMAP_KEY == "YOUR_API_KEY_HERE": | |
| print("请先在代码中设置您的高德API密钥") | |
| else: | |
| main() |
Author
tomac4t
commented
Aug 29, 2025
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment