3

这是我的代码。请告诉我它无法计算距离。在这段代码res中是一个长变量,它应该存储所覆盖的总距离。此代码应该在纬度和经度发生变化时根据 GPS 计算距离。

String serviceString = Context.LOCATION_SERVICE;
LocationManager locationManager;
locationManager= (LocationManager)getSystemService(serviceString);

String provider = LocationManager.GPS_PROVIDER;
final Location loc1=locationManager.getLastKnownLocation(provider);
//Location loc1=new Location("");

String netprovider=LocationManager.NETWORK_PROVIDER;

lat1=loc1.getLatitude();
lon1=loc1.getLongitude();               

LocationListener myLocationListener = new LocationListener() 
                        {
                            public void onLocationChanged(Location loc1) 
                            {

                            Location loc2=new Location("");
                            lat2=loc2.getLatitude();
                            lon2=loc2.getLongitude();
                            dtvalue.setText(lat1+","+lon1+","+lat2+","+lon2);

                            Location.distanceBetween(lat1,lon1,lat2,lon2,dist);
                            res=res+(long)dist[0];

                            lat1=lat2;
                            lon1=lon2;
                        }
                        public void onProviderDisabled(String provider)
                        {
                            // Update application if provider disabled.
                        }
                        public void onProviderEnabled(String provider)
                        {
                            // Update application if provider enabled.
                        }
                        public void onStatusChanged(String provider, int status,
                                Bundle extras)
                        {
                            // Update application if provider hardware status changed.
                        }
                    };
locationManager.requestLocationUpdates(provider, 5000, 1, myLocationListener);
locationManager.requestLocationUpdates(netprovider, 5000, 1, myLocationListener);
4

1 回答 1

3

您定义一个空位置Location loc2=new Location("");然后使用它的问题。

您可以在您的班级中定义lat1和。lon1

public void onLocationChanged(Location loc1) 
{

    if(lat1 != 0 && long1 != 0) { 
       Location.distanceBetween(lat1,lon1,loc1.getLatitude(),loc1.getLongitude(),dist);
       res+=(long)dist[0];
    }

    lat1=loc1.getLatitude();
    lon1=loc1.getLongitude();
}
于 2013-03-06T19:42:46.817 回答