• Android在GoogleMap(百度地图)实现自定义指南针旋转与回正功能


    Android在GoogleMap(百度地图)实现自定义指南针旋转与回正功能

    关于

      因为项目需要,需要在googleMap上面自定义指南针,实现指南针实时根据地图方位变化转动,点击指南针方位回正两个功能。

    效果图

      可以参考高德地图导航开始后地图旋转的指南针效果。
      因为手机像素过高的原因,导致我没法一次上传上来,这个是旋转效果。
    在这里插入图片描述
      定位回正
    在这里插入图片描述

    实现(这里只讲googleMap)

      关于GoogleMap的引用可以先看这篇《Android使用GoogleMap实现定位及定位回正》
    我们新建一个activity,修改activity_google_maps.xml(compass的图标可以去阿里巴巴图标库等去找即可):

    
    <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context="com.stationdm.kawasaki.GoogleMapsActivity">
        <fragment
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:name="com.google.android.gms.maps.SupportMapFragment"
            android:id="@+id/map"/>
    
        <com.google.android.material.floatingactionbutton.FloatingActionButton
            android:id="@+id/fab_compass_location"
            android:layout_width="48dp"
            android:layout_height="48dp"
            android:src="@drawable/ic_map_compass_icon"
            app:fabCustomSize="48dp"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            android:layout_marginEnd="16dp"
            android:layout_marginTop="16dp" />
    
    androidx.constraintlayout.widget.ConstraintLayout>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25

      修改GoogleMapsActivity

    private var map:GoogleMap? = null
    
    override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            binding = ActivityGoogleMapsBinding.inflate(layoutInflater)
            setContentView(binding.root)
            val mapFragment : SupportMapFragment = supportFragmentManager.findFragmentById(R.id.map) as SupportMapFragment
            mapFragment.getMapAsync(this)
             //指南针回正
            binding.fabCompassLocation.setOnSingleClickListener {
                map?.cameraPosition?.target?.let {
                    val cameraPosition = CameraPosition.Builder()
                        .target(LatLng(it.latitude, it.longitude))
                        .zoom(map!!.cameraPosition.zoom)
                        .bearing(0f)
                        .tilt(map!!.cameraPosition.tilt)
                        .build()
                    map!!.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition))
                }
            }
        }
    
      @SuppressLint("MissingPermission")
        override fun onMapReady(googleMap: GoogleMap) {
            map = googleMap
            googleMap.mapType = GoogleMap.MAP_TYPE_HYBRID
            //关于定位权限在我这里就不做赘述了,这里假设定位以及成功了
            map?.setCompassEnabled(false)//这里可以打开看一下我们实际效果和自带的是否有差异
            //添加相机移动的监听和空闲监听用来作为控制指南针监听的开关
            map?.setOnCameraMoveStartedListener(this)
            map?.setOnCameraIdleListener(this)
            //页面继承方法
       }
      override fun onCameraMoveStarted(p0: Int) {
            when (p0) {
                GoogleMap.OnCameraMoveStartedListener.REASON_GESTURE -> {//手势引起的地图移动
                    openRotation(true)
                }
            }
        }
    
        override fun onCameraIdle() {
            openRotation(false)
        }
        
        //是否开启旋转
      private fun openRotation(isOpen: Boolean) {
            if (isOpen) {
                job?.cancel()
                job = lifecycleScope.launch {
                    delay(100) //测试发现延迟0.1ms效果比较流畅
                    map?.cameraPosition?.bearing?.let {
                        binding.fabCompassLocation.rotation = -it
                        openRotation(isOpen)
                    }
                }
            }
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58

      本篇到此就结束了,有问题欢迎批评指教,觉得不错的也请点个赞,谢谢

  • 相关阅读:
    精选大厂10道常考python面试题!
    css知识学习系列(9)-每天10个知识点
    .ttf 字体剔除
    箭头函数详解
    【机器学习课程】第三章特征工程 1.特征构造1.2 多变量特征构造(特征衍生)
    使用 Node.contains 判断元素是否为后代元素对 svg 元素无效解决方案
    Docker学习总结
    shell通配符与glob
    华为云服务器内网vpc对等连接及微服务内网集群搭建处理
    [最新榜单] 智能手机数据恢复的 10 款最佳应用
  • 原文地址:https://blog.csdn.net/Tobey_r1/article/details/126711728