-
Notifications
You must be signed in to change notification settings - Fork 1.3k
[Android] Best practice to use MarkerViews? #5161
Comments
@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
viewHolder.icon.setImageResource(R.drawable.arrow_black);
convertView.setAlpha(marker.getAlpha());
convertView.setRotation(marker.getRotation()); 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
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. |
Thanks for your fast reply 👍
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 ;) |
Little update:
|
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. |
@Schumi09 Thanks for the feedback. Can you confirm what version of the SDK the app is using? We just launched the Android |
Hi, |
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). |
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: |
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. |
Sounds great, can't wait for the new snapshot/beta 👍
Yea, I'll try it then. |
Hi @tobrun, |
@Schumi09 It looks like the code that @tobrun was working on in #5076 / PR #5250 was merged into the ./gradlew clean --refresh-dependencies https://github.com/mapbox/mapbox-gl-native/commits/release-android-v4.1.0 |
Thanks @bleege 👍 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 ;) |
@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) |
@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
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. |
Oh my bad, I missed that I have accidentally added the same MarkerView twice to the map that has caused the exception! |
I think all my issues here are solved, so I closed the issue 👍 |
Excellent! Glad things are working for you @Schumi09! |
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.:
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
and after setting
it finally worked. However, for my application I will need to remove/add new MarkerViews contiously so i set up a little test like
Without
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.
The text was updated successfully, but these errors were encountered: