This is the 20th day of my participation in the Genwen Challenge
A list,
Path planning includes walking, bus, driving, cycling and other different ways. Today, with the help of Amap Web service API, travel route planning is realized.
Train of thought
Get latitude and longitude based on location
Call the API to get the route based on latitude and longitude
Route data is processed for easy browsing
Amap API
The corresponding link
https://lbs.amap.com/api/webservice/guide/api/direction
Copy the code
Go to amap open platform to register an account, and create your own project, the system will assign you a key value.
Select Web Services in Development Support, and select Web Services API
Two, get longitude and latitude
Input location, output latitude and longitude
def get_location_x_y(place) :
#place = input(" please enter the address you want to query ")
url = 'https://restapi.amap.com/v3/geocode/geo?parameters'
parameters = {
'key':'Autonavi official website to obtain key'.'address':'%s' % place
}
page_resource = requests.get(url,params=parameters)
text = page_resource.text Get the data in JSON format
data = json.loads(text) Put the data into dictionary format
location = data["geocodes"] [0] ['location']
return location
if __name__ == '__main__':
print(get_location_x_y(Beijing West Railway Station))
Copy the code
To get the results
Iii. Route Planning (four ways)
Get starting point, end point longitude and latitude, travel mode
from_place = input("Please enter your starting address.")
from_location = get_location_x_y(from_place)
to_place = input("Please enter destination")
to_location = get_location_x_y(to_place)
type = input("Mode of travel (1. Bus, 2. Walking, 3. Driving, 4. Cycling), please enter numbers.")
Copy the code
Get travel routes
Type is the way to get around (4 ways correspond to 1, 2, 3, 4)
Amap Web service API links are different for different modes of travel
url="https://restapi.amap.com"
if type= ="1":
url = url+ "/v3/direction/transit/integrated"
elif type= ="2":
url = url + "/v3/direction/walking"
elif type= ="3":
url = url + "/v3/direction/driving"
elif type= ="4":
url = url + "/v4/direction/bicycling"
Copy the code
Request parameters
parameters = {
'key': 'Autonavi official website to obtain key'.'origin': str(from_location),
'destination': str(to_location),
'extensions':'all'.'output':'json'.'city':'020',}Copy the code
From_location is the starting latitude and longitude, to_location is the ending latitude and longitude, output is the format of the data to be returned, in this case json (there are many other formats, such as XML)
The data processing
if type= ="1":
txt = txt['route'] ['transits']
for i in txt:
i = i['segments'] [0] ['bus'] ['buslines'] [0] ['name']
print(i)
elif type= ="2":
txt = txt['route'] ['paths'] [0] ['steps']
for i in txt:
i = i['instruction']
print(i)
elif type= ="3":
txt = txt['route'] ['paths'] [0] ['steps']
for i in txt:
i = i['instruction']
print(i)
elif type= ="4":
txt = txt['data'] ['paths'] [0] ['steps']
for i in txt:
i = i['instruction']
print(i)
Copy the code
According to different travel modes, data keys obtained are different, so corresponding processing is needed to facilitate browsing.
Fourth, the demonstration effect
1, bus
2, walk
3, driving
4, cycling
Five, the end
OK, so that’s Python’s route planning for different modes of travel with the help of amap Web services.