为什么FusedLocationApi.getLastLocation空 [英] Why is FusedLocationApi.getLastLocation null

本文介绍了为什么FusedLocationApi.getLastLocation空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过获得位置 FusedLocationApi.getLastLocation 和我有清单文件中的位置的权限:

I am trying to get location by using FusedLocationApi.getLastLocation and I've got the location permissions in the manifest file:

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

不过,我正当我从系统要求的位置。我只是测试关闭和打开定位服务,因此可能与该(当然,这是当我尝试此ON)。但是,即使返回null后,我在等 onLocationChanged 来被调用,它永远不会被调用。我也看到了这里一个类似的问题:<一href="http://stackoverflow.com/questions/29441384/fusedlocationapi-getlastlocation-always-null">FusedLocationApi.getLastLocation总是空

However, I am getting null when I request the location from the system. I was just testing turning off and on location services so it may be related to that (of course, it's ON when I'm trying this). But even after returning null, I'm waiting for onLocationChanged to be called and it's never called. I've also seen a similar question here: FusedLocationApi.getLastLocation always null

下面是我的code:

 protected LocationRequest createLocationRequest() {
    LocationRequest mLocationRequest = new LocationRequest();
    mLocationRequest.setInterval(120000);
    mLocationRequest.setFastestInterval(30000);
    mLocationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);
    return mLocationRequest;
}

protected GoogleApiClient getLocationApiClient(){
    return new GoogleApiClient.Builder(this)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .addApi(LocationServices.API)
            .build();
}

LocationRequest locationRequest = createLocationRequest(); 

@Override
public void onLocationChanged(Location location) {
    App.setLocation(location); // NEVER CALLED
}

@Override
public void onConnected(Bundle bundle) {
    App.setLocation(LocationServices.FusedLocationApi.getLastLocation(locationApiClient)); //RETURNS NULL
    LocationRequest locationRequest = createLocationRequest();
    LocationServices.FusedLocationApi.requestLocationUpdates(locationApiClient, locationRequest, this);
}

和上的onCreate 我的应用程序的:

locationApiClient = getLocationApiClient();
locationApiClient.connect();

其中,是我的应用程序对象。 为什么会出现空(是的,我的执行的知道,它可能是空的如文件中规定了一些罕见的情况,但是我总是因为这样,不是很少)仍然没有得到位置更新之后?这是一个真实设备(三星Galaxy S3)没有SIM卡,如果有帮助。

Where this is my application object. Why am I getting null (yes, I do know it may be null in some rare circumstances as stated in the documents, but I'm ALWAYS getting this, not rarely) and still not getting location updates thereafter? It's a real device (Samsung Galaxy S3) without a SIM card, if it helps.

推荐答案

我使用FusedLocationProvide API在我的应用程序, 这是我的code,关于设备和仿真器是双向 -

I am using FusedLocationProvide api in my app, Here is my code that works both on device and emulator -

  @Override
  protected void onCreate(Bundle savedInstanceState){
     //put your code here
     ....
     getLocation();

  }

 private void getLocation(){
    locationRequest = LocationRequest.create();
    locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
    locationRequest.setInterval(LOCATION_INTERVAL);
    locationRequest.setFastestInterval(LOCATION_INTERVAL);
    fusedLocationProviderApi = LocationServices.FusedLocationApi;
    googleApiClient = new GoogleApiClient.Builder(this)
    .addApi(LocationServices.API)
    .addConnectionCallbacks(this)
    .addOnConnectionFailedListener(this)
    .build();
    if (googleApiClient != null) {
        googleApiClient.connect();
    }
}


   @Override
   public void onConnected(Bundle arg0) {
        fusedLocationProviderApi.requestLocationUpdates(googleApiClient,  locationRequest, this);
   }

   @Override
   public void onLocationChanged(Location location) {
        Toast.makeText(mContext, "location :"+location.getLatitude()+" , "+location.getLongitude(), Toast.LENGTH_SHORT).show();
    }

我这是工作code。

this is working code of mine.

希望我的code帮你。

Hope my code help you.

干杯。

这篇关于为什么FusedLocationApi.getLastLocation空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆