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

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

问题描述

我使用了此查询URL

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?

推荐答案

我的新答案:)

使用 Google Directions API .

应该可以向http://maps.google.com/maps/api/directions/<json|xml>?<params>发出请求,并将坐标指定为origindestination参数. 我短暂地尝试了一下,但未返回任何结果.按照他们的文档,它应该可以工作,但是他们没有详细说明如何指定纬度和经度.但是他们说这是可能的.引用:

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

尽管如此,这应该可以帮助您入门.我建议使用JSON输出格式.解析起来要简单得多,并且应该占用更少的带宽(它比XML更为冗长).

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).

它有效:这是一个示例URL:http://maps.google.com/maps/api/directions/json?origin=49.75332,6.50322&destination=49.71482,6.49944&mode=walking&sensor=false

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

可以使用Haversine公式轻松确定直线距离.如果您从Google检索路线,则可以计算每个路段的距离并将其汇总.

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.

前一段时间,我写下了(著名的)算法( Haversine )在博客文章中( python pl /sql )

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

这是python代码的副本:

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

将其翻译成Java应该很简单.

Translating this to Java should be trivial.

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

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