简体   繁体   English

Android:如何获取两个地理坐标之间的步行距离?

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

I used this query URL 我使用了这个查询网址

http://maps.google.com/maps?q=from+A+to+B&output=kml 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. 它与地址名称tho一起使用。 I guess I could use google's geocoding to get the addresses first. 我想我可以使用Google的地理编码来首先获取地址。 But I wonder if there is another way to get the walking distance between two coordinates? 但是我想知道是否还有另一种方法来获取两个坐标之间的步行距离?

My New Answer :) 我的新答案:)

Use the Google Directions API . 使用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. 应该可以向http://maps.google.com/maps/api/directions/<json|xml>?<params>发出请求,并将坐标指定为origindestination参数。 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 [...] [...] origin(必填)-您希望从中计算路线的地址或 文本纬度/经度值 [...]

Nevertheless, this should get you started. 不过,这应该可以帮助您入门。 I would suggest going with the JSON output format. 我建议使用JSON输出格式。 It's much simpler to parse and should use up less bandwidth (it's less verbose as XML). 它解析起来要简单得多,并且应该占用更少的带宽(它比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 它可以正常工作:这是一个示例网址: 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

My Previous Answer 我以前的答案

The straight line distance can easily be determined using the Haversine formula. 可以使用Haversine公式轻松确定直线距离。 If you retrieve the route from Google, then you could calculate the distance of each segment and sum them up. 如果您从Google检索路线,则可以计算每个路段的距离并将其汇总。

A while back, I wrote down the (well-known) algorithm ( Haversine ) in a blog post ( python and pl/sql ) 前一段时间,我在博客文章( pythonpl / sql )中写下了(著名的)算法( Haversine

Here's the copy of the python code: 这是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

Translating this to Java should be trivial. 将其转换为Java应该很简单。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM