如果位置访问权限被拒绝,则移动摄像机



我在下面有这段代码 - 它基本上是谷歌地图应用程序,它在启动时询问位置权限,如果接受,则将相机移动到当前位置。我尝试了很多方法,但都不起作用 - 如果一个人拒绝位置许可,然后将相机移动到 xwz 坐标......现在它只是将整个地图居中,仅此而已。请帮忙!谢谢!

class MapsActivity : AppCompatActivity(), OnMapReadyCallback {
private lateinit var mMap: GoogleMap
private var latitude:Double=0.toDouble()
private var longitude:Double=0.toDouble()
private lateinit var mLastLocation: Location
private var mMarker: Marker? = null
//Location
lateinit var fusedLocationProviderClient: FusedLocationProviderClient
lateinit var locationRequest: LocationRequest
lateinit var locationCallback: LocationCallback
companion object{
private const val MY_PERMISSION_CODE: Int = 1000
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_maps)
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
val mapFragment = supportFragmentManager
.findFragmentById(R.id.map) as SupportMapFragment
mapFragment.getMapAsync(this)
//Request runtime permission
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (checkLocationPermission()) {
buildLocationRequest()
buildLocationCallBack()
fusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(this)
fusedLocationProviderClient.requestLocationUpdates(locationRequest, locationCallback, Looper.myLooper())
}
}else {
buildLocationRequest()
buildLocationCallBack()
fusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(this)
fusedLocationProviderClient.requestLocationUpdates(locationRequest, locationCallback, Looper.myLooper())
}
}
private fun buildLocationCallBack() {
locationCallback = object : LocationCallback(){
override fun onLocationResult(p0: LocationResult?) {
mLastLocation = p0!!.locations.get(p0!!.locations.size-1) //Get last location
if(mMarker != null){
mMarker!!.remove()
}
latitude = mLastLocation.latitude
longitude = mLastLocation.longitude
val latLng = LatLng(latitude,longitude)
val markerOptions = MarkerOptions().position(latLng).title("Your position").icon(bitmapDescriptorFromVector(this@MapsActivity, R.drawable.m1))
mMarker = mMap!!.addMarker(markerOptions)
//Move camera
mMap!!.moveCamera(CameraUpdateFactory.newLatLng(latLng))
mMap!!.animateCamera(CameraUpdateFactory.zoomTo(11f))
}
}
}
private fun buildLocationRequest() {
locationRequest = LocationRequest()
locationRequest.priority = LocationRequest.PRIORITY_HIGH_ACCURACY
locationRequest.interval = 5000
locationRequest.fastestInterval = 3000
locationRequest.smallestDisplacement = 10f
}
private fun checkLocationPermission() : Boolean {
if(ContextCompat.checkSelfPermission(this,android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED){
if(ActivityCompat.shouldShowRequestPermissionRationale(this, android.Manifest.permission.ACCESS_FINE_LOCATION))
ActivityCompat.requestPermissions(this, arrayOf(
android.Manifest.permission.ACCESS_FINE_LOCATION
), MY_PERMISSION_CODE)
else
ActivityCompat.requestPermissions(this, arrayOf(
android.Manifest.permission.ACCESS_FINE_LOCATION
), MY_PERMISSION_CODE)
return false
}
else
return true
}
//Override OnRequestPermissionResult
override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {
when (requestCode) {
MY_PERMISSION_CODE -> {
if(grantResults.size > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED){
if(ContextCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED)
if (checkLocationPermission()) {
buildLocationRequest()
buildLocationCallBack()
fusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(this)
fusedLocationProviderClient.requestLocationUpdates(locationRequest, locationCallback, Looper.myLooper())
mMap!!.isMyLocationEnabled=true
}
}
else {
Toast.makeText(this, "Permission Denied", Toast.LENGTH_SHORT).show()
}
}
}
}
override fun onStop() {
fusedLocationProviderClient.removeLocationUpdates(locationCallback)
super.onStop()
}
override fun onMapReady(googleMap: GoogleMap) {
mMap = googleMap
//Init google Play Services
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (ContextCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
mMap!!.isMyLocationEnabled = true
}
}
else
mMap!!.isMyLocationEnabled = true

//Enable Zoom control
mMap.uiSettings.isZoomControlsEnabled=true
//map style
try {
val success = googleMap.setMapStyle(
MapStyleOptions.loadRawResourceStyle(this, R.raw.style_json))
} catch (e: Resources.NotFoundException) {
Log.e("MapsActivity", "Can't find style. Error: ", e)
}
}
}

我认为您在onLocationResult中犯了错误.

您同时使用moveCameraanimateCamera

相反,您可以使用newLatLngZoom.喜欢

mMap!!.animateCamera(CameraUpdateFactory.newLatLngZoom(latLng , 11f))

最新更新