首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >在android中计算两个经纬点之间的距离

在android中计算两个经纬点之间的距离
EN

Stack Overflow用户
提问于 2014-03-22 19:35:55
回答 2查看 57.5K关注 0票数 24

如何找到two latitudelongitude点之间的距离

对于其中一个纬度和经度点,我将其保存在数据库中&另一个我想使用手机的当前位置

我应该在下面的哪一个中使用它们:

安卓位置计算provider

  • Google API

  • Mathematical

注意:::任何演示代码示例都会很有用

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-03-22 19:42:18

您可以使用内置算法:

代码语言:javascript
复制
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) ;

更新:(我没有任何错误)

代码语言:javascript
复制
LatLng latLngA = new LatLng(12.3456789,98.7654321);
LatLng latLngB = new LatLng(98.7654321,12.3456789);

Location locationA = new Location("point A");
locationA.setLatitude(latLngA.latitude);
locationA.setLongitude(latLngA.longitude);
Location locationB = new Location("point B");
locationB.setLatitude(latLngB.latitude);
locationB.setLongitude(latLngB.longitude);

double distance = locationA.distanceTo(locationB);
票数 53
EN

Stack Overflow用户

发布于 2014-03-22 19:39:41

您可以使用Haversine算法。另一个用户已经为此编写了一个解决方案:

代码语言:javascript
复制
public double CalculationByDistance(double initialLat, double initialLong,
                            double finalLat, double finalLong){
int R = 6371; // km
double dLat = toRadians(finalLat-initialLat);
double dLon = toRadians(finalLong-initialLong);
lat1 = toRadians(lat1);
lat2 = toRadians(lat2);

double a = Math.sin(dLat/2) * Math.sin(dLat/2) +
        Math.sin(dLon/2) * Math.sin(dLon/2) * Math.cos(lat1) * Math.cos(lat2); 
double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a)); 
return R * c;
}

public double toRadians(deg) {
  return deg * (Math.PI/180)
}

来源:https://stackoverflow.com/a/17787472/3449528

票数 14
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22577075

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档