/**
 *
 * @author Dalvin
 * This class handles the task of getting user's current location.
 */
public class MyLocationManager{
private static long MINTIME = 3000;
private static Geocoder geoCoder = null;
private static final int MAX_RESULTS = 1;
private static MyLocationManagerCallBack myLocationManagerCallBack;
private static LocationManager locationManager;
private static final String TAG = "MyLocationManager";
public static LocationListener locationListener = new LocationListener() {
 @Override
public void onStatusChanged(String provider, int status, Bundle extras) {
if(status != LocationProvider.AVAILABLE){
MyLocationManager.myLocationManagerCallBack.onResultCallBack(
StatusCodes.LOCATION_PROVIDER_UNAVAILABLE,
StatusCodes.getErrorMsg(StatusCodes.LOCATION_PROVIDER_UNAVAILABLE),
null,null,null,null);
MyLocationManager.locationManager.removeUpdates(MyLocationManager.locationListener);
}
}
 @Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
 }
 @Override
public void onProviderDisabled(String provider) {
MyLocationManager.myLocationManagerCallBack.onResultCallBack(
StatusCodes.LOCATION_PROVIDER_DISABLED,
StatusCodes.getErrorMsg(StatusCodes.LOCATION_PROVIDER_DISABLED),
null,null,null,null);
MyLocationManager.locationManager.removeUpdates(MyLocationManager.locationListener);
}
 @Override
public void onLocationChanged(Location location) {
Log.d(TAG, "onLocationChanged START");
MyLocationManager.locationManager.removeUpdates(MyLocationManager.locationListener);
if(geoCoder!=null){
try {
List<Address> listOfAddresses =
geoCoder.getFromLocation(location.getLatitude(),
location.getLongitude(), MAX_RESULTS);
if(listOfAddresses != null && listOfAddresses.size()>0){
Address address = listOfAddresses.get(0);
 MyLocationManager.myLocationManagerCallBack.onResultCallBack
(StatusCodes.SUCCESS,StatusCodes.getErrorMsg(StatusCodes.SUCCESS),
Double.toString(location.getLatitude()),
Double.toString(location.getLongitude()),
address.getAddressLine(0),
address.getPostalCode());
address = null;
listOfAddresses = null;
}else{
MyLocationManager.myLocationManagerCallBack.onResultCallBack(
StatusCodes.SUCCESS,StatusCodes.getErrorMsg(StatusCodes.SUCCESS),
Double.toString(location.getLatitude()),
Double.toString(location.getLongitude()),null,null);
 }
} catch (IOException e) {
e.printStackTrace();
}
}
Log.d(TAG, "onLocationChanged END");
}
};
 public static void getCurrentLocation(Context context,
MyLocationManagerCallBack myLocationManagerCallBack){
Log.d(TAG, "getCurrentLocation START");
if(geoCoder == null) geoCoder = new Geocoder(context);
MyLocationManager.myLocationManagerCallBack = myLocationManagerCallBack;
 MyLocationManager.locationManager = (LocationManager)
context.getSystemService(Context.LOCATION_SERVICE);
 
MyLocationManager.locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
MINTIME, 0, MyLocationManager.locationListener);
 Log.d(TAG, "getCurrentLocation END");
}
 public static void cancelCurrentLocation(){
if(MyLocationManager.locationManager != null){
MyLocationManager.locationManager.removeUpdates(MyLocationManager.locationListener);
}
}
}

/*
* Callback interface
*/
public interface MyLocationManagerCallBack {
public void onResultCallBack(int status, String statusValue,
String lat, String lng, String addressLine, String zipCode);
}

Cheers
Dalvin

On Mon, Apr 9, 2012 at 9:47 PM, Justin Anderson <[email protected]>wrote:

> http://lmgtfy.com/?q=android+current+gps+coordinates
>
> Thanks,
> Justin Anderson
> MagouyaWare Developer
> http://sites.google.com/site/magouyaware
>
>
>
> On Mon, Apr 9, 2012 at 10:07 AM, Mini agrawal <[email protected]> wrote:
>
>> Hi,
>>
>> Can anybody provide a  link to get latitude and longitude of current
>> location?/
>>
>> Thanks in advance!!
>>
>> --
>> You received this message because you are subscribed to the Google
>> Groups "Android Developers" group.
>> To post to this group, send email to [email protected]
>> To unsubscribe from this group, send email to
>> [email protected]
>> For more options, visit this group at
>> http://groups.google.com/group/android-developers?hl=en
>
>
>  --
> You received this message because you are subscribed to the Google
> Groups "Android Developers" group.
> To post to this group, send email to [email protected]
> To unsubscribe from this group, send email to
> [email protected]
> For more options, visit this group at
> http://groups.google.com/group/android-developers?hl=en
>

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to