Favolas
Favolas

Reputation: 7243

Move or animate map and marker synchronously

I have a list with a bunch of LatLng. I need to animate a marker from one position to the other until the list ends (finishing point).

I'm using an interpolator (something like this implementation from @CommonsWareenter link description here) to animate the marker from a position to the other. At the same time, I'm also creating a polyline with generated points.

With the above, I have a working solution. If setting the map to max zoom (21f) and even if the interpolator is producing results at a speed greater than 16ms, adding the polyline and the marker at the new position does not impact the UI, that is, as far as my eyes can see, it's very smooth. Of course, at some point, the marker goes out of the screen and I have to move the map with my finger to track the marker and polyline movement.

I now want the marker stay always at the centre of the screen thus the map must move.

I've tried to use map.moveCamera and map.animateCamera but then the UI sometimes gets a little sluggish. I can see that the marker does not move as smooth as before. This is using map.moveCamera because with map.animateCamera I have even worst results.

In the function that the ObjectAnimator calls everytime it interpolates the point, I'm doing this:

public void onNewPosition(LatLng newPosition){
    marker.setPostion(newPosition);
    map.moveCamera(marker.getPosition())
}

If I wrap the above in an if condition to check if the time elapsed from the previous call to this one is greater than 16ms, I still have some sluggish UI.

I've identified the problem in the map movement. Don't know if it's the matter that the UI cannot render the map movement and marker movement at the same time, thus, it seems that sometimes the marker moves and the map don't and other times is the other way around (don't know if I'm correct or not)

Any idea of how can I move the map and the marker at the same time or other solution that fulfils my needs, that is, move the marker and the map so that the user perception is that the marker is moving but staying at the centre of the screen while the map below performs the required movement?

Thanks.

Upvotes: 0

Views: 76

Answers (1)

Walter Oney
Walter Oney

Reputation: 177

Could you perhaps imbed your map in two scrolling objects: a HorizontalScrollView and a VerticalScrollView, so you can approximate your movement by a vertical and a horizontal scroll? Use a viewport for the map view that encompasses the endpoints of your trip. Scroll the map view when the focal position changes instead of calling moveCamera. The idea is to shift the redrawing overhead to the graphics engine (which ought to be very good at it) instead of the map API. [I used a similar view architecture to scroll a train schedule, and it worked very well.]

Upvotes: 2

Related Questions