Skip to content Skip to sidebar Skip to footer

Get Gps Navigation Location In A Fixed Interval Of Time, Android?

Okay here it goes my app is sort of a GPS tracker app you can say. If the user is travelling from source to destination, i want to get its GPS location say after 5-7 minutes until

Solution 1:

Use requestLocationUpdates() of LocationManager class for getting GPS location in certain time interval. Look into this for your reference.

See the following code snippet which would fetch user's current location in latitude and longitude in every 1 min.

public Location getLocation() {
    Locationlocation=null;
    try {
        LocationManagerlocationManager= (LocationManager) mContext.getSystemService(LOCATION_SERVICE); 
        // Getting GPS statusbooleanisGPSEnabled= locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
        // If GPS Enabled get lat/long using GPS Servicesif (isGPSEnabled) {
            if (location == null) {
                locationManager.requestLocationUpdates( 
                    LocationManager.GPS_PROVIDER, 
                    MIN_TIME_BW_UPDATES, 
                    MIN_DISTANCE_CHANGE_FOR_UPDATES,
                    this
                );

                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;
} 

After getting the the Latitude and Longitude use Geocoder to get detailed address.

public List<Address> getCurrentAddress() {
    List<Address> addresses = newArrayList<Address>();
    Geocodergcd=newGeocoder(mContext,Locale.getDefault());
    try {
        addresses = gcd.getFromLocation(latitude, longitude, 1);
    } catch (IOException e) {
        e.printStackTrace();
    }
    return addresses;
}

EDIT:

Do all the above functionalitie in a class that extends Service so that it tracks user location eventhough the appln is closed. See below for a sample

publicfinalclassLocationTrackerextendsServiceimplementsLocationListener {
    // Your code here
}

Post a Comment for "Get Gps Navigation Location In A Fixed Interval Of Time, Android?"