Android: GpsLocation class

Questions about programming languages and debugging
Post Reply
User avatar
Gogeta70
^_^
^_^
Posts: 3275
Joined: 25 Jun 2005, 16:00
18

Android: GpsLocation class

Post by Gogeta70 »

Hey dudes. I've been developing an app for my employer and one of the functions is to provide a GPS location when an employee clocks in using the app. I wrote a class to provide the GPS location in simple manner, so i thought i would share it.

Code: Select all

package com.example.sei;

import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;

public class GpsLocation {
	
	public LocationManager lManager = null;
	public boolean GpsEnabled, NetLocEnabled;
	
	double Latitude, Longitude;
	float Accuracy;
	
	public MyActivity callback;
	
	GpsLocation(){}
	
	public boolean StartGPS(Context c)
	{
		
		lManager = (LocationManager) c.getSystemService(Context.LOCATION_SERVICE);
		
		Accuracy = 9999;
		
		if(lManager == null)
			return false;
		
		if(!IsLocationAvailable(c))
			return false;
		
		if(!GpsEnabled && !NetLocEnabled)
			return false;
		
		if(GpsEnabled)
		{
			lManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
		}
		
		if(NetLocEnabled)
		{
			lManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
		}
		
		callback = (MyActivity) c;
		
		return true;
	}
	
	public boolean IsLocationAvailable(Context c)
	{
		GpsEnabled = false;
		NetLocEnabled = false;
		
		if(lManager == null)
			lManager = (LocationManager) c.getSystemService(Context.LOCATION_SERVICE);
		
		try {
			
			if(lManager.isProviderEnabled(LocationManager.GPS_PROVIDER))
				GpsEnabled = true;
			
			if(lManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER))
				NetLocEnabled = true;
			
		} catch (Exception e) {
			
			e.printStackTrace();
		}
		
		if(GpsEnabled || NetLocEnabled)
			return true;
		
		return false;
	}
	
	LocationListener locationListener = new LocationListener()
	{	
		public void onLocationChanged(Location location)
	    {
	    	
			if(location.getAccuracy() <= Accuracy)
			{
				Accuracy = location.getAccuracy();
	    		Latitude = location.getLatitude();
	    		Longitude = location.getLongitude();
	    		
	    		callback.GpsUpdate(Accuracy, Longitude, Latitude);
			}
	    }

		@Override
		public void onProviderDisabled(String provider) {}

		@Override
		public void onProviderEnabled(String provider) {}

		@Override
		public void onStatusChanged(String provider, int status, Bundle extras) {}
	};
	
	public void StopGPS()
	{
		lManager.removeUpdates(locationListener);
	}
	
}

// In your activity, create the GpsLocation class and create this method:

public void GpsUpdate(float Accuracy, double Longitude, double Latitude)
{
}

Anyway, just thought i'd share... i'm not too great at java yet, so there is probably a better way to do it though. ^_^
¯\_(ツ)_/¯ It works on my machine...

User avatar
bad_brain
Site Owner
Site Owner
Posts: 11636
Joined: 06 Apr 2005, 16:00
19
Location: In your eye floaters.
Contact:

Re: Android: GpsLocation class

Post by bad_brain »

um, wait...you mean you can track people with it via GPS? :-k
Image

User avatar
maboroshi
Dr. Mab
Dr. Mab
Posts: 1624
Joined: 28 Aug 2005, 16:00
18

Re: Android: GpsLocation class

Post by maboroshi »

Very nice man :-)

I Did some GPS Location stuff but in javascript. Will see if I can post the code ;)

Speak soon!

User avatar
Gogeta70
^_^
^_^
Posts: 3275
Joined: 25 Jun 2005, 16:00
18

Re: Android: GpsLocation class

Post by Gogeta70 »

The class just provides an interface to the GPS service that already exists in the phone. You could use it to track people, but you would have to make your own android app to do so.

I'm using it to track the location of our employees when they clock in and out using the app.
¯\_(ツ)_/¯ It works on my machine...

User avatar
joebox
Fame ! Where are the chicks?!
Fame ! Where are the chicks?!
Posts: 213
Joined: 19 Mar 2012, 18:15
12
Contact:

Re: Android: GpsLocation class

Post by joebox »

nice
Award winning Unlimited Web Hosting
$1.81/month http://www.topratedhostservice.com
3 free domains | $100.00 Free advertising credits | Free Custom website design
Image

Post Reply