Thursday 19 July 2012

Using GPS How to get current location Android

public getLocation()
{
LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
Criteria criteria = new Criteria();
criteria.setPowerRequirement(Criteria.POWER_LOW);
criteria.setAccuracy(Criteria.ACCURACY_COARSE);
String bestprovider = locationManager.getBestProvider(criteria,false);
Location lastknownlocation = locationManager.getLastKnownLocation(bestprovider);
Geocoder geocoder = new Geocoder(SignUp.this, Locale.getDefault());
try{

if(lastknownlocation!=null)
{
List<Address> addresses = geocoder.getFromLocation(lastknownlocation.getLatitude(),lastknownlocation.getLongitude(),100);
showToast(addresses.get(0).getAddressLine(0)+", "+addresses.get(0).getAddressLine(1)+", "+addresses.get(0).getCountryName());
}
else{
Toast.makeText(this, "location is not available", 3000).show();
}
}
catch (IOException e)
{
e.printStackTrace();
}

}


<manifest>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>
</manifest>

No comments:

Post a Comment