Ibby1501
Ibby1501

Reputation: 41

Kotlin - Set zoom level google map Android Studio

I've added the Google Map API and added my markers, but when the app is run, it is zoomed out quite a lot. I tried researching but I can't find what I need. Can someone please tell me how to set the zoom level once the map is loaded? E.g, it should show only a city not the whole world.

Here's my fragment code

package com.example.myassignment.Fragments

import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.example.myassignment.R
import com.google.android.gms.maps.CameraUpdateFactory
import com.google.android.gms.maps.GoogleMap
import com.google.android.gms.maps.OnMapReadyCallback
import com.google.android.gms.maps.model.LatLng
import com.google.android.gms.maps.model.MarkerOptions
import kotlinx.android.synthetic.main.fragment_destinations.*

private const val ARG_PARAM1 = "param1"
private const val ARG_PARAM2 = "param2"

class DestinationsFragment : Fragment(), OnMapReadyCallback {

    private var param1: String? = null
    private var param2: String? = null
    private lateinit var googleMap: GoogleMap

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
        arguments?.let {
            param1 = it.getString(ARG_PARAM1)
            param2 = it.getString(ARG_PARAM2)
        }
        mapView.onCreate(savedInstanceState)
        mapView.onResume()

        mapView.getMapAsync(this)

    }
    override fun onMapReady(map: GoogleMap?) {
        map?.let {
            googleMap = it
        }
        val sydney = LatLng(-34.0, 151.0)
        val stokeOnTrent = LatLng(53.025780,-2.177390)
        val stokeCobrdige = LatLng(53.029380,-2.188740)
        
        // Here's my markers
        googleMap.addMarker(MarkerOptions().position(sydney).title("Marker in Sydney"))
        googleMap.addMarker(MarkerOptions().position(stokeOnTrent).title("Stoke Center"))
        googleMap.addMarker(MarkerOptions().position(stokeCobrdige).title("Cobridge - Stoke"))
        googleMap.minZoomLevel
        googleMap.moveCamera(CameraUpdateFactory.newLatLng(stokeOnTrent))
    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_destinations, container, false)
    }

    companion object {
        /**
         * Use this factory method to create a new instance of
         * this fragment using the provided parameters.
         *
         * @param param1 Parameter 1.
         * @param param2 Parameter 2.
         * @return A new instance of fragment DestinationsFragment.
         */
        // TODO: Rename and change types and number of parameters
        @JvmStatic
        fun newInstance(param1: String, param2: String) =
            DestinationsFragment().apply {
                arguments = Bundle().apply {
                    putString(ARG_PARAM1, param1)
                    putString(ARG_PARAM2, param2)
                }
            }
    }

}

Thank you!

Upvotes: 1

Views: 4381

Answers (1)

Javier
Javier

Reputation: 113

If you didn't resolve this already, you need to use the function newLatLngZoom() instead of newLatLng() and provide the zoom level - 10.

googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(stokeOnTrent, someZoomLevel))

More info can be found here, but it says:

val homeLatLng = LatLng(latitude, longitude)

val zoomLevel = 15f

The zoom level controls how zoomed in you are on the map. The following list gives you an idea of what level of detail each level of zoom shows:

1: World

5: Landmass/continent

10: City

15: Streets

20: Buildings

Move the camera to homeLatLng by calling the moveCamera() function on the map object and pass in a CameraUpdate object using CameraUpdateFactory.newLatLngZoom(). Pass in the homeLatLng object and the zoomLevel.

map.moveCamera(CameraUpdateFactory.newLatLngZoom(homeLatLng, zoomLevel))

Upvotes: 8

Related Questions