gpt4 book ai didi

android - 即使打开 GPS,位置也为空

转载 作者:行者123 更新时间:2023-11-30 00:34:44 25 4
gpt4 key购买 nike

我正在研究谷歌地图。我想获取用户位置并在 map 上显示他。我写了一个 Locationlistener 来获取用户位置。

 public Location getUserLocation(){

try {
// getting GPS status
isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
// getting network status
isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (!isGPSEnabled && !isNetworkEnabled) {
// no network provider is enabled

return null;
} else {
this.canGetLocation = true;
if (isNetworkEnabled) {
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,DISTANCE_CALC,TIME_UPDATE, this);
Log.d("Network", "Network Enabled");
if (locationManager != null) {
location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
}
// if GPS Enabled get lat/long using GPS Services
if (isGPSEnabled) {
if (location == null) {
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,DISTANCE_CALC,TIME_UPDATE, this);
Log.d("GPS", "GPS Enabled");
if (locationManager != null) {
location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
}
}
}

} catch (Exception e) {
e.printStackTrace();
}

return location;
}

getUserLocation()LocationListener 中的函数之一,它将通过 GPS 或网络返回用户位置。我正在从这样的 fragment 中调用此函数。

if (Build.VERSION.SDK_INT >= 23){
if (checkPermission()) {
location = locHandler.getUserLocation();
if(location!=null)
showTheMaps(location);
else
AlertBuilder(title,body);
}else {
requestPermission();
}
}else {
//The build is less than marshmellow so no need for permission
location = locHandler.getUserLocation();
try {
// double d = location.getLatitude();
showTheMaps(location);
}catch (NullPointerException e){
AlertBuilder(title, body);
}
}


public void showTheMaps(Location location){

CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(new LatLng(location.getLatitude(),location.getLongitude()), DataModel.zoomlevel);
googleMap.animateCamera(cameraUpdate);
}

AlertBuilder 函数将显示一个带有按钮的警告框,单击该按钮可关闭应用程序。

正如您从屏幕截图中看到的那样,GPS 已启用,但返回给我的位置为空,应用程序在显示警报对话框后关闭。 enter image description here可能是什么问题呢?为什么打开 GPS 后位置返回 null?

最佳答案

使用

Location currentLocation = LocationServices.FusedLocationApi.getLastLocation(googleApiClient);

来源:https://developer.android.com/training/location/retrieve-current.html

另一种(已弃用)方法是在 map 上设置一个 OnMyLocationChangeListener 以获取位置

mMap.setOnMyLocationChangeListener(new GoogleMap.OnMyLocationChangeListener() {
@Override
public void onMyLocationChange(Location location) {
// do something with location
}
});

更多信息:how to get current location in google map android

关于android - 即使打开 GPS,位置也为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43609449/

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