簡體   English   中英

Android:如何獲取兩個地理坐標之間的步行距離?

[英]Android: how to get the walking distance between two geo coordinates?

我使用了這個查詢網址

http://maps.google.com/maps?q=from+A+to+B&output=kml

,在此問題的答案中給出。 但是在我嘗試之后,它無法與坐標一起使用。 它與地址名稱tho一起使用。 我想我可以使用Google的地理編碼來首先獲取地址。 但是我想知道是否還有另一種方法來獲取兩個坐標之間的步行距離?

我的新答案:)

使用Google Directions API

應該可以向http://maps.google.com/maps/api/directions/<json|xml>?<params>發出請求,並將坐標指定為origindestination參數。 我短暫地嘗試過,但是沒有返回結果。 按照他們的文檔,它應該可以工作,但是他們沒有詳細說明如何指定緯度和經度。 但是他們說這是可能的。 引用:

[...] origin(必填)-您希望從中計算路線的地址或 文本緯度/經度值 [...]

不過,這應該可以幫助您入門。 我建議使用JSON輸出格式。 它解析起來要簡單得多,並且應該占用更少的帶寬(它比XML少一些冗長)。

它可以正常工作:這是一個示例網址: http://maps.google.com/maps/api/directions/json?origin=49.75332,6.50322&destination=49.71482,6.49944&mode=walking&sensor=false : http://maps.google.com/maps/api/directions/json?origin=49.75332,6.50322&destination=49.71482,6.49944&mode=walking&sensor=false

我以前的答案

可以使用Haversine公式輕松確定直線距離。 如果您從Google檢索路線,則可以計算每個路段的距離並將其匯總。

前一段時間,我在博客文章( pythonpl / sql )中寫下了(著名的)算法( Haversine

這是python代碼的副本:

from math import sin, cos, radians, sqrt, atan2

    def lldistance(a, b):
   """
   Calculates the distance between two GPS points (decimal)
   @param a: 2-tuple of point A
   @param b: 2-tuple of point B
   @return: distance in m
   """
   r = 6367442.5             # average earth radius in m
   dLat = radians(a[0]-b[0])
   dLon = radians(a[1]-b[1])
   x = sin(dLat/2) ** 2 + \
       cos(radians(a[0])) * cos(radians(b[0])) *\
       sin(dLon/2) ** 2
   #original# y = 2 * atan2(sqrt(x), sqrt(1-x))
   y = 2 * asin(sqrt(x))
   d = r * y

   return d

將其轉換為Java應該很簡單。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM