1

私の仕事は、開始場所から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()

これは私が自分で行う方法を知っている限りです。開始位置からの個々の距離ではなく、往復の合計距離を取得するために誰かが私を正しい方向に向けることができる可能性はありますか?

4

1 に答える 1

1

したがって、問題と解決策を分解します。現在、startとevent1の間の距離を取得できますが、event1とevent2の間の距離を取得することはできません。startとevent2の間の距離も取得できます。event1からevent2までの距離を取得するには、計算で何を変更する必要がありますか?

詳細リクエストの編集:

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

上の2つはforループの外側にあり、変更されていません(私が気付いたのは)。したがって、距離を計算するために新しい場所に移動する場合でも、開始座標に対して距離を計算します。

于 2012-04-03T23:45:48.670 に答える