Android:Google Fused Location 在位置关闭时不起作用
Android: Google Fused Location doesn't work while location is turned off
getLastLocation()
returns NULL 而 phone location 在我的 6.0 Nexus 7 平板电脑上关闭,但在另一台设备上它可以在没有 GPS 的情况下工作。为什么会这样,有什么办法可以解决吗?我想关闭 GPS,只使用网络获取位置。
下面是我用来获取位置的class:
public class GPSCenter {
public static GoogleApiClient mGoogleApiClient;
public static Location mLastLocation;
private static Context mContext;
static GoogleApiClient.ConnectionCallbacks ccb = new GoogleApiClient.ConnectionCallbacks() {
@Override
public void onConnectionSuspended(int arg0) {
}
@Override
public void onConnected(Bundle arg0) {
if (ActivityCompat.checkSelfPermission(mContext, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
LocationRequest mLocationRequest = LocationRequest.create();
mLocationRequest.setPriority(LocationRequest.PRIORITY_NO_POWER);
mLastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
}
};
static GoogleApiClient.OnConnectionFailedListener odfl = new GoogleApiClient.OnConnectionFailedListener() {
@Override
public void onConnectionFailed(ConnectionResult arg0) {
}
};
public static synchronized void buildGoogleApiClient(Context c) {
try {
mContext = c;
mGoogleApiClient = new GoogleApiClient.Builder(c)
.addConnectionCallbacks(ccb)
.addOnConnectionFailedListener(odfl)
.addApi(LocationServices.API)
.build();
mGoogleApiClient.connect();
} catch (Exception ex) {
Log.i("location", "error " + ex.toString());
ex.printStackTrace();
}
}
public static double getLatitude(Context c) {
try {
return mLastLocation.getLatitude();
} catch (Exception ex) {
return 0.0;
}
}
public static double getLongitude(Context c) {
try {
return mLastLocation.getLongitude();
} catch (Exception ex) {
return 0.0;
}
}
}
Through Google Api i think it's not possible. for that you need to use some api for ip geolocation or you may use MNC-Mobile network location or MCC-Mobile country code through this you can get.
It may help you.
下面解释不返回任何位置值部分:
LocationRequest.PRIORITY_NO_POWER:
No locations will be returned unless a different client has requested
location updates in which case this request will act as a passive
listener to those locations.
no explicitly unique source is specified anywhere within documentation.
因此,为了在不启用 GPS 的情况下获取位置,您可以相信 PRIORITY_BALANCED_POWER_ACCURACY. From what i have observed, only PRIORITY_HIGH_ACCURACY 以强制方式使用 GPS。
平板电脑可能只有 wifi 或没有 sim 卡,这与其他设备不同,排除了其他应用程序获取位置的情况——可能使用网络。
这不是因为 GPS 问题,而是 Location Settings
尚未在设备中启用,它位于您的设备设置>Google>服务>位置或设置>隐私和安全> 位置,或只是设置> 位置。我很困惑,因为在某些设备中,下拉状态栏显示 GPS 启用按钮,有些显示位置启用按钮。
有一种方法可以自动启用位置设置(无需将用户导航到设备设置),它解决了我的问题,希望对您有所帮助:
连接 GoogleApiClient 后,进行位置设置检查,如下所示:
public static GoogleApiClient mGoogleApiClient;
private static PendingResult<LocationSettingsResult> result;
private static LocationSettingsRequest.Builder builder;
public static Location mLastLocation;
public static boolean isLocationON = false;
public static synchronized void buildGoogleApiClient(Activity a, Context c) {
try {
mActivity = a;
mContext = c;
mGoogleApiClient = new GoogleApiClient.Builder(c)
.addConnectionCallbacks(ccb)
.addOnConnectionFailedListener(odfl)
.addApi(LocationServices.API)
.build();
builder = new LocationSettingsRequest.Builder()
.addLocationRequest(locationRequest);
builder.setAlwaysShow(true);
mGoogleApiClient.connect();
} catch (Exception ex) {
ex.printStackTrace();
}
}
private static GoogleApiClient.ConnectionCallbacks ccb = new GoogleApiClient.ConnectionCallbacks() {
@Override
public void onConnected(Bundle arg0) {
checkLocationSettings();
}
@Override
public void onConnectionSuspended(int arg0) {
}
};
private static void checkLocationSettings() {
result = LocationServices.SettingsApi.checkLocationSettings(mGoogleApiClient, builder.build());
result.setResultCallback(new ResultCallback<LocationSettingsResult>() {
@Override
public void onResult(@NonNull LocationSettingsResult locationSettingsResult) {
final Status status = locationSettingsResult.getStatus();
switch (status.getStatusCode()) {
case LocationSettingsStatusCodes.SUCCESS:
isLocationON = true;
if (ActivityCompat.checkSelfPermission(mContext, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
mLastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
if (mLocationSettingsListener != null) {
mLocationSettingsListener.onLocationON();
}
break;
case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
isLocationON = false;
try {
// This line will check the result and prompt a dialog if the device location settings is not enabled
status.startResolutionForResult(mActivity, REQUEST_CHECK_SETTINGS);
} catch (IntentSender.SendIntentException e) {
}
break;
case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:
isLocationON = false;
// Location settings are unavailable so not possible to show any dialog now
if (mLocationSettingsListener != null) {
mLocationSettingsListener.onLocationError();
}
break;
}
}
});
}
您必须在模拟器中将位置精度设置为“仅设备”。模拟器中的 FusedLocationProviderApi
无法使用高精度或省电模式。
getLastLocation()
returns NULL 而 phone location 在我的 6.0 Nexus 7 平板电脑上关闭,但在另一台设备上它可以在没有 GPS 的情况下工作。为什么会这样,有什么办法可以解决吗?我想关闭 GPS,只使用网络获取位置。
下面是我用来获取位置的class:
public class GPSCenter {
public static GoogleApiClient mGoogleApiClient;
public static Location mLastLocation;
private static Context mContext;
static GoogleApiClient.ConnectionCallbacks ccb = new GoogleApiClient.ConnectionCallbacks() {
@Override
public void onConnectionSuspended(int arg0) {
}
@Override
public void onConnected(Bundle arg0) {
if (ActivityCompat.checkSelfPermission(mContext, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
LocationRequest mLocationRequest = LocationRequest.create();
mLocationRequest.setPriority(LocationRequest.PRIORITY_NO_POWER);
mLastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
}
};
static GoogleApiClient.OnConnectionFailedListener odfl = new GoogleApiClient.OnConnectionFailedListener() {
@Override
public void onConnectionFailed(ConnectionResult arg0) {
}
};
public static synchronized void buildGoogleApiClient(Context c) {
try {
mContext = c;
mGoogleApiClient = new GoogleApiClient.Builder(c)
.addConnectionCallbacks(ccb)
.addOnConnectionFailedListener(odfl)
.addApi(LocationServices.API)
.build();
mGoogleApiClient.connect();
} catch (Exception ex) {
Log.i("location", "error " + ex.toString());
ex.printStackTrace();
}
}
public static double getLatitude(Context c) {
try {
return mLastLocation.getLatitude();
} catch (Exception ex) {
return 0.0;
}
}
public static double getLongitude(Context c) {
try {
return mLastLocation.getLongitude();
} catch (Exception ex) {
return 0.0;
}
}
}
Through Google Api i think it's not possible. for that you need to use some api for ip geolocation or you may use MNC-Mobile network location or MCC-Mobile country code through this you can get. It may help you.
下面解释不返回任何位置值部分:
LocationRequest.PRIORITY_NO_POWER:
No locations will be returned unless a different client has requested location updates in which case this request will act as a passive listener to those locations.
no explicitly unique source is specified anywhere within documentation.
因此,为了在不启用 GPS 的情况下获取位置,您可以相信 PRIORITY_BALANCED_POWER_ACCURACY. From what i have observed, only PRIORITY_HIGH_ACCURACY 以强制方式使用 GPS。
平板电脑可能只有 wifi 或没有 sim 卡,这与其他设备不同,排除了其他应用程序获取位置的情况——可能使用网络。
这不是因为 GPS 问题,而是 Location Settings
尚未在设备中启用,它位于您的设备设置>Google>服务>位置或设置>隐私和安全> 位置,或只是设置> 位置。我很困惑,因为在某些设备中,下拉状态栏显示 GPS 启用按钮,有些显示位置启用按钮。
有一种方法可以自动启用位置设置(无需将用户导航到设备设置),它解决了我的问题,希望对您有所帮助:
连接 GoogleApiClient 后,进行位置设置检查,如下所示:
public static GoogleApiClient mGoogleApiClient;
private static PendingResult<LocationSettingsResult> result;
private static LocationSettingsRequest.Builder builder;
public static Location mLastLocation;
public static boolean isLocationON = false;
public static synchronized void buildGoogleApiClient(Activity a, Context c) {
try {
mActivity = a;
mContext = c;
mGoogleApiClient = new GoogleApiClient.Builder(c)
.addConnectionCallbacks(ccb)
.addOnConnectionFailedListener(odfl)
.addApi(LocationServices.API)
.build();
builder = new LocationSettingsRequest.Builder()
.addLocationRequest(locationRequest);
builder.setAlwaysShow(true);
mGoogleApiClient.connect();
} catch (Exception ex) {
ex.printStackTrace();
}
}
private static GoogleApiClient.ConnectionCallbacks ccb = new GoogleApiClient.ConnectionCallbacks() {
@Override
public void onConnected(Bundle arg0) {
checkLocationSettings();
}
@Override
public void onConnectionSuspended(int arg0) {
}
};
private static void checkLocationSettings() {
result = LocationServices.SettingsApi.checkLocationSettings(mGoogleApiClient, builder.build());
result.setResultCallback(new ResultCallback<LocationSettingsResult>() {
@Override
public void onResult(@NonNull LocationSettingsResult locationSettingsResult) {
final Status status = locationSettingsResult.getStatus();
switch (status.getStatusCode()) {
case LocationSettingsStatusCodes.SUCCESS:
isLocationON = true;
if (ActivityCompat.checkSelfPermission(mContext, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
mLastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
if (mLocationSettingsListener != null) {
mLocationSettingsListener.onLocationON();
}
break;
case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
isLocationON = false;
try {
// This line will check the result and prompt a dialog if the device location settings is not enabled
status.startResolutionForResult(mActivity, REQUEST_CHECK_SETTINGS);
} catch (IntentSender.SendIntentException e) {
}
break;
case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:
isLocationON = false;
// Location settings are unavailable so not possible to show any dialog now
if (mLocationSettingsListener != null) {
mLocationSettingsListener.onLocationError();
}
break;
}
}
});
}
您必须在模拟器中将位置精度设置为“仅设备”。模拟器中的 FusedLocationProviderApi
无法使用高精度或省电模式。