3

I searched here for an idea to build an efficient location search that runs in a backround service. I found this link Energy efficient GPS tracking but it's not what i'm looking for. i'm looking for a simple solution that will check the distance in meters and then will know if the user is close or far from the target location and then save battery life. I don't want that the time dimension will have any effective in this algorithm only the distance.

Note: I have all the right permissions. here's my code (runs in the service):

public class LocationService extends Service{

    private static final int SLEEP = 250;
    private static final int VIBRATE = 500;

    private double targetLat;
    private double targetLng;
    private LocationManager manager;
    private boolean alarm;
    private String ring;
    private Uri soundUri;
    private long [] pattern;        
    private float minDistance;

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

    @SuppressWarnings("static-access")
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this);
        targetLat = Double.parseDouble(settings.getString("lat", "0"));
        targetLng = Double.parseDouble(settings.getString("lng", "0"));
        targetLat = targetLat / 1E6;
        targetLng = targetLng / 1E6;
        alarm = settings.getBoolean("alarm", true);
        ring = settings.getString("ringDet", "");
        if(ring != ""){
            soundUri = Uri.parse(ring);         
        }

        manager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
        manager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, minDistance, location);
        manager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, minDistance, location);

        pattern = new long[200];
        pattern[0] = 0;
        for(int i = 1; i < pattern.length; i++){
            if(i % 2 != 0){
                pattern[i] = SLEEP;
            }
            else{
                pattern[i] = VIBRATE;
            }
        }

        //return super.onStartCommand(intent, flags, startId);
        return super.START_STICKY;
    }   

    LocationListener location = new LocationListener() {

        public void onStatusChanged(String provider, int status, Bundle extras) {}
        public void onProviderEnabled(String provider) {}       
        public void onProviderDisabled(String provider) {}

        @SuppressWarnings("deprecation")
        public void onLocationChanged(Location location) {
            float [] results = new float [3];

            Location.distanceBetween(targetLat, targetLng, location.getLatitude(), location.getLongitude(), results);

            float distance = results[0];

            if(distance > 20000){
                minDistance = 15000;
                toaster(">20000");
                manager.removeUpdates(this);
            }
            else if(distance > 10000){
                minDistance = 5000;
                toaster(">10000");
                manager.removeUpdates(this);
            }
            else if(distance > 5000){
                minDistance = 2500;
                toaster(">5000");
                manager.removeUpdates(this);
            }
            else if(distance > 2500){
                minDistance = 1000;
                toaster(">2500");
                manager.removeUpdates(this);
            }
            else if(distance > 1000){
                minDistance = 0;
                toaster(">1000");               
            }

            if(distance < 800 && alarm){                
                Notification notification = new Notification(R.drawable.ic_launcher, "WakeApp", System.currentTimeMillis());
                Intent notificationIntent = new Intent(getApplicationContext(),MainActivity.class);

                PendingIntent contentIntent = PendingIntent.getActivity(getApplicationContext(), 0, notificationIntent, 0);
                notification.setLatestEventInfo(getApplicationContext(), getApplicationContext().getResources().getString(R.string.notification_message), "", contentIntent);

                NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);

                //notification.defaults |= Notification.DEFAULT_VIBRATE;
                notification.defaults |= Notification.DEFAULT_LIGHTS;       
                notification.icon = R.drawable.ic_launcher;         

                if(soundUri != null){
                    notification.sound = soundUri;
                }
                else if(soundUri == null){
                    soundUri = RingtoneManager.getActualDefaultRingtoneUri(getApplicationContext(), RingtoneManager.TYPE_ALARM);
                    notification.sound = soundUri;
                }                                                                                   

                notification.vibrate = pattern;

                alarm = false;
                notificationManager.notify(1, notification);                
                manager.removeUpdates(this);
                stopSelf();
            }
        }
    };

    private void toaster(String text){
        Toast.makeText(this, text, Toast.LENGTH_SHORT).show();
    }
Community
  • 1
  • 1
Shalom Melamed
  • 299
  • 1
  • 4
  • 13

1 Answers1

1

You can use following method to calculate distance between two gps position, in meter.

public static float distFrom (float lat1, float lng1, float lat2, float lng2 ) 
{
    double earthRadius = 3958.75;
    double dLat = Math.toRadians(lat2-lat1);
    double dLng = Math.toRadians(lng2-lng1);
    double a = Math.sin(dLat/2) * Math.sin(dLat/2) +
    Math.cos(Math.toRadians(lat1)) * Math.cos(Math.toRadians(lat2)) *
    Math.sin(dLng/2) * Math.sin(dLng/2);
    double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
    double dist = earthRadius * c;

    int meterConversion = 1609;

    return new Float(dist * meterConversion).floatValue();
}

Original source is my answer.

For GPS Co-Ordinate i have created one Library code like below,

public class LocListener implements LocationListener
{
    private static double lat =0.0;
    private static double lon = 0.0;
    private static double alt = 0.0; 
    private static double speed = 0.0;
    private static long dateTime;

    public static double getLat()
    {
        return lat;
    }

    public static double getLon() 
    {
        return lon;
    }

    public static double getAlt()
    {
        return alt;
    }

    public static double getSpeed()
    {
        return speed;
    }

    // Added By Kalpen Vaghela on 17 09 2012
    public static long getDateTime()
    {
        return dateTime;
    }

    @Override
    public void onLocationChanged(Location location) 
    {
        lat = location.getLatitude();
        lon = location.getLongitude();
        alt = location.getAltitude();
        speed = location.getSpeed(); 
        dateTime = location.getTime();
    }

    @Override
    public void onProviderDisabled(String provider) {}
    @Override
    public void onProviderEnabled(String provider) {}
    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {}
}
Community
  • 1
  • 1
Lucifer
  • 29,392
  • 25
  • 90
  • 143
  • thank for your answer. do you have an idea for algorithm gps search? – Shalom Melamed Sep 20 '12 at 11:03
  • hey lucifer, i don't see how is your suggestion helping the gps work less in different distances. from my understanding i need to use the requestLocationUpdates method so it will be effective. thanks anyway – Shalom Melamed Sep 21 '12 at 07:44