我想知道是否有一种方法可以使用Android设备的加速度计来设置显示的Google地图的方向?
以下是我如何获得Accelerometer的参考:
val sensorManager = getSystemService(Context.SENSOR_SERVICE) as SensorManager
val sensorAccelerometer = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER)
if (sensorAccelerometer != null) sensorManager.registerListener(this, sensorAccelerometer, SensorManager.SENSOR_DELAY_NORMAL)
override fun onSensorChanged(event: SensorEvent?) {
if (event?.sensor?.type == Sensor.TYPE_ACCELEROMETER){
Log.d(TAG, "onSensorChanger ACCELEROMETER")
val accelX = event.values[0]
val accelY = event.values[1]
val accelZ = event.values[2]
textViewAdView.text = "aX=${accelX.toString()}\r\naY=${accelY.toString()}\r\naZ=${accelZ.toString()}"
}
}
谢谢 !
您可以使用googleMap.setMyLocationEnabled(true)显示用户的当前位置和移动方向;
但是如果你想要翻转整个地图,你可以使用updateCameraBearing()
:
我已经将此功能与以下Kotlin代码一起使用:
override fun onMapReady(googleMap: GoogleMap) {
mMap = googleMap
mMap.uiSettings.isCompassEnabled = true
mMap.uiSettings.isMapToolbarEnabled = true
mMap.uiSettings.isMyLocationButtonEnabled = true
try {
mMap.isMyLocationEnabled = true
}catch (e: SecurityException) {
}
}
我会检查轴承的东西,谢谢
我会在外面尝试这个:
从fusedLocationClient获取新位置时:
fusedLocationClient = LocationServices.getFusedLocationProviderClient(this)
...
updateCamera(location.bearing)
....
private fun updateCamera(bearing : Float) {
val oldPos = mMap.getCameraPosition()
val pos = CameraPosition.builder(oldPos).bearing(bearing).build()
mMap.animateCamera(CameraUpdateFactory.newCameraPosition(pos))
textViewAdView.text = "Bearing=$bearing"
}