无法使用Android Studio从谷歌Map获取纬度和经度

kzipqqlq  于 2022-12-21  发布在  Android
关注(0)|答案(1)|浏览(173)

我正在写一个使用谷歌Map的Android应用程序,现在我的目标是从用户请求位置权限,然后检索他的坐标,根据谷歌搜索我发现以下选项:

locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationListener = location -> {
    lat = location.getLatitude();
    lon = location.getLongitude();
};

if(ContextCompat.checkSelfPermission(this,Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
    ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 1);
} else {
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,1,locationListener);
}

System.out.println("lon + lat"+ lat + " " + lon);

问题是它没有进入locationListener函数,我试着打印纬度和经度的结果,结果是0.0,0.0。
纬度和经度,我得到了0.0

pgvzfuti

pgvzfuti1#

在www.example.com中添加依赖项gradle.app
implementation 'com.google.android.gms:play-services-location:21.0.1'

Kotlin

您可以使用FusedLocationProviderClient获取用户的当前位置,如下所示

private lateinit var fusedLocationClient: FusedLocationProviderClient

在**onCreate()**方法中

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.id.activity_main)

    // Initialize object
    fusedLocationClient = LocationServices.getFusedLocationProviderClient(this)
    getCurrentLocation()
}

最后一个位置返回当前可用的最新历史位置。

private fun getCurrentLocation() {
    // Checking Location Permission
    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        resultLauncher.launch(Manifest.permission.ACCESS_FINE_LOCATION)
        return
    }
    fusedLocationClient.getLastLocation().addOnSuccessListener(this, OnSuccessListener { location: Location? ->
        if (location != null) {
            val lat = location.latitude
            val lon = location.longitude
        }
    })
}

private val resultLauncher = registerForActivityResult<String, Boolean>(RequestPermission()) { isGranted: Boolean ->
    if (isGranted) {
        getCurrentLocation()
    } else {
        Toast.makeText(this, "Location permission not granted", Toast.LENGTH_SHORT).show()
    }
}

java

private FusedLocationProviderClient fusedLocationClient;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState)
    setContentView(R.id.activity_main)

    fusedLocationClient = LocationServices.getFusedLocationProviderClient(this);
    getCurrentLocation();
}

private void getCurrentLocation() {
    // Checking Location Permission
    if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        resultLauncher.launch(android.Manifest.permission.ACCESS_FINE_LOCATION);
        return;
    }

    fusedLocationClient.getLastLocation().addOnSuccessListener(this, location -> {
        if (location != null) {
            double lat = location.getLatitude();
            double lon = location.getLongitude();
        }
    });
}

使用resultLauncher对象请求位置权限。

private final ActivityResultLauncher<String> resultLauncher = registerForActivityResult(new ActivityResultContracts.RequestPermission(), isGranted -> {
    if (isGranted) {
        getCurrentLocation();
    } else {
        Toast.makeText(this, "Location permission not granted", Toast.LENGTH_SHORT).show();
    }
});

相关问题