How to use Python to call Baidu Map API to implement the geographical location query function?
With the development of the Internet, the acquisition and utilization of geographical location information is becoming more and more important. Baidu Maps is a very common and practical map application that provides a wealth of geographical location query services. This article will introduce how to use Python to call Baidu Map API to implement the geographical location query function, and attach a code example.
import requests def get_location(address): url = "http://api.map.baidu.com/geocoding/v3/" ak = "your_api_key" # 替换成你自己的百度地图API密钥 params = { "address": address, "ak": ak } response = requests.get(url, params=params) data = response.json() if data["status"] == 0: location = data["result"]["location"] print("经度:", location["lng"]) print("纬度:", location["lat"]) else: print("查询失败") address = input("请输入地理位置:") get_location(address)
In the code, we first define a get_location
function, which accepts an address parameter address
as input. Then, we assign the API address of Baidu Maps to the url
variable and replace it with our own key. Next, we use the requests.get
method to send an HTTP GET request, passing in the address parameters and key as query parameters. Finally, we parse the returned JSON data and output the queried latitude and longitude information.
The above are the steps and sample code for using Python to call Baidu Map API to implement the geographical location query function. Through Baidu Map API, we can obtain geographical location information very conveniently, which provides convenience for map application development. Hope this article can be helpful to you!
The above is the detailed content of How to use Python to call Baidu Map API to implement geographical location query function?. For more information, please follow other related articles on the PHP Chinese website!