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

Setting initial angle and center #18

Closed c0dd3vi11 closed 4 years ago

c0dd3vi11 commented 4 years ago

@peterLaurence, could you help me again, please?

I got that I can use MapViewConfiguration.setStartScale to initiate the scale. But how to initiate angle and center for map properly?

p.s. I'm storing angle, scale & center between launching and also want a feature to open map positioned properly on the certain map's zone.

p-lr commented 4 years ago

After the MapView has been laid out, you can do like so:

with(mapView) {
    angle = 15f
    val centerX = scrollX + halfWidth
    val centerY = scrollY + halfHeight
    post {
        scrollToAndCenter(centerX, centerY)
    }
}
c0dd3vi11 commented 4 years ago

I'm trying to apply ReferentialData:

with(mapView) {
    post {
        scale = data.scale
        setAngle(data.angle)
        scrollToAndCenter(data.centerX.toInt(), data.centerY.toInt())
    }
}

It rotates, scales, but not scrolls...

c0dd3vi11 commented 4 years ago

And it seems that it also moves to the top-left corner

p-lr commented 4 years ago

I would check the values you have for data.centerX and data.centerY. Typically, if those values are too small, it scrolls to the top left corner.

c0dd3vi11 commented 4 years ago

I found out that in:

override fun scrollTo(x: Int, y: Int) {
    val (constrainedX, constrainedY) = gestureController.setConstrainedScroll(x, y)
    super.scrollTo(constrainedX, constrainedY)
}

I got zeros in constrainedX, constrainedY.

p-lr commented 4 years ago

Can you also explain when you're trying to restore the state of MapView? Is it on device rotation (and activity re-create?)

c0dd3vi11 commented 4 years ago

Ah, yes, the are small. Less than 1. It seems that they are between 0 an 1

c0dd3vi11 commented 4 years ago

I'm trying to restore ReferentialData just after configure.

c0dd3vi11 commented 4 years ago

I'm just trying to save it and restore to open map on a previous location.

p-lr commented 4 years ago

Do you know that the MapView has an automatic feature of state restore (angle, scale and scroll) on device rotation?

c0dd3vi11 commented 4 years ago

I'm trying to share the ReferentialData between three MapView's to save the cache. To able fast switching between maps without reloading.

c0dd3vi11 commented 4 years ago
Screenshot 2020-06-01 at 17 09 35
p-lr commented 4 years ago

If you look at the doc of ReferentialData:

ref-data

You can see that those values you have are in percent of the full width/height of your map.

c0dd3vi11 commented 4 years ago

Oh... ^_^ That's my fault...

c0dd3vi11 commented 4 years ago

I can't understand how to calculate real center with that percentage. Could you help me please?

c0dd3vi11 commented 4 years ago
Screenshot 2020-06-01 at 18 12 54

That works incorrect...

p-lr commented 4 years ago

You shouldn't use width and height, which might be 0 at the time you call your function. Instead, use the fullWidth and fullHeight you set at the configuration of the MapView.

Le lun. 1 juin 2020 à 17:13, c0dd3vi11 notifications@github.com a écrit :

[image: Screenshot 2020-06-01 at 18 12 54] https://user-images.githubusercontent.com/60510579/83423365-87643480-a433-11ea-9458-ff8d6ed6d117.png That works incorrect...

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/peterLaurence/MapView/issues/18#issuecomment-636915279, or unsubscribe https://github.com/notifications/unsubscribe-auth/ADXKCCXPAKI74BA5OKIKNYTRUPARXANCNFSM4NPYMH2Q .

c0dd3vi11 commented 4 years ago

Oh... this is so unobvious. I tried fullWidth/fullHeight and got the opposite situation, when viewport sticks to the right-bottom.

c0dd3vi11 commented 4 years ago

I did the combo! ^_^

fun MapView.applyReferentialData(config: MapViewConfiguration, data: ReferentialData) {
    post {
        scale = data.scale
        setAngle(data.angle)
        val centerX = config.fullWidth * data.scale * data.centerX
        val centerY = config.fullHeight * data.scale * data.centerY
        scrollToAndCenter(centerX.toInt(), centerY.toInt())
    }
}