moagrius / TileView

TileView is a subclass of android.view.ViewGroup that asynchronously displays, pans and zooms tile-based images. Plugins are available for features like markers, hotspots, and path drawing.
MIT License
1.46k stars 337 forks source link

Markers and map out of sync with the zooming #36

Closed tekblom closed 10 years ago

tekblom commented 10 years ago

Hi!

I have a problem when I am changing the LayoutParams for a view in a Callout that is visible. After I have updated the LayoutParams the map and the markers get out of sync with the zooming.

Sometimes markers and map are connected but goes against the topleft corner instead of zooming, other times maps and markers are not in sync and goes in different directions.

Do you have a clue of what this could be?

Best regards

Tobias

tekblom commented 10 years ago

This is run from a none UI thread, maybe that has something to do with it?

moagrius commented 10 years ago

I don't think the threading is relevant. I suspect it's just that the layout mechanism used by markers and callouts is unfamiliar to you. You can check out TranslationLayout and AnchorLayout for details on how exactly markers are laid out, but the basic idea is that they're positioned based on the full-size map (whatever size you set with TileView.setSize, and those numbers are "scaled" so that they move with the tileview as it scales, but only positionally (not dimensionally). There's also anchor points to condiser, as is described here and here

Really everything significant is happening in TranslationLayout.onLayout, here - that should show you what's going on in broad strokes. If you're messing with x or y, then make sure that the values you're setting are relative to the full size image. If you're using width and height, refer to how anchors work.

Hope the helps - post back if not.

tekblom commented 10 years ago

Thanks for your answer.

Though I cannot cast my LayoutParams to a AnchorLayout.LayoutParams. This is because my View is contained inside an RelativeLayout. This is my code before the change that causes it to get out of sync when zooming.

My Callout view:

<FrameLayout ...>
...
<RelativeLayout ..>
             <ImageView  android:id="@+id/car_callout_health_bar"
                                   ../>
</RelativeLayout>
...
</FrameLayout>

My Callout.java:

updateCallout()  {
  ViewGroup.LayoutParams healthParams = findViewById(R.id.car_callout_health_bar).getLayoutParams();
  healthParams.width = (int) (getResources().getDimension(R.dimen.car_callout_total_bar_width) * car.health);
  findViewById(R.id.car_callout_health_bar).setLayoutParams(healthParams);
}

Shouldn't I be able to change the LayoutParams inside an MarkerView without AnchorLayout? E.g. I need the RelativeLayout to position my ImageView.

moagrius commented 10 years ago

sure, the Marker can be anything at all (I generally use RelativeLayout myself - available for review in the demo app). That said, the change in width will affect the total width of the parent (Marker/Callout), which will affect how the anchor value is calculated. Do you understand how the anchor float values work? Have you tried a 0,0 anchor point (so the marker will have it's top-left corner aligned with the pixel position provided).

I'm still not 100% clear on what's happening - can you describe in detail the issue you're experiencing? If you'd like to share your app, I'd be happy to take a look.

tekblom commented 10 years ago

Just to let you know. I have recreated my views in the demo app and I don't get the problem there. I will dig into it a bit more later this or the next week and find what is causing the error in my app.

moagrius commented 10 years ago

sounds good. good luck

Sikter commented 10 years ago

Hi moagrius, I don't want open new issue (I will if needed be :D ) for my small question thats kinda related to this one. You said markers are scaled positionally with TileView and I understand that, but not dimensionally. What's the most elegant way to scale markers dimensionally with TileView?

moagrius commented 10 years ago

Hi @Sikter,

There's no way out-of-the-box to do that. I'd probably recommend adding a new ScalingLayout to the TileView. If you can use pixel values (relative to the original/fullsize or largest tile set), it should be fairly easy to implement - if you need to use a coordinate system it could get a little trickier.

You could also have each marker do it's own scaling, and have it listen to scale changes and react appropriately. The basic operation is pretty simple to have a View/Group scale - the concept can be seen pretty quickly in the very short ScalingLayout class (I'm not suggesting you use the class necessarily, but it does show a simple way to scale a View/Group).

Post back (in a new issue, as these are substantively different ideas) if you need more help.

tekblom commented 10 years ago

I solved this one !

It was becuase I didn't updated the views on ui-thread.

Thanks for the help!

moagrius commented 10 years ago

Glad you got it worked out.