gpt4 book ai didi

android - 如何计算行进距离

转载 作者:行者123 更新时间:2023-11-29 18:15:51 25 4
gpt4 key购买 nike

我正在开发一个需要计算行进距离的应用程序。我在计算距离时遇到问题。这是我的代码:

 locationmanager.requestLocationUpdates(bestProvider,0,0,new Listener());

public class Listener implements LocationListener{

@Override
public void onLocationChanged(Location location) {
if(location!=null) {
if(loc1==null) {
loc1=new Location(location);
}

speed=location.getSpeed()*3.6;
loc2=new Location(location);
dist+=loc1.distanceTo(loc2);
loc1=loc2;
breaktv.setText(""+dist/1000);

我的 Intent 是计算汽车或其他东西行驶的总距离。但是 distanceTo() 方法对我来说效果不佳。

最佳答案

从技术上讲,它应该可以工作。

我有一些建议:

  • dist 必须是 float(如果还没有的话)
  • dist += loc2.distanceTo(location),不需要创建loc1

这是我用过的代码

                if(lastLoc != null)
{
ttf = (location.getTime() - lastLoc.getTime()) / 1000;
int R = 6371;
double lat1 = Math.PI / 180.0 *lastLoc.getLatitude();
double lon1 = Math.PI / 180.0 *lastLoc.getLongitude();
double lat2 = Math.PI / 180.0 *location.getLatitude();
double lon2 = Math.PI / 180.0 *location.getLongitude();
// double dLon = Math.PI / 180.0 * (location.getLongitude() - lastLoc.getLongitude());
double dLat = (lat2-lat1);
double dLon = (lon2-lon1);
double a = Math.sin(dLat/2) * Math.sin(dLat/2) +
Math.cos(lat1) * Math.cos(lat2) *
Math.sin(dLon/2) * Math.sin(dLon/2);
double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
double d = R * c;
totalDistance += d;

}

lastLoc = location;

关于android - 如何计算行进距离,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8132198/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com