Android 土工合成岩与前一种相比有很大差距

Android 土工合成岩与前一种相比有很大差距,android,android-gps,android-fusedlocation,Android,Android Gps,Android Fusedlocation,在我的应用程序中,有跟踪一个人旅行距离的规定。有一个服务等级,用于获取当前的纬度和经纬度。有时分数可以获得,但有时不是。我从一个点开始,步行大约500米,然后返回到同一点,但计算的距离是3.5公里。 为了找到行驶的距离,我计算了当前位置和以前位置之间的差值。这些值与新值相加,并一直持续到最后。问题出现了,有时gps点与以前的距离非常远(有时获取的点与以前的距离会超过700米) 这是我的班级。如果我做错了什么,请告诉我 public class LocationUpdateService exte

在我的应用程序中,有跟踪一个人旅行距离的规定。有一个服务等级,用于获取当前的纬度和经纬度。有时分数可以获得,但有时不是。我从一个点开始,步行大约500米,然后返回到同一点,但计算的距离是3.5公里。 为了找到行驶的距离,我计算了当前位置和以前位置之间的差值。这些值与新值相加,并一直持续到最后。问题出现了,有时gps点与以前的距离非常远(有时获取的点与以前的距离会超过700米)

这是我的班级。如果我做错了什么,请告诉我

public class LocationUpdateService extends Service implements
        GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, LocationListener {
    protected static final String TAG = "LocationUpdateService";
    public static final long UPDATE_INTERVAL_IN_MILLISECONDS = 10000;
    public static final long FASTEST_UPDATE_INTERVAL_IN_MILLISECONDS = UPDATE_INTERVAL_IN_MILLISECONDS / 2;
    private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 3; // 10 meters
    public static Boolean mRequestingLocationUpdates;
    protected String mLastUpdateTime;
    protected GoogleApiClient mGoogleApiClient;
    protected LocationRequest mLocationRequest;
    protected Location mCurrentLocation;
    protected Location mLastLocation;
    Location currentLocation, previousLocation;
    public static boolean isEnded = false;
    private Context mContext;
    ServiceListener listener;
    SharedPreferences sharedPreferences;
    double latitude; // latitude
    double longitude; // longitude
    private String locationProvider;                                                                // source of fetched location
    LocationManager locationManager;
    private IBinder mBinder = new MyBinder();

    public LocationUpdateService() {
        super();
    }

    public void setListener(ServiceListener listener) {
        this.listener = listener;

    }

    @Override
    public void onCreate() {
        super.onCreate();
        // Kick off the process of building a GoogleApiClient and requesting the LocationServices
        mContext = getApplicationContext();
        //locationManager = (LocationManager) mContext.getSystemService(Context.LOCATION_SERVICE);
        sharedPreferences = mContext.getSharedPreferences(WebServiceHelper.PREFS_NAME, 0);
        System.out.println("GPS TRACKER STARTED");
        deletefile("asap_distance.csv");
    }

//    @Override
//    public IBinder onBind(Intent intent) {
//        return null;
//    }


    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        // Within {@code onPause()}, we pause location updates, but leave the
        // connection to GoogleApiClient intact.  Here, we resume receiving
        // location updates if the user has requested them.
        System.out.println(TAG + "onStartCommand");

        Log.d("LOC", "Service init...");
        isEnded = false;
        mRequestingLocationUpdates = false;
        mLastUpdateTime = "";
        buildGoogleApiClient();
        if (mGoogleApiClient.isConnected() && mRequestingLocationUpdates) {
            startLocationUpdates();
        }
        return Service.START_REDELIVER_INTENT;
    }


    @Override
    public void onConnected(Bundle bundle) {
        System.out.println(TAG + "onConnected");
        startLocationUpdates();
    }

    @Override
    public void onConnectionSuspended(int i) {
        // The connection to Google Play services was lost for some reason. We call connect() to
        // attempt to re-establish the connection.
        Log.i(TAG, "Connection suspended==");
        mGoogleApiClient.connect();
    }

    @Override
    public void onLocationChanged(Location location) {
        System.out.println(TAG + "onLocationChanged");
        if (location == null) {
            getLastKnownLocation();
        } else {
            mCurrentLocation = location;
            if (mCurrentLocation.hasAccuracy() && mCurrentLocation.getAccuracy() > MIN_DISTANCE_CHANGE_FOR_UPDATES)
                setLocationData(mCurrentLocation);
            mLastUpdateTime = DateFormat.getTimeInstance().format(new Date());

        }

//
//        updateUI();
//        Toast.makeText(this, "Location changed",
//                Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onConnectionFailed(ConnectionResult connectionResult) {
        // Refer to the javadoc for ConnectionResult to see what error codes might be returned in
        // onConnectionFailed.
        Log.i(TAG, "Connection failed: ConnectionResult.getErrorCode() = " + connectionResult.getErrorCode());
    }

    /**
     * Builds a GoogleApiClient. Uses the {@code #addApi} method to request the
     * LocationServices API.
     */
    protected synchronized void buildGoogleApiClient() {
        Log.i(TAG, "Building GoogleApiClient===");
        mGoogleApiClient = new GoogleApiClient.Builder(this)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .addApi(LocationServices.API)
                .build();

        createLocationRequest();
    }

    public void setLocationData(Location location) {

        if (location != null) {
            currentLocation = location;
            double previous_latitude, previous_longitude, current_lat, current_long;

            latitude = location.getLatitude();
            longitude = location.getLongitude();
            //  Toast.makeText(mContext, "Current lattitude:" + latitude + "Current Longitude:" + longitude, Toast.LENGTH_LONG).show();

            previous_latitude = previousLocation.getLatitude();
            previous_longitude = previousLocation.getLongitude();
            current_lat = currentLocation.getLatitude();
            current_long = currentLocation.getLongitude();
            Log.d(TAG, "Previous lattitude:" + previous_latitude + "Previous Longitude:" + previous_longitude);
            Log.d(TAG, "Current lattitude:" + current_lat + "Current Longitude:" + current_long);

            sharedPreferences.edit().putFloat("lastSavedLat", (float) current_lat).apply();
            sharedPreferences.edit().putFloat("lastSavedLon", (float) current_long).apply();

            if (previousLocation != null && sharedPreferences.contains("sdeMarkedStartLocLat")) {

                if (current_lat == previous_latitude && current_long == previous_longitude) {
                    Log.d(TAG, "No  Displacement");

                } else {
                    Log.d(TAG, "Device Displaced");
                    //  double d = getDistance(previous_latitude, previous_longitude, current_lat, current_long);
                    double d = ((int) (previousLocation.distanceTo(currentLocation) * 1000)) / 1000;
                    Log.d(TAG, "Distance calculated in between previousLocation and currentLocation is" + d);
                    if (d < 5000) {
                        d = d + sharedPreferences.getFloat("sum_dist", 0);
                        sharedPreferences.edit().putFloat("sum_dist", (float) d).apply();
                        //           Toast.makeText(mContext, "Total Distance travelled is " + d + "km", Toast.LENGTH_LONG).show();

                        // Log.d("Distance Calculator ON", "distance" + d);
                        SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
                        writeToFile(sdf.format(new Date()) + "," + currentLocation.getLatitude() + "," + currentLocation.getLongitude() + ", " + d + " meters" + "\n", "asap.csv");
                        writeToFile(currentLocation.getLatitude() + "," + currentLocation.getLongitude() + "\n", "asap_distance.csv");

                    }


                }

            }

            if (listener != null)
                listener.onLocationReceived(location);
            System.out.println("GPS TRACKER " + latitude + " " + longitude);
            previousLocation = currentLocation;
        } else {
            Log.d(TAG, "Location is null");
            return;
        }

    }

    public double getDistance(double start_lat, double start_long, double dest_lat, double dest_long) {

        double dist = 0;
        Location locationA = new Location("point A");

        locationA.setLatitude(start_lat);
        locationA.setLongitude(start_long);

        Location locationB = new Location("point B");

        locationB.setLatitude(dest_lat);
        locationB.setLongitude(dest_long);

        dist = locationA.distanceTo(locationB);


        if (dist != 0) {
            dist = dist / 1000;
        }
        return dist;
    }


    /**
     * Updates the latitude, the longitude, and the last location time in the UI.
     */
    private void updateUI() {
        Toast.makeText(this, "Latitude: =" + mCurrentLocation.getLatitude() + " Longitude:=" + mCurrentLocation
                .getLongitude(), Toast.LENGTH_SHORT).show();
        System.out.println("updateUI");
        Log.d(TAG, "Latitude:==" + mCurrentLocation.getLatitude() + "\n Longitude:==" + mCurrentLocation.getLongitude
                ());

    }


    /**
     * Sets up the location request. Android has two location request settings:
     * {@code ACCESS_COARSE_LOCATION} and {@code ACCESS_FINE_LOCATION}. These settings control
     * the accuracy of the current location. This sample uses ACCESS_FINE_LOCATION, as defined in
     * the AndroidManifest.xml.
     * <p/>
     * When the ACCESS_FINE_LOCATION setting is specified, combined with a fast update
     * interval (5 seconds), the Fused Location Provider API returns location updates that are
     * accurate to within a few feet.
     * <p/>
     * These settings are appropriate for mapping applications that show real-time location
     * updates.
     */
    protected void createLocationRequest() {
        mGoogleApiClient.connect();
        mLocationRequest = new LocationRequest();

        // Sets the desired interval for active location updates. This interval is
        // inexact. You may not receive updates at all if no location sources are available, or
        // you may receive them slower than requested. You may also receive updates faster than
        // requested if other applications are requesting location at a faster interval.
        mLocationRequest.setInterval(UPDATE_INTERVAL_IN_MILLISECONDS);

        // Sets the fastest rate for active location updates. This interval is exact, and your
        // application will never receive updates faster than this value.
        mLocationRequest.setFastestInterval(FASTEST_UPDATE_INTERVAL_IN_MILLISECONDS);
        mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
        mLocationRequest.setSmallestDisplacement(MIN_DISTANCE_CHANGE_FOR_UPDATES);
    }

    /**
     * Requests location updates from the FusedLocationApi.
     */
    protected void startLocationUpdates() {
        if (!mRequestingLocationUpdates) {
            mRequestingLocationUpdates = true;

            // The final argument to {@code requestLocationUpdates()} is a LocationListener
            // (http://developer.android.com/reference/com/google/android/gms/location/LocationListener.html).
            LocationServices.FusedLocationApi.requestLocationUpdates(
                    mGoogleApiClient, mLocationRequest, this);
            Log.i(TAG, " startLocationUpdates===");
            isEnded = true;
        }
        mLastLocation = LocationServices.FusedLocationApi.getLastLocation(
                mGoogleApiClient);
        previousLocation = mLastLocation;

        if (mLastLocation != null) {
            onLocationChanged(mLastLocation);
        }
    }

    /**
     * Function to get latitude
     */
    public double getLatitude() {
        if (mCurrentLocation != null) {
            latitude = mCurrentLocation.getLatitude();
        }

        // return latitude
        return latitude;
    }


    /**
     * Function to get longitude
     */
    public double getLongitude() {
        if (mCurrentLocation != null) {
            longitude = mCurrentLocation.getLongitude();
        }

        // return longitude
        return longitude;
    }


    /**
     * Removes location updates from the FusedLocationApi.
     */
    protected void stopLocationUpdates() {
        if (mRequestingLocationUpdates) {
            mRequestingLocationUpdates = false;
            // It is a good practice to remove location requests when the activity is in a paused or
            // stopped state. Doing so helps battery performance and is especially
            // recommended in applications that request frequent location updates.

            Log.d(TAG, "stopLocationUpdates();==");
            // The final argument to {@code requestLocationUpdates()} is a LocationListener
            // (http://developer.android.com/reference/com/google/android/gms/location/LocationListener.html).
            LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        stopLocationUpdates();
        System.out.println("GPS TRACKER STOPPED");

    }

    public Location getLastKnownLocation() {
        //  locationProvider = LocationManager.GPS_PROVIDER;
        Location lastKnownLocation = null;
        // Or use LocationManager.GPS_PROVIDER
        if (Build.VERSION.SDK_INT >= 23 &&
                ContextCompat.checkSelfPermission(mContext, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED &&
                ContextCompat.checkSelfPermission(mContext, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            return lastKnownLocation;
        }
        try {
            //lastKnownLocation = locationManager.getLastKnownLocation(locationProvider);
            lastKnownLocation = LocationServices.FusedLocationApi.getLastLocation(
                    mGoogleApiClient);
            return lastKnownLocation;
        } catch (Exception e) {
            Log.e(TAG, e.getMessage());
        }
        return lastKnownLocation;
    }

    @Override
    public IBinder onBind(Intent intent) {
        Log.v("GPS", "in onBind");
        return mBinder;

    }

    @Override
    public void onRebind(Intent intent) {
        Log.v("GPS", "in onRebind");
        super.onRebind(intent);
    }

    @Override
    public boolean onUnbind(Intent intent) {
        Log.v("GPS", "in onUnbind");
        return true;
    }

    public void deletefile(String filename) {
        File sdCard = Environment.getExternalStorageDirectory();
        File directory = new File(sdCard.getAbsolutePath() + "/ASAPLog");
        //Now delete the file in the above directory and write the contents into it
        try {
            File file = new File(directory.getAbsolutePath() + "/" + filename);
            if (file.exists()) {
                boolean deleted = file.delete();
                System.out.println("deleted" + deleted);

            } else {
                System.out.println("File Doesnot Exists");

            }
        } catch (Exception e) {
            e.printStackTrace();

        }

    }

    public void writeToFile(String text, String filename) {


        File sdCard = Environment.getExternalStorageDirectory();
        File directory = new File(sdCard.getAbsolutePath() + "/ASAPLog");
        if (!directory.exists())
            directory.mkdirs();
        // String savedText = readFromFile(filename).toString();

        //Now create the file in the above directory and write the contents into it
        try {
            File file = new File(directory.getAbsolutePath() + "/" + filename);
            FileOutputStream fOut = new FileOutputStream(file, true);
            OutputStreamWriter osw = new OutputStreamWriter(fOut);
            osw.append(text);
            osw.flush();
            osw.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public class MyBinder extends Binder {
        LocationUpdateService getService() {
            return LocationUpdateService.this;
        }
    }

}
公共类LocationUpdateService扩展服务实现
GoogleAppClient.ConnectionCallbacks,GoogleAppClient.OnConnectionFailedListener,LocationListener{
受保护的静态最终字符串标记=“LocationUpdateService”;
公共静态最终长更新间隔(以毫秒为单位)=10000;
public static final long faster_UPDATE_INTERVAL_IN_毫秒=UPDATE_INTERVAL_IN_毫秒/2;
私有静态最终长距离最小距离更改用于更新=3;//10米
公共静态布尔MRequestingLocationUpdate;
受保护字符串mLastUpdateTime;
受保护的GoogleapClient MGoogleapClient;
受保护的位置请求mLocationRequest;
保护位置mCurrentLocation;
受保护位置;
位置currentLocation、previousLocation;
公共静态布尔值isEnded=false;
私有上下文;
服务监听器;
SharedReferences SharedReferences;
双纬度;//纬度
双经度;//经度
私有字符串locationProvider;//获取位置的源
地点经理地点经理;
private IBinder mBinder=new MyBinder();
公共位置更新服务(){
超级();
}
公共void setListener(ServiceListener listener){
this.listener=listener;
}
@凌驾
public void onCreate(){
super.onCreate();
//启动构建GoogleAppClient并请求LocationServices的过程
mContext=getApplicationContext();
//locationManager=(locationManager)mContext.getSystemService(Context.LOCATION\u服务);
SharedReferences=mContext.getSharedReferences(WebServiceHelper.PREFS\u名称,0);
System.out.println(“GPS跟踪器启动”);
删除文件(“asap_distance.csv”);
}
//@覆盖
//公共IBinder onBind(意向){
//返回null;
//    }
@凌驾
公共int onStartCommand(Intent Intent、int标志、int startId){
//在{@code onPause()}中,我们暂停位置更新,但保留
//与GoogleAppClient的连接完好无损。在这里,我们继续接收
//如果用户请求位置更新,则位置更新。
System.out.println(标记+“onStartCommand”);
Log.d(“LOC”,“服务初始化…”);
isEnded=假;
mRequestingLocationUpdates=false;
mLastUpdateTime=“”;
buildGoogleAppClient();
if(mgoogleapClient.isConnected()&&mRequestingLocationUpdates){
startLocationUpdates();
}
返回服务。启动\u重新交付\u意图;
}
@凌驾
未连接的公共空间(捆绑包){
System.out.println(标签+“未连接”);
startLocationUpdates();
}
@凌驾
公共空间连接暂停(int i){
//由于某种原因,与Google Play服务的连接丢失。我们调用connect()以
//尝试重新建立连接。
Log.i(标记“连接挂起==”);
mGoogleApiClient.connect();
}
@凌驾
已更改位置上的公共无效(位置){
System.out.println(标记+“onLocationChanged”);
if(位置==null){
getLastKnownLocation();
}否则{
mCurrentLocation=位置;
if(mCurrentLocation.hasAccuracy()&&mCurrentLocation.getAccuracy()>MIN\u DISTANCE\u CHANGE\u用于更新)
setLocationData(mCurrentLocation);
mLastUpdateTime=DateFormat.getTimeInstance().format(新日期());
}
//
//updateUI();
//Toast.makeText(此“位置已更改”,
//吐司。长度(短)。show();
}
@凌驾
公共无效onConnectionFailed(ConnectionResult ConnectionResult){
//请参阅javadoc for ConnectionResult,以查看在中可能返回的错误代码
//OnConnection失败。
Log.i(标记,“连接失败:ConnectionResult.getErrorCode()=”+ConnectionResult.getErrorCode());
}
/**
*构建GoogleAppClient。使用{@code#addApi}方法请求
*LocationServices API。
*/
受保护的同步无效BuildGoogleAppClient(){
Log.i(标记“Building GoogleAppClient==”);
mgoogleapclient=新的Googleapclient.Builder(此)
.addConnectionCallbacks(此)
.addOnConnectionFailedListener(此)
.addApi(LocationServices.API)
.build();
createLocationRequest();
}
公共void setLocationData(位置){
如果(位置!=null){
当前位置=位置;
双倍前纬度、前经度、当前纬度、当前长度;
纬度=位置。getLatitude();
longitude=location.getLongitude();
//Toast.makeText(mContext,“当前晶格:”+纬度+“当前经度:”+经度,Toast.LENGTH_LONG).show();
previous_latitude=previousLocation.getLatitude();
previous_longitude=previousLocation.getLongitude();
current_lat=currentLocation.getLat