[英]Android: Google Fused Location doesn't work while location is turned off
getLastLocation()
returns NULL while phone location is Off on my 6.0 Nexus 7 tablet, but it works without GPS on another device. 在我的6.0 Nexus 7平板电脑上手机位置关闭时,
getLastLocation()
返回NULL,但在另一台设备上没有GPS的情况下,该功能也可以正常工作。 Why is it so and any way to solve it? 为什么会这样,并且有什么解决办法? I want to keep GPS off and just use network to get location.
我想保持GPS关闭,仅使用网络获取位置。
Below is the class I am using to get location: 下面是我用来获取位置的类:
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;
}
}
}
It isn't because of the GPS thing but, the Location Settings
is not enabled yet in the device, it is under your device Settings>Google>Services>Location, or Settings>Privacy&security>Location, or just Settings>Location. 这不是由于GPS引起的,而是设备中尚未启用“
Location Settings
,它位于设备的“设置”>“ Google”>“服务”>“位置”或“设置”>“隐私和安全性”>“位置”下,或者仅在“设置”>“位置”下。 I got confused because in some device the drop-down status bar shows GPS enabling button and some shows Location enabling button. 我很困惑,因为在某些设备中,下拉状态栏显示GPS启用按钮,而某些显示位置启用按钮。 There is a way to enable the Location Settings automatically (without navigating user to device settings), it solved my issue, hope it helps you:
有一种方法可以自动启用“位置设置”(无需将用户导航到设备设置),它解决了我的问题,希望对您有所帮助:
After GoogleApiClient is connected, do a Location Settings checking as below: 连接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;
}
}
});
}
Through Google Api i think it's not possible.
我认为通过Google Api是不可能的。 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.
为此,您需要使用一些api进行ip地理位置定位,或者您可以通过此方法使用MNC-Mobile网络位置或MCC-Mobile国家/地区代码。 It may help you.
它可能会帮助您。
Th following explains the not returning any location value part: 以下内容说明了不返回任何位置值的部分:
LocationRequest . LocationRequest 。 PRIORITY_NO_POWER :
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.
除非其他客户端请求位置更新, 否则不会返回任何位置,在这种情况下,此请求将充当这些位置的被动侦听器。
For FusedLocationProvider , 对于FusedLocationProvider ,
no explicitly unique source is specified anywhere within documentation .
在文档中任何地方都没有指定明确的唯一来源 。
So for obtaining the location without enabling GPS, you can trust PRIORITY_BALANCED_POWER_ACCURACY . 因此,要在不启用GPS的情况下获取位置,可以信任PRIORITY_BALANCED_POWER_ACCURACY 。 From what i have observed, only PRIORITY_HIGH_ACCURACY uses GPS in a mandatory fashion.
根据我的观察,只有PRIORITY_HIGH_ACCURACY以强制方式使用GPS。
A tablet could be wifi-only or not have a sim which, unlike other devices, rules out the scenario of location being obtained by other apps – maybe using the network. 平板电脑可能仅具有wifi功能,或者没有SIM卡,与其他设备不同,该卡排除了其他应用程序(可能使用网络)获取位置信息的情况。
You have to set Location accuracy to Device Only in the emulator. 您必须在模拟器中将“位置精度”设置为“仅设备”。 High Accuracy or Battery Saving will not work with
FusedLocationProviderApi
in the emulator. 高精度或省电模式
FusedLocationProviderApi
用于仿真器中的FusedLocationProviderApi
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.