Find Location using GPS in Android

by Sasikumar 2014-05-02 14:25:58

To find location using GPS in android use the following code,
public class MainActivity extends Activity implements LocationListener {
LocationManager myLocationManager ;
String myProvider;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

// Getting LocationManager object
myLocationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);

// Creating an empty criteria object
Criteria myCriteria = new Criteria();

// Getting the name of the provider that meets the criteria
myProvider = myLocationManager.getBestProvider(myCriteria, false);

if(myProvider!=null && !myProvider.equals("")){

// Get the location from the given provider
Location location = myLocationManager.getLastKnownLocation(myProvider);

myLocationManager.requestLocationUpdates(myProvider, 20000, 1, this);

if(location!=null)
{
// Getting reference to TextView displayview to display data
TextView displayview = (TextView)findViewById(R.id.displayview);

// Setting Current Longitude & Latitude
displayview.setText("Longitude:" + location.getLongitude() + " Latitude:" + location.getLatitude());
}
else
Toast.makeText(getBaseContext(), "Location can't be retrieved", Toast.LENGTH_SHORT).show();

}else{
Toast.makeText(getBaseContext(), "No Provider Found", Toast.LENGTH_SHORT).show();
}
}

 
587
like
0
dislike
0
mail
flag

You must LOGIN to add comments