G
N
I
D
A
O
L

python根据地名获取经纬度(附两地距离计算代码)

本文是通过调用百度开放平台的API实现的经纬度查询。因此在使用前需要你去注册一个应用。

相关步骤

  1. 打开链接

    百度地图开放平台

  2. 创建应用

    创建应用

  3. 获取AK

    获取AK

  4. 替换程序中的AK,运行就能得到返回的JSON字符串

源码

# 导入相关的包
import requests #HTTP请求
import time 
import re
import json # 用于解析Json
import pandas as pd # 用于处理数据(数据分析相关的同学必须会的包)
  1. 数据准备

CSV文件中有一列是city,使用pandas直接读取即可:

df = pd.DataFrame()
df = pd.read_csv(r'./city.csv',encoding="gbk")
df['city']

得到的数据如下所示,总共有一百条,是我国2020年百强县的名单:

0      昆山市
1      江阴市
2     张家港市
3      晋江市
4      常熟市
      ... 
95     福安市
96     射阳县
97     邹平县
98     海城市
99     青州市
Name: city, Length: 100, dtype: object
user_geohash num city
0 94ek6ke 1028 昆山市
1 94ek6lj 980 江阴市
2 94ek6lw 919 张家港市
3 94ek6kn 908 晋江市
4 94ek6l5 517 常熟市
... ... ... ...
95 9q0ltaw 243 福安市
96 99s4avh 242 射阳县
97 9519pwt 240 邹平县
98 94jrlp3 240 海城市
99 946hodd 240 青州市

100 rows × 3 columns


执行calc_ll后返回的值如下所示:

showLocation&&showLocation({"status":0,"result":{"location":{"lng":120.98745249794995,"lat":31.390863425081866},"precise":0,"confidence":20,"comprehension":100,"level":"区县"}})

因为使用的是API,因此得到是上述的Json字符串。

根据地名计算经纬度的代码:

def calc_ll(x):
    Post_url = "http://api.map.baidu.com/geocoding/v3/?address=" + x + "&output=json&ak=(百度API开放平台获取的AK)&callback=showLocation" #自己想办法弄到key
    Post_data = {
        'address': x
    }
    Text = se.post(Post_url, data=Post_data).text.replace("'", '"').replace('/ ', '/')[27:-1] # 提取为Json格式,去掉‘showLocation&&showLocation()’这些额外的字符
    jsonValue = json.loads(Text) # 转化为Json对象
    # print(jsonValue) # 打印Json值
    if('result' in jsonValue):
        print(jsonValue['result']['location']['lng'])
        return [jsonValue['result']['location']['lng'],jsonValue['result']['location']['lat']]
    else:
        return ' '
    
address = df['city'].apply(calc_ll)
se = requests.session()

上述百强县执行代码后的结果:

{'status': 0, 'result': {'location': {'lng': 120.98745249794995, 'lat': 31.390863425081864}, 'precise': 0, 'confidence': 20, 'comprehension': 100, 'level': '区县'}}
120.98745249794995
{'status': 0, 'result': {'location': {'lng': 120.29156800752115, 'lat': 31.926044909769043}, 'precise': 0, 'confidence': 20, 'comprehension': 100, 'level': '区县'}}
120.29156800752115
{'status': 0, 'result': {'location': {'lng': 120.56155363871446, 'lat': 31.88114053634028}, 'precise': 0, 'confidence': 20, 'comprehension': 100, 'level': '区县'}}
120.56155363871446
{'status': 0, 'result': {'location': {'lng': 118.55843052000408, 'lat': 24.787824575144548}, 'precise': 0, 'confidence': 20, 'comprehension': 100, 'level': '区县'}}
118.55843052000408
{'status': 0, 'result': {'location': {'lng': 120.75949588665195, 'lat': 31.65953827674108}, 'precise': 0, 'confidence': 20, 'comprehension': 100, 'level': '区县'}}
120.75949588665195
{'status': 0, 'result': {'location': {'lng': 121.27259613631863, 'lat': 30.175256610720446}, 'precise': 0, 'confidence': 20, 'comprehension': 100, 'level': '区县'}}
121.27259613631863
{'status': 0, 'result': {'location': {'lng': 119.83160603216115, 'lat': 31.342956624267874}, 'precise': 0, 'confidence': 20, 'comprehension': 100, 'level': '区县'}}
119.83160603216115
{'status': 0, 'result': {'location': {'lng': 113.08755916595523, 'lat': 28.251818487944462}, 'precise': 0, 'confidence': 20, 'comprehension': 100, 'level': '区县'}}
113.08755916595523
{'status': 0, 'result': {'location': {'lng': 120.08158099305928, 'lat': 29.31114987541007}, 'precise': 0, 'confidence': 20, 'comprehension': 100, 'level': '区县'}}
120.08158099305928
{'status': 0, 'result': {'location': {'lng': 121.13559529350024, 'lat': 31.464599352977785}, 'precise': 0, 'confidence': 20, 'comprehension': 100, 'level': '区县'}}
121.13559529350024
{'status': 0, 'result': {'location': {'lng': 113.64939823850789, 'lat': 28.169832216070507}, 'precise': 0, 'confidence': 20, 'comprehension': 100, 'level': '区县'}}
113.64939823850789
{'status': 0, 'result': {'location': {'lng': 110.50542966826288, 'lat': 38.848544495220935}, 'precise': 0, 'confidence': 20, 'comprehension': 100, 'level': '区县'}}
110.50542966826288
{'status': 0, 'result': {'location': {'lng': 121.18843649817478, 'lat': 31.87361172603022}, 'precise': 0, 'confidence': 20, 'comprehension': 100, 'level': '区县'}}
121.18843649817478
{'status': 0, 'result': {'location': {'lng': 118.80341550029883, 'lat': 25.03694981503913}, 'precise': 0, 'confidence': 20, 'comprehension': 100, 'level': '区县'}}
118.80341550029883
{'status': 0, 'result': {'location': {'lng': 120.25340833196779, 'lat': 29.714934649194955}, 'precise': 0, 'confidence': 20, 'comprehension': 100, 'level': '区县'}}
120.25340833196779
{'status': 0, 'result': {'location': {'lng': 106.4075265551776, 'lat': 27.797752158542853}, 'precise': 0, 'confidence': 20, 'comprehension': 100, 'level': '区县'}}
106.4075265551776
{'status': 0, 'result': {'location': {'lng': 118.39257197205464, 'lat': 24.966362350467605}, 'precise': 0, 'confidence': 20, 'comprehension': 100, 'level': '区县'}}
118.39257197205464
{'status': 0, 'result': {'location': {'lng': 112.55840691564445, 'lat': 28.284104536209348}, 'precise': 0, 'confidence': 20, 'comprehension': 100, 'level': '区县'}}
112.55840691564445
{'status': 0, 'result': {'location': {'lng': 120.57946558109177, 'lat': 32.37785148610902}, 'precise': 0, 'confidence': 20, 'comprehension': 100, 'level': '区县'}}
120.57946558109177
{'status': 0, 'result': {'location': {'lng': 120.9925018561657, 'lat': 28.118179995228274}, 'precise': 0, 'confidence': 20, 'comprehension': 100, 'level': '区县'}}
120.9925018561657
{'status': 0, 'result': {'location': {'lng': 119.39046524748903, 'lat': 25.72634329903456}, 'precise': 0, 'confidence': 20, 'comprehension': 100, 'level': '区县'}}
119.39046524748903
{'status': 0, 'result': {'location': {'lng': 121.16058507482123, 'lat': 30.042734639570824}, 'precise': 0, 'confidence': 20, 'comprehension': 100, 'level': '区县'}}
121.16058507482123
{'status': 0, 'result': {'location': {'lng': 121.66347374677267, 'lat': 31.81423284964318}, 'precise': 0, 'confidence': 20, 'comprehension': 100, 'level': '区县'}}
121.66347374677267
{'status': 0, 'result': {'location': {'lng': 120.03953537126692, 'lat': 36.27034908804922}, 'precise': 0, 'confidence': 20, 'comprehension': 100, 'level': '区县'}}
120.03953537126692
{'status': 0, 'result': {'location': {'lng': 120.47443309577918, 'lat': 32.53919574424043}, 'precise': 0, 'confidence': 20, 'comprehension': 100, 'level': '区县'}}
120.47443309577918
{'status': 0, 'result': {'location': {'lng': 119.61250060471407, 'lat': 32.01592027416992}, 'precise': 0, 'confidence': 20, 'comprehension': 100, 'level': '区县'}}
119.61250060471407
{'status': 0, 'result': {'location': {'lng': 121.39258889457587, 'lat': 28.377655489850408}, 'precise': 0, 'confidence': 20, 'comprehension': 100, 'level': '区县'}}
121.39258889457587
{'status': 0, 'result': {'location': {'lng': 120.05859939046078, 'lat': 32.17747402371212}, 'precise': 0, 'confidence': 20, 'comprehension': 100, 'level': '区县'}}
120.05859939046078
{'status': 0, 'result': {'location': {'lng': 120.48536852807433, 'lat': 37.65255512549617}, 'precise': 0, 'confidence': 20, 'comprehension': 100, 'level': '区县'}}
120.48536852807433
{'status': 0, 'result': {'location': {'lng': 121.19243791023843, 'lat': 32.33587288615511}, 'precise': 0, 'confidence': 20, 'comprehension': 100, 'level': '区县'}}
121.19243791023843
{'status': 0, 'result': {'location': {'lng': 115.95046017276752, 'lat': 28.551604186108275}, 'precise': 0, 'confidence': 20, 'comprehension': 100, 'level': '区县'}}
115.95046017276752
{'status': 0, 'result': {'location': {'lng': 120.68656506738283, 'lat': 30.51792376977565}, 'precise': 0, 'confidence': 20, 'comprehension': 100, 'level': '区县'}}
120.68656506738283
{'status': 1, 'msg': 'Internal Service Error:无相关结果', 'results': []}
{'status': 0, 'result': {'location': {'lng': 120.66158218606775, 'lat': 27.783950736231652}, 'precise': 0, 'confidence': 20, 'comprehension': 100, 'level': '区县'}}
120.66158218606775
{'status': 0, 'result': {'location': {'lng': 120.28353203592053, 'lat': 31.988170075210594}, 'precise': 0, 'confidence': 20, 'comprehension': 100, 'level': '区县'}}
120.28353203592053
{'status': 0, 'result': {'location': {'lng': 120.57154315530762, 'lat': 30.63631118771037}, 'precise': 0, 'confidence': 20, 'comprehension': 100, 'level': '区县'}}
120.57154315530762
{'status': 0, 'result': {'location': {'lng': 118.0191233013022, 'lat': 34.34464551932591}, 'precise': 0, 'confidence': 20, 'comprehension': 100, 'level': '区县'}}
118.0191233013022
{'status': 0, 'result': {'location': {'lng': 118.70645736056483, 'lat': 40.00418392493125}, 'precise': 0, 'confidence': 20, 'comprehension': 100, 'level': '区县'}}
118.70645736056483
{'status': 0, 'result': {'location': {'lng': 104.03757322351338, 'lat': 30.648477494759213}, 'precise': 0, 'confidence': 75, 'comprehension': 0, 'level': '购物'}}
104.03757322351338
{'status': 0, 'result': {'location': {'lng': 120.32653706475246, 'lat': 32.872716312488244}, 'precise': 0, 'confidence': 20, 'comprehension': 100, 'level': '区县'}}
120.32653706475246
{'status': 0, 'result': {'location': {'lng': 122.49250463073764, 'lat': 37.171317939253235}, 'precise': 0, 'confidence': 20, 'comprehension': 100, 'level': '区县'}}
122.49250463073764
{'status': 0, 'result': {'location': {'lng': 118.65451902647484, 'lat': 24.737665160882703}, 'precise': 0, 'confidence': 20, 'comprehension': 100, 'level': '区县'}}
118.65451902647484
{'status': 0, 'result': {'location': {'lng': 121.98563754334361, 'lat': 39.63274074738802}, 'precise': 0, 'confidence': 20, 'comprehension': 100, 'level': '区县'}}
121.98563754334361
{'status': 0, 'result': {'location': {'lng': 119.85751406137442, 'lat': 32.917824213889254}, 'precise': 0, 'confidence': 20, 'comprehension': 100, 'level': '区县'}}
119.85751406137442
{'status': 0, 'result': {'location': {'lng': 113.44960935856369, 'lat': 30.33358775114635}, 'precise': 0, 'confidence': 20, 'comprehension': 100, 'level': '城市'}}
113.44960935856369
{'status': 0, 'result': {'location': {'lng': 111.24639840545701, 'lat': 39.87054026503683}, 'precise': 0, 'confidence': 20, 'comprehension': 100, 'level': '区县'}}
111.24639840545701
{'status': 0, 'result': {'location': {'lng': 119.46561561653884, 'lat': 32.787147020528316}, 'precise': 0, 'confidence': 20, 'comprehension': 100, 'level': '区县'}}
119.46561561653884
{'status': 0, 'result': {'location': {'lng': 112.9054740908161, 'lat': 30.40835793241892}, 'precise': 0, 'confidence': 20, 'comprehension': 100, 'level': '城市'}}
112.9054740908161
{'status': 0, 'result': {'location': {'lng': 117.01440416129005, 'lat': 35.40752093244999}, 'precise': 0, 'confidence': 20, 'comprehension': 100, 'level': '区县'}}
117.01440416129005
{'status': 0, 'result': {'location': {'lng': 117.16455781372443, 'lat': 31.712962132716886}, 'precise': 0, 'confidence': 20, 'comprehension': 100, 'level': '区县'}}
117.16455781372443
{'status': 0, 'result': {'location': {'lng': 113.02843100495258, 'lat': 34.754096888696935}, 'precise': 0, 'confidence': 20, 'comprehension': 100, 'level': '区县'}}
113.02843100495258
{'status': 0, 'result': {'location': {'lng': 116.94258562390337, 'lat': 34.76515195541547}, 'precise': 0, 'confidence': 20, 'comprehension': 100, 'level': '区县'}}
116.94258562390337
{'status': 0, 'result': {'location': {'lng': 119.19146705795117, 'lat': 32.27809048316069}, 'precise': 0, 'confidence': 20, 'comprehension': 100, 'level': '区县'}}
119.19146705795117
{'status': 0, 'result': {'location': {'lng': 118.7974153748082, 'lat': 36.86192174647094}, 'precise': 0, 'confidence': 20, 'comprehension': 100, 'level': '区县'}}
118.7974153748082
{'status': 0, 'result': {'location': {'lng': 113.49522360557991, 'lat': 34.14826652246125}, 'precise': 0, 'confidence': 20, 'comprehension': 100, 'level': '区县'}}
113.49522360557991
{'status': 0, 'result': {'location': {'lng': 117.17256521920983, 'lat': 35.120034809573916}, 'precise': 0, 'confidence': 20, 'comprehension': 100, 'level': '区县'}}
117.17256521920983
{'status': 0, 'result': {'location': {'lng': 119.13848201812112, 'lat': 26.156035214577336}, 'precise': 0, 'confidence': 20, 'comprehension': 100, 'level': '区县'}}
119.13848201812112
{'status': 0, 'result': {'location': {'lng': 109.75459043995566, 'lat': 39.570092815192666}, 'precise': 0, 'confidence': 20, 'comprehension': 100, 'level': '区县'}}
109.75459043995566
{'status': 0, 'result': {'location': {'lng': 113.74643447195358, 'lat': 34.40176637910559}, 'precise': 0, 'confidence': 20, 'comprehension': 100, 'level': '区县'}}
113.74643447195358
{'status': 1, 'msg': 'Internal Service Error:无相关结果', 'results': []}
{'status': 0, 'result': {'location': {'lng': 112.77847075950989, 'lat': 32.13472991431875}, 'precise': 0, 'confidence': 20, 'comprehension': 100, 'level': '区县'}}
112.77847075950989
{'status': 0, 'result': {'location': {'lng': 86.18149430993829, 'lat': 41.7323732214122}, 'precise': 0, 'confidence': 20, 'comprehension': 100, 'level': '区县'}}
86.18149430993829
{'status': 0, 'result': {'location': {'lng': 121.43643177294247, 'lat': 29.294317441105566}, 'precise': 0, 'confidence': 20, 'comprehension': 100, 'level': '区县'}}
121.43643177294247
{'status': 0, 'result': {'location': {'lng': 119.91749832197264, 'lat': 31.0325793268347}, 'precise': 0, 'confidence': 20, 'comprehension': 100, 'level': '区县'}}
119.91749832197264
{'status': 1, 'msg': 'Internal Service Error:无相关结果', 'results': []}
{'status': 0, 'result': {'location': {'lng': 112.60858070620743, 'lat': 35.072907226846525}, 'precise': 0, 'confidence': 20, 'comprehension': 100, 'level': '城市'}}
112.60858070620743
{'status': 0, 'result': {'location': {'lng': 118.36153652101795, 'lat': 34.37561095463685}, 'precise': 0, 'confidence': 20, 'comprehension': 100, 'level': '区县'}}
118.36153652101795
{'status': 0, 'result': {'location': {'lng': 119.9944142939757, 'lat': 36.78269225408944}, 'precise': 0, 'confidence': 20, 'comprehension': 100, 'level': '区县'}}
119.9944142939757
{'status': 0, 'result': {'location': {'lng': 113.39739094909922, 'lat': 34.54589798344474}, 'precise': 0, 'confidence': 20, 'comprehension': 100, 'level': '区县'}}
113.39739094909922
{'status': 0, 'result': {'location': {'lng': 120.44060165264568, 'lat': 37.36113714256612}, 'precise': 0, 'confidence': 20, 'comprehension': 100, 'level': '区县'}}
120.44060165264568
{'status': 0, 'result': {'location': {'lng': 111.456515837124, 'lat': 30.38440427431352}, 'precise': 0, 'confidence': 20, 'comprehension': 100, 'level': '区县'}}
111.456515837124
{'status': 0, 'result': {'location': {'lng': 119.94859307736998, 'lat': 37.18263660907421}, 'precise': 0, 'confidence': 20, 'comprehension': 100, 'level': '区县'}}
119.94859307736998
{'status': 0, 'result': {'location': {'lng': 119.17543320290359, 'lat': 31.950923490521372}, 'precise': 0, 'confidence': 20, 'comprehension': 100, 'level': '区县'}}
119.17543320290359
{'status': 0, 'result': {'location': {'lng': 120.4335951222883, 'lat': 27.523066788446513}, 'precise': 0, 'confidence': 20, 'comprehension': 100, 'level': '区县'}}
120.4335951222883
{'status': 0, 'result': {'location': {'lng': 120.2484219279141, 'lat': 29.295861065150863}, 'precise': 0, 'confidence': 20, 'comprehension': 100, 'level': '区县'}}
120.2484219279141
{'status': 0, 'result': {'location': {'lng': 114.21051607638233, 'lat': 36.70248828627979}, 'precise': 0, 'confidence': 20, 'comprehension': 100, 'level': '区县'}}
114.21051607638233
{'status': 0, 'result': {'location': {'lng': 122.97449548262718, 'lat': 39.686956078878055}, 'precise': 0, 'confidence': 20, 'comprehension': 100, 'level': '区县'}}
122.97449548262718
{'status': 0, 'result': {'location': {'lng': 119.41640221866636, 'lat': 36.00200885807568}, 'precise': 0, 'confidence': 20, 'comprehension': 100, 'level': '区县'}}
119.41640221866636
{'status': 0, 'result': {'location': {'lng': 117.77460624826666, 'lat': 35.9146198665876}, 'precise': 0, 'confidence': 20, 'comprehension': 100, 'level': '区县'}}
117.77460624826666
{'status': 0, 'result': {'location': {'lng': 120.26472938795023, 'lat': 23.0309091042802}, 'precise': 0, 'confidence': 25, 'comprehension': 100, 'level': '乡镇'}}
120.26472938795023
{'status': 0, 'result': {'location': {'lng': 116.1724489811672, 'lat': 23.30363452186039}, 'precise': 0, 'confidence': 20, 'comprehension': 100, 'level': '区县'}}
116.1724489811672
{'status': 0, 'result': {'location': {'lng': 114.72642585137605, 'lat': 22.991443112002415}, 'precise': 0, 'confidence': 20, 'comprehension': 100, 'level': '区县'}}
114.72642585137605
{'status': 0, 'result': {'location': {'lng': 114.98643626401979, 'lat': 30.10166780010339}, 'precise': 0, 'confidence': 20, 'comprehension': 100, 'level': '区县'}}
114.98643626401979
{'status': 0, 'result': {'location': {'lng': 104.47858183931757, 'lat': 25.715651258076416}, 'precise': 0, 'confidence': 20, 'comprehension': 100, 'level': '区县'}}
104.47858183931757
{'status': 0, 'result': {'location': {'lng': 121.23851297084417, 'lat': 28.14186298682908}, 'precise': 0, 'confidence': 20, 'comprehension': 100, 'level': '区县'}}
121.23851297084417
{'status': 0, 'result': {'location': {'lng': 116.45556614759954, 'lat': 33.93480147585089}, 'precise': 0, 'confidence': 20, 'comprehension': 100, 'level': '区县'}}
116.45556614759954
{'status': 0, 'result': {'location': {'lng': 116.1056050954973, 'lat': 38.717348630774524}, 'precise': 0, 'confidence': 20, 'comprehension': 100, 'level': '区县'}}
116.1056050954973
{'status': 0, 'result': {'location': {'lng': 117.17356009256213, 'lat': 32.483722623763164}, 'precise': 0, 'confidence': 20, 'comprehension': 100, 'level': '区县'}}
117.17356009256213
{'status': 1, 'msg': 'Internal Service Error:无相关结果', 'results': []}
{'status': 0, 'result': {'location': {'lng': 121.02257525525401, 'lat': 30.682240998057836}, 'precise': 0, 'confidence': 20, 'comprehension': 100, 'level': '区县'}}
121.02257525525401
{'status': 0, 'result': {'location': {'lng': 118.41349864392043, 'lat': 37.059638498906146}, 'precise': 0, 'confidence': 20, 'comprehension': 100, 'level': '区县'}}
118.41349864392043
{'status': 1, 'msg': 'Internal Service Error:无相关结果', 'results': []}
{'status': 0, 'result': {'location': {'lng': 120.93251060584033, 'lat': 30.83712219382042}, 'precise': 0, 'confidence': 20, 'comprehension': 100, 'level': '区县'}}
120.93251060584033
{'status': 0, 'result': {'location': {'lng': 114.29657265142765, 'lat': 23.178805592432578}, 'precise': 0, 'confidence': 20, 'comprehension': 100, 'level': '区县'}}
114.29657265142765
{'status': 0, 'result': {'location': {'lng': 103.72553503682038, 'lat': 36.10975375497273}, 'precise': 0, 'confidence': 20, 'comprehension': 100, 'level': '区县'}}
103.72553503682038
{'status': 0, 'result': {'location': {'lng': 119.65440636047963, 'lat': 27.094323508059915}, 'precise': 0, 'confidence': 20, 'comprehension': 100, 'level': '区县'}}
119.65440636047963
{'status': 0, 'result': {'location': {'lng': 120.26444564891948, 'lat': 33.78101359213001}, 'precise': 0, 'confidence': 20, 'comprehension': 100, 'level': '区县'}}
120.26444564891948
{'status': 0, 'result': {'location': {'lng': 117.74947848721935, 'lat': 36.8700322166973}, 'precise': 0, 'confidence': 20, 'comprehension': 100, 'level': '区县'}}
117.74947848721935
{'status': 0, 'result': {'location': {'lng': 109.12452136754654, 'lat': 21.481065838235963}, 'precise': 0, 'confidence': 20, 'comprehension': 100, 'level': '区县'}}
109.12452136754654
{'status': 0, 'result': {'location': {'lng': 118.48653672086745, 'lat': 36.690416508705376}, 'precise': 0, 'confidence': 20, 'comprehension': 100, 'level': '区县'}}
118.48653672086745

查看address的值:

address
0     [120.98745249794995, 31.390863425081864]
1     [120.29156800752115, 31.926044909769043]
2      [120.56155363871446, 31.88114053634028]
3     [118.55843052000408, 24.787824575144548]
4      [120.75949588665195, 31.65953827674108]
                        ...                   
95    [119.65440636047963, 27.094323508059915]
96     [120.26444564891948, 33.78101359213001]
97      [117.74947848721935, 36.8700322166973]
98    [109.12452136754654, 21.481065838235963]
99    [118.48653672086745, 36.690416508705376]
Name: city, Length: 100, dtype: object
df['pos'] = df1 # 保存到df文件中
user_geohash num city pos
0 94ek6ke 1028 昆山市 [120.98745249794995, 31.390863425081864]
1 94ek6lj 980 江阴市 [120.29156800752115, 31.926044909769043]
2 94ek6lw 919 张家港市 [120.56155363871446, 31.88114053634028]
3 94ek6kn 908 晋江市 [118.55843052000408, 24.787824575144548]
4 94ek6l5 517 常熟市 [120.75949588665195, 31.65953827674108]
... ... ... ... ...
95 9q0ltaw 243 福安市 [119.65440636047963, 27.094323508059915]
96 99s4avh 242 射阳县 [120.26444564891948, 33.78101359213001]
97 9519pwt 240 邹平县 [117.74947848721935, 36.8700322166973]
98 94jrlp3 240 海城市 [109.12452136754654, 21.481065838235963]
99 946hodd 240 青州市 [118.48653672086745, 36.690416508705376]

100 rows × 4 columns

拓展——计算两者之间的距离

在得到相关地名的经纬度之后,进一步就可以根据经纬度计算两点之间的距离,输入的是度数(°)


根据经纬度计算实际距离的代码:

import math
Earth_Radius=6378.137
def sin_square(a):
    result=math.sin(a)
    return result*result
def rad(ang):
    return math.pi*ang/180.0
#a,b参数是数组
def distance_calculate(a,b):
    lata,lnga=a[0],a[1]
    latb,lngb=b[0],b[1]

    radlata,radlnga=rad(lata),rad(lnga)
    radlatb, radlngb = rad(latb), rad(lngb)

    #纬度差
    minus_a=radlata-radlatb
    #经度差
    minus_b=radlnga-radlngb

    return 2*math.asin( math.sqrt(sin_square(minus_a/2) + math.cos(radlata)*math.cos(radlatb)*sin_square(minus_b/2)))*Earth_Radius

#坐标测试
#google
print(1000*distance_calculate((30.26848247,119.9499548338),(30.2688658749,119.9628533571)))
#baidu
print(1000*distance_calculate((30.274671,119.969392),(30.274261,119.956631)))
posted @ 2021-05-25 20:27  StimuMing  阅读(706)  评论(0编辑  收藏  举报