Android:如何获得两个地理坐标之间的步行距离? [英] Android: how to get the walking distance between two geo coordinats?

查看:99
本文介绍了Android:如何获得两个地理坐标之间的步行距离?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用了此查询网址


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


,这个答案在问题。但是在我尝试之后,它不适用于坐标。它适用于地址名称tho。我想我可以使用谷歌的地理编码来获取地址。但我不知道是否有另一种方式来获得两个坐标之间的步行距离?

解决方案

我的新答案:)



使用 Google Directions API a>。



应该可以向请求http://maps.google.com/maps/api/directions/< ; json | xml>?< params> 并指定坐标为 origin destination 参数。 我简单地试了一下,但没有返回任何结果。沿着他们的文档它应该可以工作,但是他们没有详细解释如何指定纬度和经度。但他们说这是可能的。 Quote:


[...] 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

我的上一个答案



使用Haversine公式可以很容易地确定直线距离。如果您从Google获取路线,那么您可以计算每个路段的距离并将它们相加。

后来,我写下了(知名的)算法( Haversine )在博客文章( python and pl / sql

以下是python代码的副本:

 从数学导入sin,cos,radians,sqrt,atan2 

def lldistance(a,b):

计算两个GPS点之间的距离(十进制)
@param a:点的2元组A
@param b:B 2元组b
@return:以m
为单位的距离b
r = 6367442.5#平均地球半径m
dL at =弧度(a [0] -b [0])
dLon =弧度(a [1] -b [1])$ ​​b $ bx = sin(dLat / 2)** 2 + \
cos(弧度(a [0]))* cos(弧度(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应该是微不足道的。


I used this query URL

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

,which is given in the answer to this question. But after I tried it, it doesn't work with coordinates. It works with address names tho. I guess I could use google's geocoding to get the addresses first. But I wonder if there is another way to get the walking distance between two coordinates?

解决方案

My New Answer :)

Use the Google Directions API.

It should be possible to make a request to http://maps.google.com/maps/api/directions/<json|xml>?<params> and specifying the coords as origin and destination parameter. I briefly tried it but it returned no results. Along their docs it should work, but they don't explain in detail how to specify latitude and longitude. But they say it's possible. Quote:

[...] origin (required) — The address or textual latitude/longitude value from which you wish to calculate directions [...]

Nevertheless, this should get you started. I would suggest going with the JSON output format. It's much simpler to parse and should use up less bandwidth (it's less verbose as XML).

It works: Here's an example URL: http://maps.google.com/maps/api/directions/json?origin=49.75332,6.50322&destination=49.71482,6.49944&mode=walking&sensor=false

My Previous Answer

The straight line distance can easily be determined using the Haversine formula. If you retrieve the route from Google, then you could calculate the distance of each segment and sum them up.

A while back, I wrote down the (well-known) algorithm (Haversine) in a blog post (python and pl/sql)

Here's the copy of the python code:

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

Translating this to Java should be trivial.

这篇关于Android:如何获得两个地理坐标之间的步行距离?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆