Rotate marker as per user direction on Google Maps V2 Android

前端 未结 3 581
情书的邮戳
情书的邮戳 2020-12-07 23:20

I want to rotate marker as per bearing or sensor value received from Accelerometer to show the user where actually he is moving. I have set marker icon and flat value to tru

3条回答
  •  庸人自扰
    2020-12-07 23:58

    In Kotlin by using Google SphericalUtil class we can get bearing by passing source and destination LatLngs like:

    fun calculateBearing(lat1: Double, lng1: Double, lat2: Double, lng2: Double): Float {
            val sourceLatLng = LatLng(lat1, lng1)
            val destinationLatLng = LatLng(lat2, lng2)
            return SphericalUtil.computeHeading(sourceLatLng, destinationLatLng).toFloat()
        }
    

    Then set this result 'bearing` to the marker like

    Val bearing  = calculateBearing(lat1, lng1, lat2, lng2)
    marker.rotation(bearing)
    

    Reference: https://developers.google.com/maps/documentation/android-sdk/utility/#spherical

提交回复
热议问题