android - 如果位置访问权限被拒绝,则将相机移动到
问题描述
我在下面有这段代码——它基本上是谷歌地图应用程序,它在启动时询问位置许可,如果被接受,则将相机移动到当前位置。我尝试了很多东西,但都不起作用——如果一个人拒绝定位,那么将相机移动到 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
。
您正在使用moveCamera
和animateCamera
同时。
相反,您可以使用newLatLngZoom
. 喜欢,
mMap!!.animateCamera(CameraUpdateFactory.newLatLngZoom(latLng , 11f))
推荐阅读
- angular - 无法读取未定义的属性“dtInstance”
- node.js - Passport Js自定义回调函数不调用下一个中间件
- javascript - 使用文件类型输入时将控制更改为未控制
- awk - awk:对列的子集执行算术并打印具有修改值的所有列
- python-3.x - 如何从字符串中删除某些字母?
- python - 如何为以 os.system() 或其他 python 函数启动的(fortran)程序提供输入?
- robotframework - 机器人框架 FOR 循环
- javascript - 使用 PHP 下载 CSV 文件
- python - How to manage Modbus RS485 connection from a python script?
- python - Nested Loops Trouble