gpt4 book ai didi

java - 无法获取位置坐标?经纬度

转载 作者:行者123 更新时间:2023-11-30 10:30:17 28 4
gpt4 key购买 nike

我对纬度和经度有疑问。当我想获取坐标时,只有 24 个 API 检索坐标。其他人没有。不知道为什么

我正在使用 GPSTracker 服务

public Location getLocation() {
try {
locationManager = (LocationManager) mContext
.getSystemService(LOCATION_SERVICE);

// 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
} else {
this.canGetLocation = true;
if (isNetworkEnabled) {
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {

}
locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
Log.d("Network", "Network");
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,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
Log.d("GPS Enabled", "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;
}

我正在通过这种方法将数据保存到数据库中:

private void getCoordinates() {
GPSTracker gpstracker = new GPSTracker(this);
if(gpstracker.canGetLocation)
{
double locationLng = gpstracker.getLongitude();
double locationLat = gpstracker.getLatitude();

databaseReference.child("lng").setValue(locationLng);
databaseReference.child("lat").setValue(locationLat);
}else
gpstracker.showSettingsAlert();
}

在这里你可以看到我的数据库,第一个用户已经被 24 Api 模拟器保存,第二个被 25 保存,其他的也不起作用:

db

最佳答案

您需要 23 和 25 api 的权限请求

将这个添加到类中

  private static final int PERMISSION_ACCESS_COARSE_LOCATION = 0;

if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_COARSE_LOCATION)
!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.ACCESS_COARSE_LOCATION},
PERMISSION_ACCESS_COARSE_LOCATION);
}

关于java - 无法获取位置坐标?经纬度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43733693/

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