Repository navigation
[Android] Best practice to use MarkerViews? #5161
Description
Hi,
good job on the MarkerViews. I guess this will help me a lot and it already looks promising :)
However, I'm not quite sure yet what's be best or intended way to use it.
My first expectation was that I could use it in the very same way as the normal Markers, e.g.:
IconFactory iconFactory = IconFactory.getInstance(SimpleMapViewActivity.this);
Drawable iconDrawable = ContextCompat.getDrawable(SimpleMapViewActivity.this, R.drawable.purple_marker);
Icon icon = iconFactory.fromDrawable(iconDrawable);
mapboxMap.addMarker(new MarkerViewOptions()
.position(new LatLng(-33.8500000, 18.4158234))
.icon(icon)
.rotation(45)).setAlpha(0.5f);
But no marker was added to the map.
After taking a look into the TestApp I saw the use of Adapters. So I defined some XML view similar to the CountryMarkerView and and extended the TextAdapter class like
viewHolder.icon.setImageResource(R.drawable.arrow_black);
convertView.setAlpha(marker.getAlpha());
convertView.setRotation(marker.getRotation());
and after setting
final MarkerViewManager markerViewManager = mapboxMap.getMarkerViewManager();
markerViewManager.addMarkerViewAdapter(new TextAdapter(getApplicationContext()));
it finally worked. However, for my application I will need to remove/add new MarkerViews contiously so i set up a little test like
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
mapboxMap.removeAnnotations();
markerViewManager.invalidateViewMarkers();
markerViewManager.getMarkerViewAdapters().removeAll(markerViewManager.getMarkerViewAdapters());
handler.postDelayed(new Runnable() {
@Override
public void run() {
mapboxMap.addMarker(new MarkerViewOptions()
.position(new LatLng(-33.8500000, 18.4158234))
.rotation(45)).setAlpha(0.5f);
mapboxMap.addMarker(new MarkerViewOptions()
.position(new LatLng(-33.848546, 18.435047))
.rotation(90)).setAlpha(0.9f);
markerViewManager.addMarkerViewAdapter(new TextAdapter(getApplicationContext()));
}
}, 2000);
handler.postDelayed(this, 4000);
}
}, 4000);
Without
markerViewManager.invalidateViewMarkers(); markerViewManager.getMarkerViewAdapters().removeAll(markerViewManager.getMarkerViewAdapters());
and adding the Adapter afterwards again I did not have success to add the Markers again.
So in conclusion, what is the easiest way if you only want to add markers to the app that can be rotated and having different alpha level, like I thought it would be possible in the very beginning. Do I really have to set up XML Views and Adapter Classes or even write an Extended MarkerView class on my own? At least there is no default fallback I think. I ran my tests on a Nexus 5 with Android 6.0.1.
Activity
tobrun commentedon May 26, 2016
@Schumi09 thank you for providing feedback and iterating on this feature so quickly.
Currently the Icon field is pretty much ignored and we had to keep it around since
MarkerView extends Marker. I currently see 2 possibilities:Small note here is that alpha and rotation are automatically updated by the adapter itself (all the fields of Marker are linked to the MapView so it should automatically call invalidate). I initially though about implementing it in the way you are proposing but I wanted to make it as easy as possible for the end developer. eg. Calling
marker.setAlpha(0)will result in hiding the view with an alpha animation.I will need to look into attaching/removing the adapters and how to invalidate them correctly. What is the use case of adding/removing adapters? Small side note here is instead of adding/removing markers,you can also call marker.setVisible(true/false)?
Thanks again for providing this feedback, I will look into integrating what was mentioned above to make the ViewMarker API more clear and logical.
Schumi09 commentedon May 26, 2016
Thanks for your fast reply 👍
I'll consider the notes about alpha and rotation, didn't try it without it yet.
It has been the only way for me to remove the markers and add new ones afterwards.
I've already seen it. In my project/master thesis im dealing with different visualizations of off-screen landmarks. For this purpose I calculate locations that are on-screen. So the new locations depend on the current mapview/user location and the landmarks locations (angle, distance). Like this it's pretty easy to use classic map annotations as base for my visualizations that are anyway nothing else than markers, polygons or lines. Moreover, I can't update existing polylines or polygons, only markers. Therefore, in my current implementation I just replace the current visualization with a new one. If i wanted to implement a way to update the previous visualization it would be basically the same way like it currently is, the only difference would be updating markers only ;)
I see that this is not a usual use case but removing and adding new markers should work somehow.
But i think I'm going to use marker.setVisible(true/false) for on-screen landmarks, so far I also replace the markers because it was handy ;)
Schumi09 commentedon May 27, 2016
Little update:
So i actually found out that
addMarkers(); mMapboxMap.removeAnnotations(); addMarkers();works. addmarkers() contains the loop you've written in the TestApp to test TextMarkers and I used the TextAdapter and view_text_marker.
So the readded MarkerViews are not shown when you use Handler (that I only used to visually check whether the removing and adding actually works). In my application I'm not doing this anyway.
Log.d("Markers", mMapboxMap.getMarkers().size() + "" );provided the right amount of markers though, they were just not visible/rendered.
Schumi09 commentedon May 27, 2016
Next little update:
At start I do addMarkers() and set an onMyLocationChanged listener
when the location changes the markers disappear but are not added again. However, when I store a reference to a marker in order to move it it works.
I guess it should be possible to remove like all annotations on a triggered event and just add new ones ;) It seems a bit strange that it works when you just do add - remove - add but when there are Events or Handlers used it does not.
bleege commentedon May 27, 2016
@Schumi09 Thanks for the feedback. Can you confirm what version of the SDK the app is using? We just launched the Android
4.1.0-beta.1yesterday if you haven't seen that yet.Schumi09 commentedon May 27, 2016
Hi,
I've seen that you released a beta and I'm still on the Snapshot :) But there should be no difference I guess
tobrun commentedon Jun 2, 2016
One of the features of ViewAnnotations is that the following is already managed by the system:
When you pan a map and markers are no longer visible, the views will no longer be updated. So don't worry to much about adding them all at once and leaving them there (side note here is that you need to set the initial camera position first else you will try showing them all at once which will not work well).
Schumi09 commentedon Jun 2, 2016
Let me explain it better:

I got a map view that is obviously restricted by its bounding box. But i want to show indicators to the user that there are (global) landmarks around that are not in the bounding box. So i calculate positions within map view to place a marker for example that points to the off screen object. As I do it as experiment for navigation systems you are obv. always moving and that's why I recalculate everything when the location has changed (maybe using the locationtracking soon and then put it on the camerachange event). So removing and overwriting the visualizations is much more simple than providing ways to update them (It's like you also don't provide a way to update a polyline because it is not really needed)
For now I don't deal with a large set a of landmarks, so they are limited and preselected.
This is somer older screen of one of the visualizations:)
So the issue i experienced last week when i set up a basic demo app was:
If i remember correctly, I did not change/move the camera position with the handlers, but with the location updates. Tho i used setCameraPosition, already noticed the markers disappear while panning so moving the camera and setting up a MarkerView at once might cause troubles.
Schumi09 commentedon Jun 5, 2016
This is some code snippet that might help you reproducing it:
So when above is called I get
When I pan the map a little bit the markerview instantly shows up while panning. Maybe there is some issue with the animation of fading a markerview out earlier? (Is there/will there be a way to disable the animation anyway?). Doing the 'update' in the onCameraChanged event did not help either. I also tried delaying the adding with a Handler for some milliseconds, no success.
Recently, this also happened frequently to me with the above code:
06-05 17:51:31.464 9616-9616/ifgi.mapbox_playground E/AndroidRuntime: FATAL EXCEPTION: main Process: ifgi.mapbox_playground, PID: 9616 java.lang.IllegalStateException: Already in the pool! at android.support.v4.util.Pools$SimplePool.release(Pools.java:113) at com.mapbox.mapboxsdk.annotations.MarkerViewManager$1.onAnimationEnd(MarkerViewManager.java:213) at com.mapbox.mapboxsdk.utils.AnimatorUtils$3.onAnimationEnd(AnimatorUtils.java:79) at android.animation.ValueAnimator.endAnimation(ValueAnimator.java:1239) at android.animation.ValueAnimator$AnimationHandler.doAnimationFrame(ValueAnimator.java:766) at android.animation.ValueAnimator$AnimationHandler$1.run(ValueAnimator.java:801) at android.view.Choreographer$CallbackRecord.run(Choreographer.java:858) at android.view.Choreographer.doCallbacks(Choreographer.java:670) at android.view.Choreographer.doFrame(Choreographer.java:603) at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:844) at android.os.Handler.handleCallback(Handler.java:739) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:148) at android.app.ActivityThread.main(ActivityThread.java:5417) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)tobrun commentedon Jun 6, 2016
This issue comes from not invalidating the marker when adding a Marker. This is fixed in #5250.
Would you able to retest this when we release another beta build? I have been doing some fixes with this system in #5250. Would love to hear if this issue still exists.
Schumi09 commentedon Jun 6, 2016
Sounds great, can't wait for the new snapshot/beta 👍
So far the new MarkerView look pretty promising. I don't have to mess around with bitmap stuff anymore for setting alpha and rotation and it also looks like the issue about disappearing markers is fixed :) So the biggest issues for me are solved for me so far...
Yea, I'll try it then.
Schumi09 commentedon Jun 7, 2016
Hi @tobrun,
can you confirm that your changes from yesterday made it into the last snapshot (from yesterday evening) ? I deleted the cache files and also invalidated the cache in android studio so it got redownloaded. Because the options.roation() still takes an integer it looks like there is something missing.
bleege commentedon Jun 7, 2016
@Schumi09 It looks like the code that @tobrun was working on in #5076 / PR #5250 was merged into the
release-android-v4.1.0branch yesterday morning. We're using this branch to produce SNAPSHOTS and related builds for Android 4.1.0 right now and I created the last SNAPSHOT yesterday evening (Number 46) that should have this included for you. Getting the latest SNAPSHOT can be tricky sometimes, although your process sounds like it should have done this. Can you try the following on the command line to ensure that the latest SNAPSHOT downloads (it'll get logged in the shell as Gradle downloads the files)?https://github.com/mapbox/mapbox-gl-native/commits/release-android-v4.1.0
Schumi09 commentedon Jun 7, 2016
Thanks @bleege 👍
Usually I don't run into these issues. Although I have deleted the cache folder yesterday it just redownloaded the wrong snapshot again. But now it did the trick it seems :)
Unfortunately, it's already evening over here in germany so I can't provide you much feedback right now.
So first of all the invalidating seems fine.
However, the above exception is still thrown for one of my vizualizations. It's happening for the MarkerViews that all use the same Icon with new MarkerViewOptions tho.
Additionally, when pressing the device's home button I currently get
Looks like a new behaviour at least, at this point I cannot assure that it's not related to my code ;)
tobrun commentedon Jun 8, 2016
@Schumi09 thank you for that feedback. looking at the stacktrace I'm guessing this has to do with Activity destroy and at the same time still doing operations related to ViewMarkers. The solution for this is making MarkerAdapter Activity context aware.(in normal cases this was onPause/onResume but with the new multi window support in Android N. I would look into onStart/onStop instead)
Schumi09 commentedon Jun 8, 2016
@tobrun thanks for the feedback, I switched the testing device only some weeks ago and didn't focus on those things yet.
I made a mistake, so don't consider below
To point this out:
I set up several MarkerViews one after another via a For-Loop basically. They all share the same Icon that I have globally stored and the difference between the MarkerViews are position, rotation, alpha so I do new MarkerViewOptions for each. This already crashes the app at first execution of this code. I had another type of vizualization with MarkerViews running properly, but there I always build entirely new drawables that become icons for every execution.
Schumi09 commentedon Jun 8, 2016
Oh my bad, I missed that I have accidentally added the same MarkerView twice to the map that has caused the exception!
So far everything is running as expected which is amazing, so many new opportunities 👍
Schumi09 commentedon Jun 22, 2016
I think all my issues here are solved, so I closed the issue 👍
bleege commentedon Jun 27, 2016
Excellent! Glad things are working for you @Schumi09!