我有一个包含不同城市和特定la
t和long
值的文本文件,以及可以获取初始原点位置和目标位置的代码,并使用{{返回两点之间的距离1}}。我正在努力的是如何建立一个如下所示的距离矩阵:
Google maps API
文本文件如下所示:
x loc1 loc2 loc3 ...
loc1 [0 3 4]
loc2 [5 0 7]
loc3 [9 2 0]
....
有关如何处理此问题的任何建议?我觉得我让它变得比以前更加复杂。我想到只是迭代一个循环,它占据第一个位置,然后遍历所有其他位置,并在循环回到开头之前将第一行追加到末尾。假设知道文本文件中的行数,这很好,但是重组结果数组会很麻烦。
答案 0 :(得分:0)
这是我正在寻找的答案,万一有人好奇。
xxxxxxxxxxxxxxxxxx答案:
所以你有一个zip(n)列表,其中n = 1 ... N
每个zip(n)包含一个lat值和long值 - 比如zip(n).lat = 44.22; zip(n).long = 39.17
距离矩阵则为M(N,N) - 一个方形矩阵,其中对角线始终为0,因为距离zip(n)== zip(n)的距离。此外,矩阵的上半部分与下半部分相同。 zip1到zipX的含义与zipX到zip1的含义相同。因此,您只需要计算矩阵的1/2 - 不包括对角线。您可以将其用作错误检查。
他的代码相当混乱而不是python,但逻辑过程仍然是相同的,这就是我所坚持的。如果人们对它感兴趣,我会在下面粘贴他的代码(用java编写)
for循环是:
Map<int,int> M = new HashMap<int,int>(); - in Java all values are initialized to 0 for you; index goes from 0...N-1
for (int r = 1; r < N-1; r++) { // Skip the two corners (0,0) and (N-1,N-1) which are 0
for (int c = r + 1; c < N; c++) {
M(r,c) = getDistance(zip(r),zip(c));
}; // end for c
}; // end for r
You can also use an Map of Integers where the key is the string r+","+c
Map<String,Integer> M = new HashMap<String,Integer>();
for (int r = 1; r < N-1; r++) { // Skip the two corners (0,0) and (N-1,N-1) which are 0
M.put(r+","+r,new Integer(0)); // diagonal
for (int c = r + 1; c < N; c++) {
M.put(r+","+c,new Integer(getDistance(zip(r),zip(c))); // Upper half
M.put(c+","+r,new Integer(getDistance(zip(r),zip(c))); // Lower half
}; // end for c
}; // end for r
M.put("0,0",new Integer(0)); // upper left corner
M.put((N-1)+","+(N-1),new Integer(0)); // lower right corner