问题
Is it possible to get the Lat & Lng without going outdoor? My current situation is that user would have to be outdoor in order to get their location , in which also takes quite a long time for them to get one. I want my user to be able to get their location indoor.
回答1:
Yes it is possible , take hint from following code:
try {
gps_enabled = locManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
} catch (Exception ex) {
}
try {
network_enabled = locManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
} catch (Exception ex) {
}
if (!gps_enabled && !network_enabled) {
// show alert
}
if (network_enabled) {
locManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locListener);
}
if (gps_enabled) {
locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locListener);
}
回答2:
Yes, It is Possible, Inside the door you are getting your network. so you can use LocationManager.NETWORK_PROVIDER
for getting the GPS Co-Ordinates.
Please look at to this answer.
回答3:
It depends on the GPS Provider you are using, and signal availabiliy to fix the location, I think you are using GPS provider only, so it is not working in door, try using network provider also in consideration.
Also, use Last known location, by the time gps fixes location, there is a good blog to fetch location at earliest.
android-developers.blogspot.in/2011/06/deep-dive-into-location.html
回答4:
LocationManager locManager;
Location location;
double lat;
double lng;
try {
gps_enabled = locManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
} catch (Exception ex) {
}
try {
network_enabled = locManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
} catch (Exception ex) {
}
if (!gps_enabled && !network_enabled) {
// show alert
}
if (network_enabled) {
locManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
locManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,1000,1, locationListener);
location = locManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
lat = location.getLatitude();
lng = location.getLongitude();
}
if (gps_enabled) {
locManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,1000,1, locationListener);
location = locManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
lat = location.getLatitude();
lng = location.getLongitude();
}
来源:https://stackoverflow.com/questions/9372725/android-getting-latitude-and-longitude