gpt4 book ai didi

java - locationManager.getLastKnownLocation 请求后返回 null

转载 作者:行者123 更新时间:2023-12-02 01:49:36 33 4
gpt4 key购买 nike

我尝试通过 GPS 获取当前位置并显示它。

首先,我初始化 LocationManager,因此我总是使用方法 getLocation()

public class MainActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener, LocationListener {

private static final int REQUEST_LOCATION = 1;
private static int MY_PERMISSIONS_REQUEST_ACCESS;

TextView locationText;

LocationManager locationManager;
String lattitude,longitude;

@Override
protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

Button buttonLocate = (Button) findViewById(R.id.buttonLocate);

locationText = (TextView)findViewById(R.id.locationText);

ActivityCompat.requestPermissions(this, new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION}, REQUEST_LOCATION);

buttonLocate.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if(locationManager != null) {
if (!locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
// ..DO SMTH
} else if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
getLocation();
}

}
}
});

}

在方法getLocation()中,我正在检查权限,如果获得许可,我会请求位置更新并尝试获取纬度和经度,这是我的问题,因为位置是总是 null,所以我无法获取坐标! 代码可能有什么问题以及如何修复它?...

void getLocation() {

if (ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission
(MainActivity.this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {

ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, REQUEST_LOCATION);

} else {

// REQUEST!

locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 5000, 5, this);

Location location;


if (locationManager != null) {
location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

if (location != null) {
// LOCATION IS ALWAYS NULL!!!

double latti = location.getLatitude();
double longi = location.getLongitude();
lattitude = String.valueOf(latti);
longitude = String.valueOf(longi);

locationText.setText("Your current location is" + "\n" + "Lattitude = " + lattitude
+ "\n" + "Longitude = " + longitude);

} else {

Toast.makeText(this, "Unble to Trace your location", Toast.LENGTH_SHORT).show();

}
}
}

最佳答案

getLastKnownLocation返回设备的最后已知位置。如果没有缓存最后一个已知位置,它将返回 null。

getLastKnownLocation为空时,您需要请求位置更新。

为了获取位置更新,您必须实现 onLocationChanged :

public void onLocationChanged(Location location) {
// Called when a new location is found by the network location provider.
Log.i("Message: ","Location changed, " + location.getAccuracy() + " , " + location.getLatitude()+ "," + location.getLongitude());
}

关于java - locationManager.getLastKnownLocation 请求后返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53187799/

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