简体   繁体   English

在 Android 中设置谷歌地图我的位置按钮

[英]Styling google maps my location button in Android

So I am trying to use a floating action button instead of the default google map's my location button.所以我尝试使用浮动操作按钮而不是默认的谷歌地图的我的位置按钮。 Any ideas of how can I do that?关于我该怎么做的任何想法? I've tried many ways, I tried to call the MyLocationButtonClick() event inside my FloatingActionButton with either the map?.uiSettings?.isMyLocationButtonEnabled true or false but it did not zoom the user position.我尝试了很多方法,我尝试使用map?.uiSettings?.isMyLocationButtonEnabled true 或 false 在我的FloatingActionButton中调用MyLocationButtonClick()事件,但它没有缩放用户 position。 Any help is welcome!欢迎任何帮助! Thanks for your time!谢谢你的时间!

This is my xml with the map and the button I want to replace for the google maps my location button.这是我的 xml 和 map 和我想为谷歌地图替换的按钮我的位置按钮。

<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>

And here is my kotlin onCreate() with my floating button click listener这是我的 kotlin onCreate()和我的浮动按钮点击监听器

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

You need to modify the map's camera as follows,您需要按如下方式修改地图的相机,

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

to zoom into user position.放大用户 position。 Read this for more details阅读本文了解更多详情

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM