Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jquery-ui/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用python计算往返距离_Python_Graph Algorithm - Fatal编程技术网

用python计算往返距离

用python计算往返距离,python,graph-algorithm,Python,Graph Algorithm,我的任务是计算从开始地点到我从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(longit

我的任务是计算从开始地点到我从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()

这是我自己所知道的。有没有可能有人能给我指出正确的方向,以获得总的往返距离,而不是从起点到终点的单个距离?

因此,分解您的问题和解决方案。 当前,您可以获取开始和事件1之间的距离,但无法获取事件1和事件2之间的距离。您还可以获得开始和事件2之间的距离。要获得从事件1到事件2的距离,您需要在计算中更改什么

编辑细化请求:

latRad1=getLatRad(起始纬度) longRad1=getLongRad(起始经度)

dlon=longRad2-longRad1 a=数学sin(dlat/2)**2+数学cos(latRad1)*数学cos(latRad2)*数学sin(dlon/2)**2


上2个在for循环之外,没有更改(我注意到)。因此,当你移动到一个新的位置来计算距离时,你仍然会根据起始坐标来计算距离。

因此,分解你的问题和解决方案。 当前,您可以获取开始和事件1之间的距离,但无法获取事件1和事件2之间的距离。您还可以获得开始和事件2之间的距离。要获得从事件1到事件2的距离,您需要在计算中更改什么

编辑细化请求:

latRad1=getLatRad(起始纬度) longRad1=getLongRad(起始经度)

dlon=longRad2-longRad1 a=数学sin(dlat/2)**2+数学cos(latRad1)*数学cos(latRad2)*数学sin(dlon/2)**2


上2个在for循环之外,没有更改(我注意到)。因此,当您移动到一个新位置来计算距离时,您仍然会根据起始坐标来计算距离。

您打算计算所有点的“最佳”往返距离、按特定顺序的往返距离,还是“任何”到达所有点的往返距离?+1表示ybakos答案。旅行推销员问题,你需要想出一个启发式的方法,对吗?我猜事件有一个日历顺序(可能是eventids数组中ID的顺序),所以没有“最优”的概念在这个问题的背景下。我需要通过日历排序来完成,就像内切哈格说的那样,你可以确定活动时间表是分开的。如果两个活动相隔2小时,并且从活动1到活动2需要2个多小时,那么任何时间表都是不可行的,对吗?或者我们让这变得比支持的更复杂sed to be?你打算计算所有点的“最佳”往返距离吗?是按特定顺序的往返距离,还是按所有点的“任何”往返距离?+1为ybakos答案。旅行推销员问题,你需要提出一个启发式方法,对吗?我猜事件有一个日历顺序(可能是eventids数组中的ID,因此没有“最佳”的概念)在这个问题的背景下。我需要通过日历排序来完成,就像内切哈格说的那样,你可以确定活动时间表是分开的。如果两个活动相隔2小时,并且从活动1到活动2需要2个多小时,那么任何时间表都是不可行的,对吗?或者我们让这变得比支持的更复杂你想怎么样?