I need to find the Current latitude and longitude to get the address, but its not working.
here is my permisions:
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>
my java code:
public void GetCurrentLocal() throws IOException{
LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
Criteria criteria = new Criteria();
String provider = locationManager.getBestProvider(criteria, true);
Location myLocation = locationManager.getLastKnownLocation(provider);
double latitude = myLocation.getLatitude();
double longitude = myLocation.getLongitude();
Geocoder geocoder;
List<Address> addresses;
geocoder = new Geocoder(this, Locale.getDefault());
addresses = geocoder.getFromLocation(latitude, longitude, 1);
String address = addresses.get(0).getAddressLine(0);
System.out.println(address);
}
thanks.
If it fails on myLocation.getLatitude();
as you mentioned in the comment then it means that locationManager.getLastKnownLocation(provider);
doesn't return a last known location because it doesn't have one so it return null and this is the reason you have a NullPointerExeception
error. you need to implement a LocationListener
in this case and to run this method after you have received at least one location update.
To implement LocationListener
you can check this example:
http://www.androidhive.info/2012/07/android-gps-location-manager-tutorial/
I guess that this reading material could be really handy as well for you:
http://android-developers.blogspot.de/2011/06/deep-dive-into-location.html