1

getLastLocation()在我的6.0 Nexus 7平板电脑上手机位置关闭时返回NULL,但它在其他设备上无GPS功能。为什么是这样以及如何解决它?我想保持GPS关闭,只需使用网络获取位置。Android:Google Fused位置在位置关闭时不起作用

下面是我使用来获得位置类:

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

您是否找到任何解决方案 –

+0

@ M.Yogeshwaran yes请检查下面的答案:) –

回答

0

这是不是因为GPS的事情,但,在Location Settings未在设备没有启用,它是你的设备设置>谷歌>服务>位置,或设置>隐私&安全>位置,或者只是设置下>位置。我很困惑,因为在某些设备中,下拉状态栏显示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; 
      } 
     } 
    }); 
} 
+0

谢谢@BeeingJK有可能在后台运行这个东西 –

+0

你可以使用一个服务在后台运行它。位置服务的一个即时例子[这里](http://stackoverflow.com/a/34919769/4665578)(我从来没有尝试过),它包括谷歌播放服务检查和位置更新侦听器的功能,你可以做同样的事情在连接GoogleApiClient之后,请求位置之前检查“checkLocationSettings”。但对于'startResolutionForResult',你可能需要一个dialogActivity,因为它需要一个活动来提示对话。为了简化它,我只是把东西放在课堂上,然后从活动中调用,并在位置ON时拨打电话 –

+1

@BeeingJk但是,您的问题是如何在不启用GPS的情况下获取位置。但是,您在此启用gps.is'nt它? –

0

通过谷歌阿比我认为这是不可能的。为此,您需要使用一些API来进行IP地理定位,或者您可以使用MNC-Mobile网络位置或MCC移动国家代码。 它可能会帮助你。

+0

感谢您的回复,您的'不可能'意味着Google Fused Location无法在没有GPS的情况下获取设备位置?但它适用于其他设备 –

0

钍以下说明不返回任何位置值部分:
LocationRequestPRIORITY_NO_POWER

没有位置将被退回,除非不同的客户端请求 位置更新在这种情况下,该请求将作为一个被动的听众 到这些位置。

FusedLocationProvider对于,

no explicitly unique source is specified anywhere within documentation

因此,要获得没有启用GPS的位置,您可以信任PRIORITY_BALANCED_POWER_ACCURACY。从我所观察到的,只有PRIORITY_HIGH_ACCURACY强制使用GPS。

平板电脑可能只有wifi,或者没有与其他设备不同的SIM卡,可以排除其他应用获取位置的场景 - 可能使用网络。

+0

感谢您的解释,我用'PRIORITY_BALANCED_POWER_ACCURACY'取代了'PRIORITY_NO_POWER',但平板电脑仍然在其位置关闭时返回NULL,对此有何看法? –

0

您必须在模拟器中将位置准确性设置为仅设备。模拟器中的FusedLocationProviderApi不适用于高精度或省电模式。