Reputation: 3
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.
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.
<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
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
Upvotes: 0
Views: 409
Reputation: 1574
You need to modify the map's camera as follows,
map.moveCamera(
CameraUpdateFactory.newLatLngZoom(location, DEFAULT_ZOOM.toFloat()))
to zoom into user position. Read this for more details
Upvotes: 1