【问题标题】:android : CurrentLocation always return null valueandroid:CurrentLocation 总是返回空值
【发布时间】:2016-07-11 04:30:52
【问题描述】:

我尝试使用 android studio 获取当前位置,但变量 currentLocation 始终为空值

和android studio在

下画红线 位置 currentLocation= LocationServices.FusedLocationApi.getLastLocation(mLocationclient);

并要求检查权限 我尝试了两种检查权限和不检查的方法 但同样的结果返回空值

请问谁能帮帮我? 这是我使用的代码

私有 GoogleApiClient mLLocationclient; mLLocationclient=new GoogleApiClient.Builder(this) .addApi(LocationServices.API) .addConnectionCallbacks(这个) .addOnConnectionFailedListener(这个) 。建造(); mLLocationclient.connect(); 位置 currentLocation= LocationServices.FusedLocationApi.getLastLocation(mLocationclient); 如果(当前位置==空) Toast.makeText(this,"无法连接",Toast.LENGTH_SHORT).show(); 别的 { LatLng ll=新的 LatLng( currentLocation.getLatitude(), currentLocation.getLongitude() ); CameraUpdate 更新=CameraUpdateFactory.newLatLngZoom(ll,50); mMap.animateCamera(更新); }

请帮帮我。

【问题讨论】:

  • android studio 在下画红线 -- 显示什么错误?
  • 我认为这不是错误。但它只是请求检查权限
  • 你能发布你的清单文件吗?

标签: android


【解决方案1】:

实际上回答的答案非常有帮助 所以这就是我对这个问题所做的 问题是因为我已经在 onCreate() 实现的方法中编写了转到当前位置的完整代码 进入这个位置,getLastLocation 为空 我做了什么 我刚刚写了连接

private GoogleApiClient mLocationclient;            

mLocationclient = new GoogleApiClient.Builder(this)
                    .addApi(LocationServices.API)
                    .addConnectionCallbacks(this)
                    .addOnConnectionFailedListener(this)
                    .build();
            mLocationclient.connect();

然后我去当前位置监听按钮clickListener 就像这段代码我已经编码到 clickListener 中

LatLng ll=null;
    Location currentLocation= LocationServices.FusedLocationApi.getLastLocation(mlocationclient);
    if(currentLocation==null)
        Toast.makeText(context,"could not connect",Toast.LENGTH_SHORT).show();
    else {
            ll=new LatLng(
                currentLocation.getLatitude(),
                currentLocation.getLongitude()
        );
    }



 CameraUpdate update = CameraUpdateFactory.newLatLngZoom(ll, 17);


mMap.moveCamera(update);

【讨论】:

    【解决方案2】:

    您获得 null 的原因有多种。最有可能的是,它没有以前的位置可供拉取。请记住,getLastLocation 实际上并没有出去获取新的位置。它只是抓住你手机的最后一个。您的手机将从各种来源/方法中获取位置数据,这就是正在使用的数据。根据我的经验,它通常长达几分钟。

    我愿意:

    1) 检查您没有使用 sim 2) 检查您的 GPS 是否在您的手机上工作(谷歌地图是否工作?) 3) 尝试强制更新位置(LocationManager.requestLocationUpdate(...) as seen here)并从那里调试。

    我相信只要您还支持 SDK

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-11-17
      • 1970-01-01
      • 1970-01-01
      • 2012-11-12
      • 2014-06-24
      • 2019-12-05
      • 1970-01-01
      相关资源
      最近更新 更多