Python gets the latitude and longitude of the address via Baidu Map API

Source: Internet
Author: User

API Address: http://developer.baidu.com/map/index.php?title=webapi/guide/webservice-geocoding

A simple API that can be used to obtain latitude and longitude by address (place name), or to obtain the location information of the perimeter via latitude.

AK is Baidu developer Apikey, registered address: http://developer.baidu.com/

#!/usr/bin/env Python3ImportRequestsImportJSONdefLOCATEBYADDR (Address, city=None):" "The city is optional, depending on the address to determine the latitude and longitude" "Items= {'Output':'JSON','AK':'a9f77xxxxxxxxxxxxxxxxxxxxxxxxxx','Address': Address}ifcity:items[' City'] =City R= Requests.get ('http://api.map.baidu.com/geocoder/v2/', params=items) Dictresult=json.loads (R.text)returndictresult['result'][' Location']if  notdictresult['Status']ElseNonedefLocatebylatlon (lat, lon, pois=0):" "determine the address based on latitude and longitude" "Items= {' Location': str (LAT) +','+ STR (LON),'AK':'a9f77xxxxxxxxxxxxxxxxxxxxxxxxxx','Output':'JSON'}    ifpois:items['POIs'] = 1R= Requests.get ('http://api.map.baidu.com/geocoder/v2/', params=items) Dictresult=json.loads (R.text)returndictresult['result']if  notdictresult['Status']ElseNonedefMain (): Address= Input ('Enter Address:') City= Input ('Enter City: (optional)') Result=locatebyaddr (address, city)Print(Result)if __name__=='__main__': Main ()

Run:

Python gets the latitude and longitude of the address via the Baidu Map API

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.