My task is to calculate the round trip distance from the start location to all of the event locations that I have grabbed from Facebook in miles and back to the start. My code so far:
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) latRad2= getLatRad(coordinates) 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()
This is as far as I know how to do on my own. Is there any chance someone could point me in the right direction to get the total round trip distance instead of the individual distances from the start location?
So decompose your problem vs your solution. Currently you can get the distance between start and event1, but you cant get the distance between event1 and event2. You can also get the distance between start and event2. What do you need to change in your calculation to get the distance from event1 to event2?
Edit for Elaboration Request:
latRad1 = getLatRad(start_latitude) longRad1= getLongRad(start_longitude)
dlon = longRad2 - longRad1 a = math.sin(dlat/2)**2 + math.cos(latRad1) * math.cos(latRad2) * math.sin(dlon/2)**2
The upper 2 are outside of your for loop and are not changed (that I noticed). Therefore when you move to a new location to calculate the distance, your still calculating it against the starting coords.