我的任务是计算从起始位置到我从 Facebook 获取的所有事件位置(以英里为单位)并返回起始位置的往返距离。 到目前为止我的代码:
import json
import re
from urllib import urlopen
import math
def getLatRad(latitude):
return float(latitude) * (math.pi/180.0)
def getLongRad(longitude):
return float(longitude) * (math.pi/180.0)
tuplist =[]
finaltuplist = []
#start_latitude = float(input('Pls enter the latitude co-ordinate of your starting location:'))
#start_longitude = float(input('Pls enter the longitude co-ordinate of your starting location:'))
start_latitude = 41.721194054071
start_longitude = -73.934258235003
longRad1= getLongRad(start_longitude)
latRad1 = getLatRad(start_latitude)
def main():
eventids = [
'264100516989470',
'129843580476568',
'158475914271199',
]
for event in eventids:
f = urlopen('http://graph.facebook.com/%s' % event)
d = json.load(f)
name = d['name']
longitude = d["venue"]["longitude"]
latitude = d["venue"]["latitude"]
tuplist.append((name,longitude,latitude))
for coordinates in tuplist:
longRad2= getLongRad(coordinates[1])
latRad2= getLatRad(coordinates[2])
dlon = longRad2 - longRad1
dlat = latRad2 - latRad1
a = math.sin(dlat/2)**2 + math.cos(latRad1) * math.cos(latRad2) * math.sin(dlon/2)**2
c = 2 * math.asin(math.sqrt(a))
m = 3960 * c
sum = m + m
print sum
if __name__ == '__main__':
main()
据我所知,这是我自己能做的。是否有人可以为我指明正确的方向,以获取总往返距离,而不是距起始位置的单独距离?
最佳答案
所以分解你的问题和你的解决方案。 目前可以获取start和event1之间的距离,但是无法获取event1和event2之间的距离。您还可以获得 start 和 event2 之间的距离。您需要在计算中更改哪些内容才能获得事件 1 到事件 2 的距离?
编辑详细说明请求:
latRad1 = getLatRad(start_latitude) longRad1= getLongRad(start_longitude)
dlon = 长拉德2 - 长拉德1 a = math.sin(dlat/2)**2 + math.cos(latRad1) * math.cos(latRad2) * math.sin(dlon/2)**2
上面的 2 个在 for 循环之外并且没有改变(我注意到)。因此,当您移动到新位置来计算距离时,您仍然会根据起始坐标来计算距离。
关于python - 用python计算往返距离,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10003036/