有没有办法根据加速度计设置 google 地图的方向?
Is there a way to set the direction of google maps according to the accelerometer?
我想知道是否有办法使用 Android 设备的加速度计来设置显示的 Google 地图的方向?
以下是我如何获得对加速度计的引用:
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"
}
我想知道是否有办法使用 Android 设备的加速度计来设置显示的 Google 地图的方向?
以下是我如何获得对加速度计的引用:
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"
}