我正在尝试通过GPS或位置提供商获取用户的当前位置,但我尝试的每个解决方案(很多来自stacksoverflow和google,youtube)都给出了经度和纬度的空值。
以下是我正在使用的代码
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
上面的代码总是给出0.0和0.0作为lat和long。
我还在Android Manifest中包含了权限:
getaddrinfo ENOTFOUND localhost
Error: getaddrinfo ENOTFOUND localhost
at errnoException (dns.js:28:10)
at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:76:26)
有什么建议吗?
答案 0 :(得分:0)
public static void getLocation(final Context context, final Looper looper, final LocationUpdateListener locationUpdateListener, final LocationListener locationListener) {
try {
boolean isProviderEnabled;
Location location;
final LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
isProviderEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
if (isProviderEnabled) {
if (ContextCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
locationManager.requestSingleUpdate(LocationManager.GPS_PROVIDER, locationListener, looper);
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
if (ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
locationManager.removeUpdates(locationListener);
}
getBackupLocation(context, looper, locationUpdateListener, locationListener);
}
}, 10000);
return;
}
}
isProviderEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (isProviderEnabled) {
locationManager.requestSingleUpdate(LocationManager.NETWORK_PROVIDER, locationListener, looper);
return;
}
location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location != null) {
if (locationUpdateListener != null) {
locationUpdateListener.onLocationUpdate(location);
locationManager.removeUpdates(locationListener);
}
return;
}
location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null) {
if (locationUpdateListener != null) {
locationUpdateListener.onLocationUpdate(location);
locationManager.removeUpdates(locationListener);
}
return;
}
float defaultValue = -10000;
SharedManager sharedManager = SharedManager.getInstance();
double lat = sharedManager.getDoubleValue(SharedManager.LAT, defaultValue);
double lng = sharedManager.getDoubleValue(SharedManager.LNG, defaultValue);
if (lat != defaultValue && lng != defaultValue) {
location = new Location(LocationManager.PASSIVE_PROVIDER);
location.setLatitude(lat);
location.setLongitude(lng);
if (locationUpdateListener != null) {
locationUpdateListener.onLocationUpdate(location);
locationManager.removeUpdates(locationListener);
}
return;
}
location = new Location(LocationManager.PASSIVE_PROVIDER);
location.setLatitude(PASSIVE_LAT);
location.setLongitude(PASSIVE_LONG);
if (locationUpdateListener != null) {
locationUpdateListener.onLocationUpdate(location);
locationManager.removeUpdates(locationListener);
}
}
catch(
Exception e)
{
//No Location
Location location = new Location(LocationManager.PASSIVE_PROVIDER);
location.setLatitude(PASSIVE_LAT);
location.setLongitude(PASSIVE_LONG);
if (locationUpdateListener != null) {
locationUpdateListener.onLocationUpdate(location);
}
}
}
检查GPS,网络和LastKnownLocation的工作功能.. 我的建议:首先使用LastKnownLocation然后使用Netowrk和最后一个度假村作为GPS ..减去电池使用量。
位置结果返回听众。
答案 1 :(得分:0)
这就是我用于项目的全部内容。希望能帮助到你!
声明:
Location location;
LocationManager locationManager;
String provider;
的onCreate:
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
provider = locationManager.getBestProvider(new Criteria(), false);
locationManager.requestLocationUpdates(provider, 400, 1, this);
location = locationManager.getLastKnownLocation(provider);
lat = location.getLatitude(); long = location.getLongitude();