user12000870
user12000870

Reputation:

How to zoom in on the actual location of the device?

I can not zoom in the current location of the device, could someone help me? any help is welcome.

override fun onMapReady(googleMap: GoogleMap) {
    Log.i("MAP READY", "READY")
    val position = if (currentLocation != null) LatLng(currentLocation!!.latitude, currentLocation!!.longitude) else null
    this.map = googleMap
    
    getFineLocationPermission()
    this.map!!.setOnMarkerClickListener(this)
    this.map!!.uiSettings.isRotateGesturesEnabled = true
    this.map!!.uiSettings.isZoomGesturesEnabled = true
    this.map!!.setOnInfoWindowClickListener(this)
    this.map!!.setOnMapLongClickListener(this)


}

enter image description here

Upvotes: 1

Views: 1393

Answers (2)

Manoj Perumarath
Manoj Perumarath

Reputation: 10234

Better you can optimize your code

override fun onMapReady(googleMap: GoogleMap) {
Log.i("MAP READY", "READY")
val position = if (currentLocation != null) LatLng(currentLocation!!.latitude, 
currentLocation!!.longitude) else null
this.map = googleMap

getFineLocationPermission()
this.map?.let{
it.setOnMarkerClickListener(this)
it.uiSettings.isRotateGesturesEnabled = true
it.uiSettings.isZoomGesturesEnabled = true
it.setOnInfoWindowClickListener(this)
it.setOnMapLongClickListener(this)
//zooming into the map
it.moveCamera(CameraUpdateFactory.newLatLngZoom(currentLocation,zoomLevel)) 
}

Upvotes: 0

annkylah
annkylah

Reputation: 467

Generally, you can change a map's zoom level by changing its camera position. According to this documentation:

To change the position of the camera, you must specify where you want to move the camera, using a CameraUpdate. The Maps API allows you to create many different types of CameraUpdate using CameraUpdateFactory.

Since you did not provide your full code, let me insert samples below:

  • If you want to keep all other maps properties to be the same and just change the zoom level, you can use this line:

    //where zoomLevel is your preferred zoom level
    mMap.moveCamera(CameraUpdateFactory.zoomTo(zoomLevel)) 
    
  • If you want to zoom a map on the location where the marker was placed, then below your addMarker() function, you can add this line to zoom your map on a City level:

    // zoomLevel is set at 10 for a City-level view
    val zoomLevel = 10
    // latLng contains the coordinates where the marker is added
    mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng,zoomLevel)) 
    

    Fun tip: You can animate the camera view update if you use animateCamera() instead of using moveCamera()

For more information on Maps SDK for Android - Camera and View settings, please see https://developers.google.com/maps/documentation/android-sdk/views

Upvotes: 2

Related Questions