如何获得公里的距离在机器人? [英] How to get Distance Kilometer in android?

查看:101
本文介绍了如何获得公里的距离在机器人?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很新的谷歌地图我要计算的android两地之间的距离。

I am very new to Google maps I want calculate the distance between two places in android.

有关,我得到的,我写了下面的code两地纬度和滞后的位置:

For that I get the two places lat and lag positions for that I write the following code:

private double getDistance(double lat1, double lat2, double lon1, double lon2) {
    double dLat = Math.toRadians(lat2 - lat1);
    double dLon = Math.toRadians(lon2 - lon1);
    double a = Math.sin(dLat / 2) * Math.sin(dLat / 2) + Math.cos(Math.toRadians(lat1)) * Math.cos(Math.toRadians(lat2)) * Math.sin(dLon / 2) * Math.sin(dLon / 2);
    double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
    double temp = 6371 * c;
    temp=temp*0.621;
    return temp;
}

以上code不能给两地之间的精确距离。什么是另一种方式来发现的距离,请给我任何建议。

The above code can't give the accurate distance between two places. What is the another way to find distance please give me any suggestions.

推荐答案

使用下面这个code你发现的距离,但要在千米转换。

Using following this code you find distance but you want to convert in kilometer.

double distance;
Location locationA = new Location("point A");
locationA.setLatitude(latA);
locationA.setLongitude(lngA);
Location locationB = new Location("point B");
locationB.setLatitude(latB);
LocationB.setLongitude(lngB);
distance = locationA.distanceTo(locationB);

这篇关于如何获得公里的距离在机器人?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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