首页 > 解决方案 > 样式化 google 在 Android 中映射我的位置按钮

问题描述

所以我试图使用浮动操作按钮而不是默认的谷歌地图的我的位置按钮。关于我该怎么做的任何想法?我尝试了很多方法,我尝试用true 或 false 调用MyLocationButtonClick()我内部的事件,但它没有缩放用户位置。欢迎任何帮助!谢谢你的时间!FloatingActionButtonmap?.uiSettings?.isMyLocationButtonEnabled

这是我的带有地图的 xml,以及我想为谷歌地图替换我的位置按钮的按钮。

<LinearLayout android:layout_height="match_parent"
    android:layout_width="match_parent"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <com.google.android.material.floatingactionbutton.FloatingActionButton
            android:id="@+id/btn_myLocation"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:elevation="8dp"
            android:backgroundTint="#fff"
            app:borderWidth="0dp"
            android:src="@drawable/ic_round_my_location_24"
            android:layout_margin="10dp"
            android:layout_alignParentEnd="true"
            android:layout_alignParentTop="true"/>

        <fragment
            android:id="@+id/map"
            class="com.google.android.gms.maps.SupportMapFragment"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>
    </RelativeLayout>

</LinearLayout>

这是我的 kotlinonCreate()和我的浮动按钮点击监听器

val view : View = inflater.inflate(R.layout.fragment_courier_map, container, false)

// starts google places client
Places.initialize(this.requireContext(), getString(R.string.google_maps_key))
placesClient = Places.createClient(requireContext())

// starts fused location
fusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(requireContext())

view.btn_myLocation.setOnClickListener {
    map!!.setOnMyLocationButtonClickListener  {
         false
    }
}

val mapFragment = childFragmentManager.findFragmentById(R.id.map) as SupportMapFragment?
mapFragment?.getMapAsync(this)
return view

标签: androidgoogle-mapskotlin

解决方案


您需要按如下方式修改地图的相机,

map.moveCamera(
   CameraUpdateFactory.newLatLngZoom(location, DEFAULT_ZOOM.toFloat()))

放大用户位置。阅读本文了解更多详情


推荐阅读