package com.samples.location.getcoordinates;

import android.app.Activity;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.TextView;

public class GetCoordinatesActivity extends Activity {

	private LocationManager manager;
	private TextView text;
	
	private LocationListener locListaner = new LocationListener() {
	
		public void onLocationChanged(Location argLocation) {
			printLocation(argLocation);
		}
	
		@Override
		public void onProviderDisabled(String arg0) {
			printLocation(null);
		}
	
		@Override
		public void onProviderEnabled(String arg0) {}
	
		@Override
		public void onStatusChanged(String arg0, int arg1, Bundle arg2) {}
	};
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		
		text = (TextView)findViewById(R.id.text);	
		
		manager = (LocationManager)getSystemService(
				  Context.LOCATION_SERVICE);		
		manager.requestLocationUpdates(
				LocationManager.GPS_PROVIDER, 0, 0, locListaner);
		
		Location loc = manager.getLastKnownLocation(
				LocationManager.GPS_PROVIDER);
		printLocation(loc);
	}
	
	private void printLocation(Location loc) {
		if (loc != null)
		{
		    text.setText("Longitude:\t" + loc.getLongitude() + 	    
	                "\nLatitude:\t" + loc.getLatitude());
		}
		else {
			text.setText("Location unavailable");
		}
	}
}