How to move the Android Google Maps API Compass Position

前端 未结 7 1583
太阳男子
太阳男子 2020-12-05 13:57

Does anyone know if you can change the compass position within the map?

All I can find is how to enable or disable it. But I need to move it down a bit so my overlay

7条回答
  •  孤城傲影
    2020-12-05 14:44

    Based on the answer of @Vignon (https://stackoverflow.com/a/38266867/2350644), here is a Kotlin code snippet to position the compass icon to the top right of the screen. You can also add custom margins (in this example the compass image has a marginTop of 50dp).

    mapFragment?.view?.let { mapView ->
      mapView.findViewWithTag("GoogleMapMyLocationButton").parent?.let { parent ->
        val vg: ViewGroup = parent as ViewGroup
        vg.post {
          val mapCompass: View = parent.getChildAt(4)
          val rlp = RelativeLayout.LayoutParams(mapCompass.height, mapCompass.height)
          rlp.addRule(RelativeLayout.ALIGN_PARENT_LEFT, 0)
          rlp.addRule(RelativeLayout.ALIGN_PARENT_TOP)
          rlp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT)
          rlp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, 0)
    
          val topMargin = (50 * Resources.getSystem().displayMetrics.density).toInt()
          rlp.setMargins(0, topMargin, 0, 0)    
          mapCompass.layoutParams = rlp
        }
      }
     }
    

提交回复
热议问题