除非打开GPS,否则融合的位置提供程序将无法获取位置 [英] Fused Location Provider not getting location unless GPS is on
问题描述
因此,我已经实现了新的Fused Location Provider API以获取用户的位置,但是由于某种原因,除非GPS开启,否则我无法获得任何位置.并非总是这样,用户是否会打开GPS,我不想每次加载应用程序时都要求他们打开GPS. 如何告诉API向其提供任何可用的提供程序的位置?
So I have implemented the new Fused Location Provider API to get a location of the user but for some reason, I cannot get any location unless the GPS is on. Not always, will users have their GPS on and I would like to not have to ask them to turn their GPS on every time the load the app. How can I tell the API to give me a location with whatever provider it has available?
这是我的代码:
public class FusedLocationService implements
LocationListener,
GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener {
public interface OnLocationChangedListener {
public void OnLocationChanged(Location location);
}
private final String TAG = "SE8611";
private boolean mRequestingLocationUpdates = true;
private OnLocationChangedListener mCallBack;
Service locationService;
private LocationRequest locationRequest;
private GoogleApiClient googleApiClient;
private Location mCurrentLocation;
private FusedLocationProviderApi fusedLocationProviderApi = LocationServices.FusedLocationApi;
public FusedLocationService(Service locationService, final long INTERVAL, final long FASTEST_INTERVAL) {
Logger.log(TAG, "FusedLocationService called");
this.mCallBack = (OnLocationChangedListener)locationService;
locationRequest = LocationRequest.create();
locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
locationRequest.setInterval(INTERVAL);
locationRequest.setFastestInterval(FASTEST_INTERVAL);
this.locationService = locationService;
googleApiClient = new GoogleApiClient.Builder(locationService)
.addApi(LocationServices.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
if (googleApiClient != null) {
googleApiClient.connect();
}
}
@Override
public void onConnected(Bundle connectionHint) {
if(mRequestingLocationUpdates) {
startLocationUpdates();
}else{
Logger.log(TAG, "Location updates are already running.");
}
}
protected void startLocationUpdates() {
this.fusedLocationProviderApi.requestLocationUpdates(
googleApiClient, locationRequest, this);
this.mRequestingLocationUpdates = false;
}
@Override
public void onLocationChanged(Location mCurrentLocation) {
Logger.log(TAG, "onLocationChanged called");
this.mCurrentLocation = mCurrentLocation;
this.mCallBack.OnLocationChanged(this.mCurrentLocation);
}
public void startLocationUpdatesAfterResume(){
if (googleApiClient.isConnected() && !mRequestingLocationUpdates) {
Logger.log(TAG, "startLocationUpdatesAfterResume called");
this.startLocationUpdates();
}
}
public void stopLocationUpdates() {
Logger.log(TAG, "stopping Location Updates");
LocationServices.FusedLocationApi.removeLocationUpdates(
googleApiClient, this);
}
public Location getLocation() {
return this.mCurrentLocation;
}
@Override
public void onConnectionSuspended(int i) {
this.mRequestingLocationUpdates = true;
}
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
this.mRequestingLocationUpdates = true;
}
}
推荐答案
我遇到了与您相同的问题,这根本不是问题. Android曾经有一个GPS按钮,可让您直接控制它,但是他们将GPS替换为一个具有不同功能的位置"按钮. 为了获得任何类型的位置,您必须将其打开. 像您一样,我认为位置"按钮只能打开和关闭GPS,但事实并非如此. 您可以通过更改位置模式来控制GPS: 1.高精度(GPS,Wi-Fi和移动网络) 2.省电(Wi-Fi和移动网络) 3.仅限GPS
I had the same issue like you, which is not an issue at all. Android used to have a GPS button that let you control it directly, but they replaced it with a Location button which works different. In order to get any type of location, you must turn it on. Like you, I thought the Location button turns on and off the GPS only, but that's not the case. You can control the GPS by changing the location mode: 1. High accuracy (GPS, Wi-Fi and mobile networks) 2. Power Saving (Wi-Fi and mobile networks) 3. GPS only
这篇关于除非打开GPS,否则融合的位置提供程序将无法获取位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!