Calculate distance using latitude and longitude using gps data . - android

I am trying to calculate distance using gps lat and long data.
but i can't get accurate result .when i am given a static data ,then distance formula work perfect ,
public class NWDService extends Service implements LocationListener {
private LocationManager myLocationManager;
private LocationProvider myLocationProvider;
NotificationManager myNotificationManager;
private long frequency;
private Handler handler;
private double total_distance = 0;
private Location currentLocation;
public void onLocationChanged(Location newLocation) {
try {
System.out.println("latitude current :"+currentLocation.getLatitude());
System.out.println("latitude current :"+currentLocation.getLongitude());
System.out.println("latitude new :"+newLocation.getLatitude());
System.out.println("latitude new :"+newLocation.getLongitude());
System.out.println("distance total :"+total_distance);
//System.out.println(distance(22.306813, 73.180239,22.301016, 73.177986, 'K') + " Kilometers\n");
double diff = 0.0;
diff = currentLocation.getLatitude()- newLocation.getLatitude();
System.out.println("difference ::"+diff);
if(diff != 0){
total_distance = total_distance + distance(currentLocation.getLatitude(), currentLocation.getLongitude(), newLocation.getLatitude(), newLocation.getLatitude(), 'K');
//total_distance = distance(22.307309,73.181098,23.030000,72.580000,'K');
handler.post(new Runnable() {
#Override
public void run() {
Toast.makeText(getApplicationContext(),"Total Distance:"+total_distance, Toast.LENGTH_LONG).show();
}
});
}
currentLocation = newLocation;
} catch (Exception e) {
currentLocation = newLocation;
e.printStackTrace();
}
}
private double distance(double lat1, double lon1, double lat2, double lon2, char unit) {
double theta = lon1 - lon2;
double dist = Math.sin(deg2rad(lat1)) * Math.sin(deg2rad(lat2)) + Math.cos(deg2rad(lat1)) * Math.cos(deg2rad(lat2)) * Math.cos(deg2rad(theta));
dist = Math.acos(dist);
dist = rad2deg(dist);
dist = dist * 60 * 1.1515;
if (unit == 'K') {
dist = dist * 1.609344;
} else if (unit == 'N') {
dist = dist * 0.8684;
}
return (dist);
}
/*:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/
/*:: This function converts decimal degrees to radians :*/
/*:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/
private double deg2rad(double deg) {
return (deg * Math.PI / 180.0);
}
/*:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/
/*:: This function converts radians to decimal degrees :*/
/*:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/
private double rad2deg(double rad) {
return (rad * 180.0 / Math.PI);
}
private void myNotify(String text) {
Notification notif = new Notification(R.drawable.ic_launcher, text, System
.currentTimeMillis());
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
new Intent(this, Home.class), 0);
notif.setLatestEventInfo(this, "NotWhileDriving", text, contentIntent);
// notif.defaults = Notification.DEFAULT_VIBRATE;
myNotificationManager.notify((int) System.currentTimeMillis(), notif);
}
#Override
public void onCreate() {
super.onCreate();
handler = new Handler();
android.util.Log.d("NWD", "creating");
myLocationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
System.out.println("location manager:"+myLocationManager.getAllProviders());
myLocationProvider = myLocationManager.getProvider("gps");
myNotificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
updatePreferences();
}
public void updatePreferences() {
// sync local variables with preferences
android.util.Log.d("NWD", "updating preferences");
frequency = 10;
// update the LM with the new frequency
myLocationManager.removeUpdates(this);
myLocationManager.requestLocationUpdates(myLocationProvider.getName(),frequency, 0, this);
}
#Override
public void onDestroy() {
super.onDestroy();
android.util.Log.d("NWD", "destroying");
myLocationManager.removeUpdates(this);
myNotify("stopping");
}
#SuppressWarnings("deprecation")
#Override
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
android.util.Log.d("NWD", "starting");
currentLocation = myLocationManager
.getLastKnownLocation(myLocationProvider.getName());
myNotify("starting");
}
public void onProviderDisabled(String arg0) {
}
public void onProviderEnabled(String arg0) {
}
public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
}
#Override
public IBinder onBind(Intent arg0) {
return null; // this is for heavy IPC, not used
}
}
Please help me what is the problem in my code...

Just use Location.distanceTo(Location) it will give you a really distance between two different Locations.

First, I don't believe this code works on different parts of the earth. I don't see the WGS 84 parameters, only some magic numbers in
dist = dist * 60 * 1.1515;
if (unit == 'K') {
dist = dist * 1.609344;
} else if (unit == 'N') {
dist = dist * 0.8684;
}
Where did you get that from? Do you understand it?
Second, if your data is noisy, you're adding up this noise by summing totalDistance. You might want to check if the distance is above some threshold and only add totalDistance if the distance threshold is exceeded.

You can use Google Distance matrix API for find accurate result of distance between two place.
for detail study you can refer from:
https://developers.google.com/maps/documentation/distancematrix/

Related

update location to the firebase database every 15 minutes only if the location has been changed? Android

I have implemented location upload to the firebase database every 15 minute using Alarm Manager, But I need to stop next update if user is still on same place where he was, (next update), It is ok to use fused location API or Location manager in android. This should work when app is killed or Android system is deep seeping mode (Locked). So we need background location permission, that is also fine! But it should not turn on location service all the time because it will drain battery. So location service should be stopped until Alarm manager start for the next time.
This is the TimerService I used!
public class TimerService extends BroadcastReceiver {
public static final int REQUEST_CODE = 12346;
// Triggered by the Alarm periodically (starts the service to run task)
#Override
public void onReceive(Context context, Intent intent) {
Intent serviceIntent = new Intent(context, TrackingService.class);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
ContextCompat.startForegroundService(context, serviceIntent);
} else {
context.startService(serviceIntent);
}
}
}
This is method in TrackingService
private void requestLocationUpdates() {
LocationRequest request = new LocationRequest();
request.setInterval(10000);
request.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
client = LocationServices.getFusedLocationProviderClient(this);
int permission = ContextCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_FINE_LOCATION);
if (permission == PackageManager.PERMISSION_GRANTED) {
locationCallback = new LocationCallback() {
#Override
public void onLocationResult(LocationResult locationResult) {
Location location = locationResult.getLastLocation();
if (location != null) {
updateFirebase(location);
}
}
};
client.requestLocationUpdates(request,locationCallback,null);
}else{
stopForeground(true);
stopSelf();
}
}
So I need to help to implement a new method something like this,
tryLocationUpdate(Location location){
if(location==stillInSamePlace){
//Do not upload to the firebase database
}else{
uploadLocation();
}
}
Can anyone help me?
I found a solution for this. You can save a longitude and latitude in Shared Preferences and check whether user has gone more than 100m. If it is true you can update the server and you can update Shared Preferences also.
SharedPreferences prefs = getSharedPreferences("code", MODE_PRIVATE);
count = prefs.getInt("location_history",0);
storedLat = prefs.getFloat("storedLat", 0);
storedLon = prefs.getFloat("storedLon", 0);
storedAccu = prefs.getFloat("storedAccu", 0);
float distance = distanceBetweenEarthCoordinates(storedLat, storedLon, (float) location.getLatitude(), (float) location.getLongitude());
if (distance > 100) {
Log.d("TAG", "onLocationResult: New Point" + count + "Distance: " + distance);
storedLat = (float) location.getLatitude();
storedLon = (float) location.getLongitude();
SharedPreferences.Editor editor = getSharedPreferences("uinfo", MODE_PRIVATE).edit();
editor.putFloat("storedLat", storedLat);
editor.putFloat("storedLon", storedLon);
count++;
editor.putInt("location_history", count);
editor.apply();
}
distanceBetweenEarthCoordinates method
public static float degreesToRadians(float degrees) {
return (float) (degrees * Math.PI / 180);
}
public static float distanceBetweenEarthCoordinates(float lat1, float lon1, float lat2, float lon2) {
float earthRadiusKm = 6371;
float dLat = degreesToRadians(lat2 - lat1);
float dLon = degreesToRadians(lon2 - lon1);
lat1 = degreesToRadians(lat1);
lat2 = degreesToRadians(lat2);
float a = (float) (Math.sin(dLat / 2) * Math.sin(dLat / 2) +
Math.sin(dLon / 2) * Math.sin(dLon / 2) * Math.cos(lat1) * Math.cos(lat2));
float c = (float) (2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a)));
return earthRadiusKm * c * 1000;
}

Calculate Distance traveled android using location manager [duplicate]

This question already has answers here:
Android Calculate Distance Traveled
(2 answers)
Closed 6 years ago.
I am currently working on a simple fitness app that allows user to track his/her performance (running,walking). I have been using location manager to get the moving speed which works very fine. However I need to get the distance traveled, how can use location manager (long and lat) to get the distance ?
Thanks
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_track);
start = (Button) findViewById(R.id.btnStart);
speed = (TextView) findViewById(R.id.txtSpeed);
locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
//initialize location listener
locationListener = new LocationListener() {
#Override
public void onLocationChanged(Location location) {
getSpeed(location);
double lat2 = location.getLatitude();
double lng2 = location.getLongitude();
}
#Override
public void onStatusChanged(String s, int i, Bundle bundle) {
}
#Override
public void onProviderEnabled(String s) {
}
#Override
public void onProviderDisabled(String s) {
}
//get the speed from the given location updates
public void getSpeed(Location location) {
currentSpeed = (location.getSpeed() * 3600 / 1000);
String convertedSpeed = String.format("%.2f", currentSpeed);
speed.setText(convertedSpeed + "Km/h");
}
};
You can possibly find distance between each successive latitude and longitude and keep adding.
Here getting distance in kilometers (km)
private double distance(double lat1, double lon1, double lat2, double lon2) {
double theta = lon1 - lon2;
double dist = Math.sin(deg2rad(lat1))
* Math.sin(deg2rad(lat2))
+ Math.cos(deg2rad(lat1))
* Math.cos(deg2rad(lat2))
* Math.cos(deg2rad(theta));
dist = Math.acos(dist);
dist = rad2deg(dist);
dist = dist * 60 * 1.1515;
return (dist);
}
private double deg2rad(double deg) {
return (deg * Math.PI / 180.0);
}
private double rad2deg(double rad) {
return (rad * 180.0 / Math.PI);
}
Hope this helps you.

How to find distance between two locations (By latitude and longitude) in kilometers

I am developing an android app which will track the current location latitude and longitude and store in external database.Here i am having a list of latitudes and longitudes.I populated them by using the custom adapter . But,Here i need the distance from one base latitude and base longitude to remaining items latitude and longitude.Here the base latitude and longitude was selected by the user it self.Here the below the list explains like this i have
SELECTION LAT LONG DISTANCE
-------------------------------------------------
checkbox1 123.4546 456.48751 Text
checkbox2 123.4546 456.48751 Text
checkbox3 123.4546 456.48751 Text
checkbox4 123.4546 456.48751 Text
If user selects the check-box 1 then i have to find the distance from check-box 1 lat long to check-box 2,check-box 3,check-box-4 lat long in KILOMETERS and display in their respected position .
This is some code from adapter i had written but it was not showing any results.
public class Locations_Adapter extends BaseAdapter {
public String distance_string;
Context context;
List<Locations_modle> objects;
double distance, latitude, longitude;
String latitude_string, longitude_string;
double baseLat, baseLong, finalLat, finalLong;
Location location_pointa, location_pointb;
TextView distance_text;
float[] results;
int selectedPostion = -1;
public Locations_Adapter(Context context, int resource, List<Locations_modle> objects) {
this.context = context;
this.objects = objects;
}
/**
* Distance calculation between two lat longs
**/
private static double calculateDistance(double baseLat, double baseLong, double latitude, double longitude, String unit) {
double theta = baseLong - longitude;
double dist = Math.sin(deg2rad(baseLat)) * Math.sin(deg2rad(latitude)) + Math.cos(deg2rad(baseLat)) * Math.cos(deg2rad(longitude)) * Math.cos(deg2rad(theta));
dist = Math.acos(dist);
dist = rad2deg(dist);
dist = dist * 60 * 1.1515;
if (unit == "K") {
dist = dist * 1.609344;
} else if (unit == "N") {
dist = dist * 0.8684;
}
return (dist);
}
/*:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/
/*:: This function converts decimal degrees to radians :*/
/*:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/
private static double deg2rad(double deg) {
return (deg * Math.PI / 180.0);
}
/*:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/
/*:: This function converts radians to decimal degrees :*/
/*:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/
private static double rad2deg(double rad) {
return (rad * 180 / Math.PI);
}
#Override
public int getCount() {
return objects.size();
}
#Override
public Object getItem(int position) {
return objects.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(final int position, View convertView, final ViewGroup parent) {
final View locations_row = LayoutInflater.from(context).inflate(R.layout.layout_adapter_list_details, null);
final Locations_modle location = (Locations_modle) objects.get(position);
TextView text_cust_name = (TextView) locations_row.findViewById(R.id.txt_cust_name_heading);
TextView latitude = (TextView) locations_row.findViewById(R.id.txt_latitude);
latitude.setText(location.getLatitude());
TextView longitude = (TextView) locations_row.findViewById(R.id.txt_longitude);
distance_text = (TextView) locations_row.findViewById(R.id.txt_distance);
longitude.setText(location.getLongitude());
text_cust_name.setText(location.getLocationName());
CheckBox check_locations = (CheckBox) locations_row.findViewById(R.id.check_locations);
final Location location_point_a = new Location("Source");
final Location location_point_b = new Location("Destination");
location_point_a.setLatitude(Double.parseDouble(location.getLatitude()));
location_point_a.setLongitude(Double.parseDouble(location.getLongitude()));
if (position == selectedPostion) {
check_locations.setChecked(true);
} else {
check_locations.setChecked(false);
}
check_locations.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
// selectedPostion = position;
latitude_string = location.getLatitude();
longitude_string = location.getLongitude();
baseLat = Double.parseDouble(latitude_string);
baseLong = Double.parseDouble(longitude_string);
for (int i = 0; i < objects.size(); i++) {
finalLat = Double.parseDouble(objects.get(i).getLatitude());
finalLong = Double.parseDouble(objects.get(i).getLongitude());
calculateDistance(baseLat, baseLong, finalLat, finalLong, "k");
}
distance_text.setText(Double.toString(calculateDistance(baseLat, baseLong, finalLat, finalLong, "k")));
} /*else {
selectedPostion = -1;
}
notifyDataSetChanged();
*/
}
});
return locations_row;
}
}
Can any one tell how to achieve this
You can try this:
public static Double distanceBetween(LatLng point1, LatLng point2)
{
if (point1 == null || point2 == null) {
return null;
}
else{
return SphericalUtil.computeDistanceBetween(point1, point2);
}
}
You can try this
public float distance (float lat_a, float lng_a, float lat_b, float lng_b )
{
double earthRadius = 3958.75;
double latDiff = Math.toRadians(lat_b-lat_a);
double lngDiff = Math.toRadians(lng_b-lng_a);
double a = Math.sin(latDiff /2) * Math.sin(latDiff /2) +
Math.cos(Math.toRadians(lat_a)) * Math.cos(Math.toRadians(lat_b)) *
Math.sin(lngDiff /2) * Math.sin(lngDiff /2);
double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
double distance = earthRadius * c;
int meterConversion = 1609;
return new Float(distance * meterConversion).floatValue();
}
Use this method:
public static double haversineDistance(double lat1, double lng1, double lat2, double lng2) {
final int R = 6371; // Radious of the earth
double latDistance = toRad(lat2-lat1);
double lonDistance = toRad(lng2-lng1);
double a = Math.sin(latDistance / 2) * Math.sin(latDistance / 2) +
Math.cos(toRad(lat1)) * Math.cos(toRad(lat2)) *
Math.sin(lonDistance / 2) * Math.sin(lonDistance / 2);
double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
double distance = R * c;
distance = distance * 1000 * 0.000621371; // DISTANCE IN MILES
return (distance / 0.62137); // CONVERT MILES TO KM
}
public static double toRad(double value) {
return value * Math.PI / 180;
}
You can try this:
Location loc = new Location("");
Location p = new Location("");
loc.setLatitude(locLat);
loc.setLongitude(locLon);
p.setLatitude(pLat);
p.setLongitude(pLon);
double dis = loc.distanceTo(p);
String.format("%.2f", dis * 0.001);
In loc you set your location
You can use internal Api to find distance between 2 latlng
public double getDistanceBetweenPoints(LatLng origin, LatLng dest, String unit) {
Location selected_location = new Location("Start");
selected_location.setLatitude(origin.latitude);
selected_location.setLongitude(origin.longitude);
Location near_locations = new Location("End");
near_locations.setLatitude(dest.latitude);
near_locations.setLongitude(dest.longitude);
//in meters
double dist = selected_location.distanceTo(near_locations);
if (unit.equals("K")) {
dist = dist * 1.609344;
} else if (unit.equals("N")) {
dist = dist * 0.8684;
}
return dist;
}

intelligent calculating distance between two geo locations in android

Look at this example:
public void start(){
//...
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, TEN_SECONDS, TEN_METERS, this);
}
#Override
public void onLocationChanged(Location location) {
if(location.distanceTo(_lastLocation) > TEN_KM_IN_METERS){
actionA(location);
_lastLocation = location;
} else {
actionB(location);
}
}
The implementation of Location#distanceTo(l) is pretty complicated and CPU-intensive. So i don't want to call this operation on every location update.
Question: is it any proper way to avoid unnecessary Location#distanceTo(l) calls
What i tried so far. According Wiki - Decimal degrees i do it that way:
private boolean closeTogether(Location a, Location b) {
double changeLat = Math.abs(a.getLatitude() - b.getLatitude());
final float myNaiveMax = 0.005;
if (changeLat > myNaiveMax) {
return false;
}
double changeLon = Math.abs(a.getLongitude() - b.getLongitude());
if (changeLon > myNaiveMax) {
return false;
}
return true;
}
#Override
public void onLocationChanged(Location location) {
if(!closeTogether(location, _lastLocation) && location.distanceTo(_lastLocation) > TEN_KM_IN_METERS){
actionA(location);
_lastLocation = location;
} else {
actionB(location);
}
}
I've found that the Haversine formula is very good for this. Works well for my delivery tracking application. Here's how I calculate the distance between two points. Should get you started :)
/**
* getDistanceBetweenTwoPoints
* #param p1 - First point
* #param p2 - Second point
* #return distance between the two specified points (as the crow flys)
*/
public static double getDistanceBetweenTwoPoints(PointF p1, PointF p2) {
double R = 6371000; // Earth radius
double dLat = Math.toRadians(p2.x - p1.x);
double dLon = Math.toRadians(p2.y - p1.y);
double lat1 = Math.toRadians(p1.x);
double lat2 = Math.toRadians(p2.x);
double a = Math.sin(dLat / 2) * Math.sin(dLat / 2) + Math.sin(dLon / 2)
* Math.sin(dLon / 2) * Math.cos(lat1) * Math.cos(lat2);
double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
double d = R * c;
return d;
}
Edit And another
public static PointF calculateDerivedPosition(PointF point,
double range, double bearing)
{
double EarthRadius = 6371000; // m
double latA = Math.toRadians(point.x);
double lonA = Math.toRadians(point.y);
double angularDistance = range / EarthRadius;
double trueCourse = Math.toRadians(bearing);
double lat = Math.asin(Math.sin(latA) * Math.cos(angularDistance) +
Math.cos(latA) * Math.sin(angularDistance) * Math.cos(trueCourse));
double dlon = Math.atan2(Math.sin(trueCourse) * Math.sin(angularDistance) * Math.cos(latA),
Math.cos(angularDistance) - Math.sin(latA) * Math.sin(lat));
double lon = ((lonA + dlon + Math.PI) % (Math.PI * 2)) - Math.PI;
lat = Math.toDegrees(lat);
lon = Math.toDegrees(lon);
PointF newPoint = new PointF((float) lat, (float) lon);
return newPoint;
}

Making gps calls faster and more efficient

I've recently been working on simple speed calculations for an application I'm working on, but the code I have takes far too long to retrieve the location, and I know that questions like this have been asked before, but none of the answers seem to retrieve the results I'm looking for. So, how can I make this code have gps fixes within a matter of seconds, and is it even possible?
My LocationListener:
package me.dylan.acf;
import java.text.DecimalFormat;
import java.util.ArrayList;
import android.app.NotificationManager;
import android.location.Location;
import android.location.LocationListener;
import android.os.Bundle;
import android.text.format.Time;
import android.widget.TextView;
public class GPSManager implements LocationListener {
ArrayList<Double> avgspeeds = new ArrayList<Double>();
TextView debug;
NotificationManager mngr;
double avgspeed;
long lastTime = 0;
GraphView view;
Location lastloc;
int earthRadius = 6371;
long delaytime = 30;
ArrayList<Double> allspeeds = new ArrayList<Double>();
public GPSManager(TextView view) {
debug = view;
Location location = ACF.instance.lmanager
.getLastKnownLocation(ACF.instance
.getProperLocationsServices(ACF.instance
.getApplicationContext()));
if (location != null) {
double speed = location.getSpeed();
lastloc = location;
debug.setText("Average Speed: " + avgspeed + "\nCurrent speed: "
+ speed + "\nLocation updates: " + avgspeeds.size());
}
}
#Override
public void onLocationChanged(Location location) {
// DecimalFormat format = new DecimalFormat("0.00");
double speed = location.getSpeed();
if (lastloc != null) {
double latDist = Math.toRadians(location.getLatitude()
- lastloc.getLatitude());
double lonDist = Math.toRadians(location.getLongitude()
- lastloc.getLongitude());
double lat1 = Math.toRadians(location.getLatitude());
double lat2 = Math.toRadians(lastloc.getLatitude());
double a = Math.sin(latDist / 2) * Math.sin(latDist / 2)
+ Math.sin(lonDist / 2) * Math.sin(lonDist / 2)
* Math.cos(lat1) * Math.cos(lat2);
double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
double dist = earthRadius * c;
speed = (dist * 0.621371) / Math.abs(System.currentTimeMillis() - lastTime * 60 * 60 * 60);
lastTime = System.currentTimeMillis();
}
allspeeds.add(speed);
if (allspeeds.size() > 30) {
allspeeds.remove(0);
}
avgspeed = 0;
for (double d : allspeeds) {
avgspeed += d;
}
avgspeed /= allspeeds.size();
// avgspeed = Double.parseDouble(format.format(avgspeed));
avgspeeds.add(avgspeed);
lastloc = location;
debug.setText("Average Speed: " + avgspeed + "\nCurrent speed: "
+ speed + "\nLocation updates: " + avgspeeds.size());
}
#Override
public void onProviderDisabled(String provider) {
}
#Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
}
Where I call it:
public void updateWithProperService() {
lmanager.requestSingleUpdate(
getProperLocationsServices(getApplicationContext()), GPSmngr,
null);
Timer timer = new Timer();
timer.schedule(new TimerTask() {
#Override
public void run() {
updateWithProperService();
}
}, 10000);
}
public String getProperLocationsServices(Context context) {
if (lmanager == null)
lmanager = (LocationManager) context
.getSystemService(Context.LOCATION_SERVICE);
int minTime = 3000;
/*
* boolean isGPS = false; boolean isNetwork = false; try { isGPS =
* lmanager.isProviderEnabled(LocationManager.GPS_PROVIDER); } catch
* (Exception e) { e.printStackTrace(); } try { isNetwork = lmanager
* .isProviderEnabled(LocationManager.NETWORK_PROVIDER); } catch
* (Exception e) { e.printStackTrace(); }
*/
List<String> matchingProviders = lmanager.getAllProviders();
Location bestResult = null;
long bestTime = 0;
for (String provider : matchingProviders) {
Location location = lmanager.getLastKnownLocation(provider);
if (location != null) {
// float accuracy = location.getAccuracy();
long time = location.getTime();
// float bestAccuracy;
/*
* if ((time > minTime && accuracy < bestAccuracy )) {
* bestResult = location; bestTime = time; } else
*/if (time < minTime &&
/* bestAccuracy == Float.MAX_VALUE && */time < bestTime) {
bestResult = location;
bestTime = time;
}
}
}
if (bestResult != null)
return bestResult.getProvider();
else
return LocationManager.NETWORK_PROVIDER;
}
google has released a nice API on their recent Google IO 2013 event:
https://developers.google.com/events/io/sessions/324498944
you should check it out and see how you can minimize your code.
do note that it requires that the device would have the play store app for this to work.
this method has many advantages over using the normal location sensors (battery, speed , accuracy,...) .
gps needs about 15 seconds if it was disabled before and when using Assisted GPS.
you cannot improve that.
Whitout AGPS it needs 25-35 seconds, under good conditions.
The term is called "Time to First fix" (TTF).

Categories

Resources