p-lr / MapView

A Fast, memory efficient Android library to display tiled maps, with support for markers, paths, and rotation.
Apache License 2.0
184 stars 38 forks source link

No tiles visible, ability to center? #24

Closed timhughes89 closed 3 years ago

timhughes89 commented 3 years ago

Hi,

I was reading one of your other threads and you mentioned the ability to centre the maps after the config - any chance you could share how to do this please?

Also when loading have to zoom for scroll to start becoming visible?

Here's my config setup

` val tileSize = 256 val config = MapViewConfiguration( levelCount = map.levelCount, fullWidth = map.fullWidth, fullHeight = map.fullHeight, tileSize = tileSize, tileStreamProvider = tileStreamProvider ).apply { setMaxScale(2f) setMinimumScaleMode(MinimumScaleMode.FILL) setPadding(12800) }

    binding.mapview.configure(config)
    binding.mapview.defineBounds(0.0, 0.0, 1.0, 1.0)

    Timer().schedule(object : TimerTask() {
        override fun run() {
            if (binding.mapview.width != 0 && binding.mapview.height != 0) {
                this.cancel()
                binding.mapview.scrollTo( binding.mapview.width / 2, binding.mapview.height / 2)
            }
        }
    }, 1, 5)`
p-lr commented 3 years ago

Hi,

The MapView configuration should be done inside onCreateView(). You can find examples in the demo package. Like this one. That should solve the tile visibility issue. Although I also suspect your padding setting to have something to do with that. Usually, the padding is a multiple of tileSize (like one or two). The padding is the number in pixels which is virtually added to the visible part on the screen to compute the tiles that should be fetched. A huge padding like that will trigger way too much tiles to be loaded and cause memory strain on your device.

As for centering, you can do the following in onStart():

post {
   if (binding.mapview.width != 0 && binding.mapview.height != 0) {
          this.cancel()
          binding.mapview.scrollToAndCenter( binding.mapview.width / 2, binding.mapview.height / 2)
   }
}

I also replaced scrollTo with scrollToAndCenter().