Home > Software engineering >  Android app: Google map has strange behavior when setting map bounds
Android app: Google map has strange behavior when setting map bounds

Time:11-20

so I want to build an android app with a fragment that displays google map. only the part of the map within those two bounds in hamburg should be displayed when the fragment is opened :LatLng((53.394655), 10.099891) and LatLng((53.694865), 9.757589). This is the fragment code:

 class MapFragment : Fragment(), OnMapReadyCallback {
        private lateinit var map: GoogleMap
    
        override fun onCreateView(
            inflater: LayoutInflater, container: ViewGroup?,
            savedInstanceState: Bundle?
        ): View? {
            // Inflate the layout for this fragment
            val binding: FragmentMapBinding =
                DataBindingUtil.inflate(inflater, R.layout.fragment_map, container, false)
             
            binding.setLifecycleOwner(this)
            // Get the SupportMapFragment and request notification when the map is ready to be used.
            val mapFragment =
                getChildFragmentManager().findFragmentById(R.id.map) as? SupportMapFragment
            mapFragment?.getMapAsync(this)
            return binding.root
        }
    
        override fun onMapReady(googleMap: GoogleMap) {
            map = googleMap
            zoomToHamburgBounds()
        }
    
        private fun zoomToHamburgBounds() {
            val hamburgBounds =LatLngBounds(
                LatLng((53.394655), 10.099891) , // SW bounds
             LatLng((53.694865), 9.757589) )// NE bounds
            map.moveCamera(CameraUpdateFactory.newLatLngBounds(hamburgBounds,10))
        }
    }

When i run the app, the map focuses on some place near canada and has a strange behavior when zooming in or out. I could not figure out the problem, the connection with google map api is successsful and I added the dependency and the manifest tag. I would appreciate any help

Thank you

CodePudding user response:

I'm struggling to see exactly what the problem is, as it appears you're doing everything correctly.

However, you failed to attach a LogCat which may have helped diagnose the issue, I've spent some time trying to repro it and the message I received was

Map size can’t be 0. Most likely, layout has not yet occured for the map view. Either wait until layout has occurred or use newLatLngBounds(LatLngBounds, int, int, int) which allows you to specify the map’s dimensions.

This lead me to believing it was a lifecycle issue and you need to wait for the map to be ready using

map.setOnMapLoadedCallback {
   zoomToHamburgBounds()
}

I'm not sure why the docs here haven't been updated in so long, but I suspect they are part of the issue: https://developers.google.com/maps/documentation/android-sdk/start#look_at_the_code

I've pushed a repo to Github for you to use as reference. Try pulling it and setting your API keys to verify the solution.

https://github.com/LDuncAndroid/Google-Maps-Bounds

Edit: In fact, coming back to this, I believe that the callback solution AND using LatLngBounds.Builder() is the complete solution`. I don't believe we can view the source of that class but there's clearly something being done that isn't when using the constructor.

  • Related