在eclipse中创建android跟踪应用程序

时间:2016-09-25 04:21:57

标签: android eclipse gps tracking

hei我正在为我的最终测试创建一个Android汽车跟踪应用程序但是当我测试我的跟踪应用程序时,它继续向我的数据库发送相同的坐标是我的gpstracking代码

    package com.example.id.library;

    import android.app.AlertDialog;
    import android.app.Service;
    import android.content.Context;
    import android.content.DialogInterface;
    import android.content.Intent;
    import android.location.Location;
    import android.location.LocationListener;
    import android.location.LocationManager; 
    import android.os.Bundle;
    import android.os.IBinder;
    import android.provider.Settings;
    import android.util.Log;

    public class gps extends Service implements LocationListener {

    private final Context mContext;

    // flag buat status gps. on atau nda
    boolean isGPSEnabled = false;

    // flag buat network.
    boolean isNetworkEnabled = false;

    // flag buat posisi gps. bisa dapat koordinat atau nda
    boolean canGetLocation = false;

    Location location; // variabel untuk lokasi
    double latitude; // latitudenya
    double longitude; // longitudenya

    // minimum perbedaan jarak untuk update
    private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10; // 10 meter

    // minimum selang waktu tiap update lokasi
    private static final long MIN_TIME_BW_UPDATES = 1000 * 60 * 1; // 1 menit

    // deklarasi Location Manager
    protected LocationManager locationManager;

    public gps(Context context) {
        this.mContext = context;
        getLocation();
    }

    public Location getLocation() {
        try {
            locationManager = (LocationManager) mContext
                    .getSystemService(LOCATION_SERVICE);

            // tarik status gps
            isGPSEnabled = locationManager
                    .isProviderEnabled(LocationManager.GPS_PROVIDER);

            // tarik status network
            isNetworkEnabled = locationManager
                    .isProviderEnabled(LocationManager.NETWORK_PROVIDER);

            if (!isGPSEnabled && !isNetworkEnabled) {
                // kalo semua GPS dan Network mati
                // buka alert untuk enable gps
            } else { // kalo GPS atau network bisa dapat lokasi
                this.canGetLocation = true;
                // ambil lokasi dari network dulu
                if (isNetworkEnabled) {
                    locationManager.requestLocationUpdates(
                            LocationManager.NETWORK_PROVIDER,
                            MIN_TIME_BW_UPDATES,
                            MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                    Log.d("Network", "Network");
                    if (locationManager != null) {
                        location = locationManager
                                .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
                        if (location != null) {
                            latitude = location.getLatitude();
                            longitude = location.getLongitude();
                        }
                    }
                }
                // kalo GPS ON, ambil koordinat yang lebih presisi
                if (isGPSEnabled) {
                    if (location == null) {
                        locationManager.requestLocationUpdates(
                                LocationManager.GPS_PROVIDER,
                                MIN_TIME_BW_UPDATES,
                                MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                        Log.d("GPS Enabled", "GPS Enabled");
                        if (locationManager != null) {
                            location = locationManager
                                    .getLastKnownLocation(LocationManager.GPS_PROVIDER);
                            if (location != null) {
                                latitude = location.getLatitude();
                                longitude = location.getLongitude();
                            }
                        }
                    }
                }
            }

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

        return location;
    }

    // fungsi untuk stop listener GPS
    public void stopUsingGPS() {
        if (locationManager != null) {
            locationManager.removeUpdates(gps.this);
        }
    }

    // fungsi untuk ambil latitude
    public double getLatitude() {
        if (location != null) {
            latitude = location.getLatitude();
        }

        // return nilai latitude
        return latitude;
    }

    // fungsi untuk ambil longitude
    public double getLongitude() {
        if (location != null) {
            longitude = location.getLongitude();
        }

        // return nilai longitude
        return longitude;
    }

    // fungsi untuk cek gps bisa ambil koordinat atau nda
    public boolean canGetLocation() {
        return this.canGetLocation;
    }

    // fungsi untuk alert dialog kalau GPS mati
    public void showSettingsAlert() {
        AlertDialog.Builder alertDialog = new AlertDialog.Builder(mContext);

        // title
        alertDialog.setTitle("Loading GPS");

        // Dialog
        alertDialog
                .setMessage("GPS dalam kondisi mati. Silakan nyalakan GPS terlebih dahulu.");

        // tombol setting
        alertDialog.setPositiveButton("Settings",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        Intent intent = new Intent(
                                Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                        mContext.startActivity(intent);
                    }
                });

        // tombol cancel
        alertDialog.setNegativeButton("Cancel",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.cancel();
                    }
                });

        // tampilkan alert
        alertDialog.show();
    }

    @Override
    public void onLocationChanged(Location location) {
    }

    @Override
    public void onProviderDisabled(String provider) {
    }

    @Override
    public void onProviderEnabled(String provider) {
    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
    }

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

}

这是我的gpsracking发送到我的数据库的我的坐标

like u see my apps keep sending the same cordinates and i swear i have already walk for more than 1 kilometers

请别人帮助我,抱歉我的英语不好

enter image description here

1 个答案:

答案 0 :(得分:0)

我认为问题是您的location字段没有更新,或者只是您正在从wifi网络检索坐标,而您的配置(10米和1分钟)不足以获得新位置。

试一试:

@Override
public void onLocationChanged(android.location.Location location) { 
    if (this.location != null) {
        setLocation(this.location, location);
    }
}

@Override
public void onProviderDisabled(String provider) {
    stopUsingGPS();
    getLocation();
}

@Override
public void onProviderEnabled(String provider) {
    stopUsingGPS();
    getLocation();
}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
    stopUsingGPS();
    getLocation();
}

private void setLocation(android.location.Location location1, android.location.Location location2) {
    if (isBetterLocation(location1, location2))
        location = location1;
    else
        location = location2;
}

/**
 * Determines whether one Location reading is better than the current
 * Location fix
 * 
 * @param location1
 *            The new Location that you want to evaluate
 * @param location2
 *            The current Location fix, to which you want to compare the new
 *            one
 */
protected boolean isBetterLocation(android.location.Location location1, android.location.Location location2) {

    if (location1 != null && location2 == null) {
        return true;
    } else if (location1 == null && location2 != null) {
        return false;
    } else if (location1 == null && location2 == null) {
        return true;
    }

    // Check whether the new location fix is newer or older
    long timeDelta = location1.getTime() - location2.getTime();
    boolean isSignificantlyNewer = timeDelta > TWO_MINUTES;
    boolean isSignificantlyOlder = timeDelta < -TWO_MINUTES;
    boolean isNewer = timeDelta > 0;

    // If it's been more than two minutes since the current location,
    // use the new location because the user has likely moved
    if (isSignificantlyNewer) {
        return true;
        // If the new location is more than two minutes older, it must
        // be worse
    } else if (isSignificantlyOlder) {
        return false;
    }

    // Check whether the new location fix is more or less accurate
    int accuracyDelta = (int) (location1.getAccuracy() - location2.getAccuracy());
    boolean isLessAccurate = accuracyDelta > 0;
    boolean isMoreAccurate = accuracyDelta < 0;
    boolean isSignificantlyLessAccurate = accuracyDelta > 200;

    // Check if the old and new location are from the same provider
    boolean isFromSameProvider = isSameProvider(location1.getProvider(), location2.getProvider());

    // Determine location quality using a combination of timeliness and
    // accuracy
    if (isMoreAccurate) {
        return true;
    } else if (isNewer && !isLessAccurate) {
        return true;
    } else if (isNewer && !isSignificantlyLessAccurate && isFromSameProvider) {
        return true;
    }
    return false;
}

/**
 * Checks whether two providers are the same
 * */
private boolean isSameProvider(String provider1, String provider2) {
    if (provider1 == null)
        return provider2 == null;

    return provider1.equals(provider2);
}

如果您对RxJava有信心并且它适合您的项目,您可以查看RxGpsService(Android服务以检索GPS位置并使用RxJava路由统计数据)