getLastLocation()
会返回NULL,但在其他设备上没有GPS可以正常工作。为什么会如此以及任何解决方法?我希望关闭GPS,只需使用网络获取位置。
以下是我用来获取位置的课程:
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;
}
}
}
答案 0 :(得分:2)
这不是因为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;
}
}
});
}
答案 1 :(得分:0)
通过谷歌Api,我认为这是不可能的。为此你需要使用一些api进行ip地理定位,或者你可以通过这个使用MNC-Mobile网络位置或MCC-Mobile国家代码。 它可能对你有帮助。
答案 2 :(得分:0)
以下说明不返回任何位置值部分:
LocationRequest。PRIORITY_NO_POWER:
除非其他客户请求,否则不会返回任何地点 位置更新,在这种情况下,此请求将充当被动 倾听那些地点。
no explicitly unique source is specified anywhere within documentation。
因此,为了在不启用GPS的情况下获取位置,您可以信任PRIORITY_BALANCED_POWER_ACCURACY。根据我的观察,只有PRIORITY_HIGH_ACCURACY以强制方式使用GPS。
平板电脑可能只支持wifi或没有SIM卡,与其他设备不同,它排除了其他应用程序获取位置的情况 - 可能使用网络。
答案 3 :(得分:0)
您必须在模拟器中将位置精度设置为仅设备。高精度或省电不适用于模拟器中的FusedLocationProviderApi
。