简体   繁体   中英

Setting map annotations in Mapbox MapSnapshotter on Android

I have an Android application which streams pictures of maps via a UDP socket to an external embedded device. Therefore, I created a foreground service (the map streaming should run in the background in order to work also when another app is in front or the screen is switched off) which instantiates a MapSnapshotter . So far everything works fine.

I would now like to add annotations (eg lines or icons like a current position marker) to the snapshotter. However, the interface of MapSnapshotter is quite limited. I would be able to edit the style JSON manually, adding the customized sources and layers I need, and setting the style directly with setStyleJson . But that makes the snapshotter reloading the whole style again, which is very slow (in my setup, it takes about 1 second), which does not fit with my requirements. Of course, another possibility is to draw the lines/icons directly in the bitmap returned by the snapshotter, but this is very limited (eg I cannot draw below the text) and gets complicated when the map is tilted.

Does anybody have experience with this problem or has an idea how to solve it? Perfect would be if I could use the Mapbox annotation plugin. Unfortunately, to instantiate an annotation manager like SymbolManager I need a MapView / MapboxMap . I assume that internally MapSnapshotter also holds some kind of MapboxMap , but it's definitely not accessible through the public interface. Any help is highly appreciated!

Mapbox's Android MapSnapshotter does not currently support capturing runtime styling added to the map. So, even if you could set up an annotations manager (which you've correctly pointed out cannot be done due to the dependence on an underlying MapView / MapboxMap ), the image retrieved from snapshot.getBitmap() would not include the annotations.

It sounds like you're interested in displaying dynamic annotations to the map (such as a user's location), which is likely subject to frequent change. As such, this suggestion likely won't met your use case but it's still worth mentioning: you could try adding markers to the map style itself using Mapbox Studio , and then pass the resulting map style to the snapshotter's withStyle parameter.

Alternatively, you could instantiate a MapboxMap object instead of a MapSnapshotter , and then use the suggested code in this Stack Overflow post , which is not specific to Mapbox. If you pass the Layout containing your Mapbox map object to the takeScreenShot method provided in the answer to the linked post, you should be able to generate a Bitmap containing your map and annotations added to said map with an annotations manager or other form of client-added map layer. Although this approach loses the benefit that snapshot generation has of happening on the device's background thread, it allows for more flexibility in terms of adding runtime styling.

In the meantime, I'd recommend keeping an eye out on Mapbox's Android MapSnapshotter documentation for any changes to accommodate this dynamic use case, and perhaps opening an issue in mapbox/mapbox-gl-native-android to voice a feature request.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM