简体   繁体   English

如何计算城市中两个区域之间的距离

[英]How to calculate distance between two areas in a city

I wanted to make an android app which provides the distance between two areas in a city. 我想制作一个提供城市中两个区域之间距离的android应用。

The user will have two input fields where he must enter the two area names he would like to find the distance. 用户将有两个输入字段,他必须在其中输入要查找距离的两个区域名称。

After he enters the area name. 在他输入区域名称之后。 My app should display the distance. 我的应用程序应显示距离。

How should I implement this? 我应该如何实施呢?

First of all you need to find latitude and longitude of that area name. 首先,您需要查找该区域名称的纬度和经度。

Convert both address in Location object, refer this . 转换Location对象中的两个地址,请参考this

Calculate distance by using distanceTo() 通过使用distanceTo()计算距离

location1.distanceTo(location2);

Returns the approximate distance in meters between this location and the given location. 返回此位置和给定位置之间的近似距离(以米为单位)。

refer this for distanceTo() detail. 有关distanceTo()的详细信息,请参考此内容

首先获取起点和终点的经度和纬度,然后使用Location类的distanceBetween方法计算距离,此处为链接http://developer.android.com/reference/android/location/Location.html#distanceBetween(double,% 20double,%20double,%20double,%20float [])

Use the Google Maps Directions API . 使用Google Maps Directions API You'll need to request the directions over HTTP. 您需要通过HTTP请求指导。 You can do this directly from Android, or via your own server. 您可以直接从Android或通过自己的服务器执行此操作。

For example, directions from Montreal to Toronto : 例如,从蒙特利尔到多伦多的路线:

GET http://maps.googleapis.com/maps/api/directions/json?origin=Toronto&destination=Montreal&sensor=false GET http://maps.googleapis.com/maps/api/directions/json?origin=Toronto&destination=Montreal&sensor=false

You'll end up with some JSON. 您最终会得到一些JSON。 In routes[].legs[].distance , you'll get an object like this: routes[].legs[].distance ,您将获得如下对象:

  "legs" : [
        {
           "distance" : {
              "text" : "542 km",
              "value" : 542389
           },

You can also get the polyline information directly from the response object. 您也可以直接从响应对象获取折线信息。

for more detail visit here. 有关更多详细信息,请访问此处。 Get the distance between two locations in android? 获取Android中两个位置之间的距离?

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

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