获取Android的两个位置之间的距离? [英] Get the distance between two locations in android?

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

问题描述

我需要得到在两个位置的距离,但我需要得到像蓝线在画面的距离。

i need to get distance between two location, but i need to get distance like blue line in the picture.

我试下:

    public double getDistance(LatLng LatLng1, LatLng LatLng2) {
    double distance = 0;
    Location locationA = new Location("A");
    locationA.setLatitude(LatLng1.latitude);
    locationA.setLongitude(LatLng1.longitude);
    Location locationB = new Location("B");
    locationB.setLatitude(LatLng2.latitude);
    locationB.setLongitude(LatLng2.longitude);
    distance = locationA.distanceTo(locationB);
    return distance;

}

但我得到的红线距离。

but i get red line distance.

推荐答案

使用谷歌地图路线API 。您需要请求指示通过HTTP。您可以直接从Android或通过自己的服务器做到这一点。

Use the Google Maps Directions API. You'll need to request the directions over HTTP. You can do this directly from Android, or via your own server.

例如,方向从<一个href="http://maps.googleapis.com/maps/api/directions/json?origin=Toronto&destination=Montreal&sensor=false">Montreal多伦多:

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

您最终会得到一些JSON。在路线[]腿[]距离,你会得到这样一个对象:

You'll end up with some JSON. In routes[].legs[].distance, you'll get an object like this:

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

您也可以直接从响应对象获取的折线的信息。

You can also get the polyline information directly from the response object.

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

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