How can I get the distance between two point by latlng?(如何通过 latlng 获得两点之间的距离?)
本文介绍了如何通过 latlng 获得两点之间的距离?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想通过 latlng 获取从 A 到 B 的距离.当我使用以下代码运行我的应用程序时,它返回错误的结果.Fx 我试了A(31.172740,115.0081630),然后B(30.6055980,114.3603140),结果大约是3111km.但是正确的距离大约是134km.我的代码是:
I want to get the distance from A to B by latlng.When i run my app with the following code,it return a wrong result. Fx i tryed A( 31.172740,115.0081630) ,then B(30.6055980,114.3603140),the result is about 3111km.but the right distance is about 134km. My code is:
class btnListener implements OnClickListener{
public void onClick(View v) {
lat_a=lat_A.getText().toString();
lng_a=lng_A.getText().toString();
lat_b=lat_B.getText().toString();
lng_b=lng_B.getText().toString();
double a1,n1,a2,n2;
a1=30.6055980;
a2=Double.parseDouble(lat_b);
n2=Double.parseDouble(lng_b);
double R=6371;
double D=Math.acos(Math.sin(a1)*Math.sin(a2)+Math.cos(a1)*Math.cos(a2)*Math.cos(n2-n1));
Toast.makeText(getApplication(), "the distance is"+String.valueOf(D*R)+"km from A to B", Toast.LENGTH_SHORT).show();
}
}
推荐答案
试试下面的代码.
public float distance (float lat_a, float lng_a, float lat_b, float lng_b )
{
double earthRadius = 3958.75;
double latDiff = Math.toRadians(lat_b-lat_a);
double lngDiff = Math.toRadians(lng_b-lng_a);
double a = Math.sin(latDiff /2) * Math.sin(latDiff /2) +
Math.cos(Math.toRadians(lat_a)) * Math.cos(Math.toRadians(lat_b)) *
Math.sin(lngDiff /2) * Math.sin(lngDiff /2);
double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
double distance = earthRadius * c;
int meterConversion = 1609;
return new Float(distance * meterConversion).floatValue();
}
如果您不理解,请查看此链接,此处提供相同的代码.
If you didn't understood, please check this link, same code available here.
这篇关于如何通过 latlng 获得两点之间的距离?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
织梦狗教程
本文标题为:如何通过 latlng 获得两点之间的距离?
基础教程推荐
猜你喜欢
- iOS4 创建后台定时器 2022-01-01
- 通过重定向链接在 Google Play 中打开应用 2022-01-01
- 如何从 logcat 中删除旧数据? 2022-01-01
- AdMob 广告未在模拟器中显示 2022-01-01
- Cocos2d iPhone 非矩形精灵触摸检测 2022-01-01
- navigator.geolocation.getCurrentPosition 在 Android 浏览器上 2022-01-01
- NSString intValue 不能用于检索电话号码 2022-01-01
- iPhone - 获取给定地点/时区的当前日期和时间并将其与同一地点的另一个日期/时间进行比较的正确方法 2022-01-01
- libGDX 从精灵或纹理中获取像素颜色 2022-01-01
- Android:getLastKnownLocation(LocationManager.NETWORK_PROVIDER 2022-01-01
