geopy是Python關(guān)于地理位置的一個(gè)第三方庫(kù),用這個(gè)庫(kù)來(lái)進(jìn)行地址位置信息的查詢和轉(zhuǎn)換非常方便,本文介紹關(guān)于geopy的常用的幾種用法
康巴什網(wǎng)站制作公司哪家好,找成都創(chuàng)新互聯(lián)!從網(wǎng)頁(yè)設(shè)計(jì)、網(wǎng)站建設(shè)、微信開(kāi)發(fā)、APP開(kāi)發(fā)、響應(yīng)式網(wǎng)站等網(wǎng)站項(xiàng)目制作,到程序開(kāi)發(fā),運(yùn)營(yíng)維護(hù)。成都創(chuàng)新互聯(lián)于2013年開(kāi)始到現(xiàn)在10年的時(shí)間,我們擁有了豐富的建站經(jīng)驗(yàn)和運(yùn)維經(jīng)驗(yàn),來(lái)保證我們的工作的順利進(jìn)行。專注于網(wǎng)站建設(shè)就選成都創(chuàng)新互聯(lián)。
pip install geopy
>>> import json, logging
>>> from geopy.geocoders import Nominatim
>>> geolocator = Nominatim()
>>> location = geolocator.geocode("北京天安門(mén)")
>>> print location.address
天安門(mén), 1, 西長(zhǎng)安街, 崇文, 北京市, 東城區(qū), 北京市, 100010, 中國(guó)
>>> print (location.latitude, location.longitude)
(39.90733345, 116.391244079988)
>>> print json.dumps(location.raw, indent=4, ensure_ascii=False, encoding='utf8')
{
"display_name": "天安門(mén), 1, 西長(zhǎng)安街, 崇文, 北京市, 東城區(qū), 北京市, 100010, 中國(guó)",
"importance": 0.00025,
"place_id": "74005413",
"lon": "116.391244079988",
"lat": "39.90733345",
"osm_type": "way",
"licence": "Data ? OpenStreetMap contributors, ODbL 1.0. https://osm.org/copyright",
"osm_id": "25097203",
"boundingbox": [
"39.9072273",
"39.9075343",
"116.3906566",
"116.3918428"
],
"type": "yes",
"class": "building"
}
>>> import json, logging
>>> from geopy.geocoders import Nominatim
>>> geolocator = Nominatim()
>>> location = geolocator.reverse("39.90733345,116.391244079988")
>>> print location.address
天安門(mén), 1, 西長(zhǎng)安街, 崇文, 北京市, 東城區(qū), 北京市, 100010, 中國(guó)
>>> print json.dumps(location.raw, indent=4, ensure_ascii=False, encoding='utf8')
{
"display_name": "天安門(mén), 1, 西長(zhǎng)安街, 崇文, 北京市, 東城區(qū), 北京市, 100010, 中國(guó)",
"place_id": "74005413",
"lon": "116.391244079988",
"boundingbox": [
"39.9072273",
"39.9075343",
"116.3906566",
"116.3918428"
],
"osm_type": "way",
"licence": "Data ? OpenStreetMap contributors, ODbL 1.0. https://osm.org/copyright",
"osm_id": "25097203",
"lat": "39.90733345",
"address": {
"building": "天安門(mén)",
"city": "北京市",
"house_number": "1",
"country": "中國(guó)",
"suburb": "東城區(qū)",
"state": "北京市",
"postcode": "100010",
"country_code": "cn",
"road": "西長(zhǎng)安街"
}
}