问题
Here I am using this code in order to get the latitude and longitude of the location but all that I am getting is latitude 0.0 and longitude 0.0 the code to find the latitude and logitude that I am using is given as below , Also I am testing this code in Genymotion emulator and all that is printed in the log is 0.0 0.0 because the if(address.size()>0) is not getting satisfyied can anyone tell where am I going wrong or is it because of emulator.
Geocoder geo = new Geocoder(getActivity());
try {
List<Address> address = geo.getFromLocationName(str_ELocation,1);
if (address.size() > 0) {
Log.d("List Size : ", "" + address.size());
latitude = address.get(0).getLatitude();
longitude = address.get(0).getLongitude();
} else {
Log.d("In Else", "In Else");
}
} catch (IOException e) {
// TODO: handle exception
e.printStackTrace();
}
Log.d("LONGITUDE AND LATITUDE", "" + longitude + "" + latitude);
Also is there any other simpler solution to just find latitude and longitude. I am using this in Fragment and so I am using getActivity() here instead of myClassName.this.
回答1:
I'm guessing you don't have a backend service you are using to obtain this information.
Note The Docs say
The Geocoder class requires a backend service that is not included in the core android framework. The Geocoder query methods will return an empty list if there no backend service in the platform. Use the isPresent() method to determine whether a Geocoder implementation exists.
This should also be done on a background Thread
if not already being done.
Alternative
You may be better off using LocationManager
Be sure to read those docs on it before beginning to have an understanding of how it works and should be implemented (That portion is short).
Here is an example that can get you started
Anything like this should be done on a background Thread
and not on the UI Thread
. And there are listeners which can return the values you need.
full disclosure: I haven't used GPS but I read through the docs ;)
来源:https://stackoverflow.com/questions/22578724/not-getting-the-real-latitude-and-longitude-only-getting-0-0-0-0