gpt4 book ai didi

机器人 : CurrentLocation always return null value

转载 作者:行者123 更新时间:2023-11-29 20:01:42 24 4
gpt4 key购买 nike

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

然后android studio在

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

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

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

    private GoogleApiClient mLocationclient;mLocationclient=new GoogleApiClient.Builder(this)                    .addApi(LocationServices.API)                    .addConnectionCallbacks(this)                    .addOnConnectionFailedListener(this)                    .build();            mLocationclient.connect();            Location currentLocation= LocationServices.FusedLocationApi.getLastLocation(mLocationclient);            if(currentLocation==null)                Toast.makeText(this,"could not connect",Toast.LENGTH_SHORT).show();            else {                LatLng ll=new LatLng(                        currentLocation.getLatitude(),                        currentLocation.getLongitude()                );                CameraUpdate update=CameraUpdateFactory.newLatLngZoom(ll,50);                mMap.animateCamera(update);            }

请帮助我。

最佳答案

实际上回答的答案很有帮助所以这就是我对这个问题所做的问题是因为我已经在 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);

关于机器人 : CurrentLocation always return null value,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36185695/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com