我的任务是计算从开始位置到我从 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()
据我所知,这是我自己做的。有没有人可以指出我正确的方向以获得总往返距离而不是从起始位置的各个距离?