简体   繁体   English

在 Android 上的 Mapbox MapSnapshotter 中设置地图注释

[英]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.我有一个 Android 应用程序,它通过 UDP 套接字将地图图片流式传输到外部嵌入式设备。 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 .因此,我创建了一个前台服务(地图流应该在后台运行,以便在另一个应用程序在前面或屏幕关闭时也能工作)实例化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.但是, MapSnapshotter的界面非常有限。 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 .我将能够手动编辑样式 JSON,添加我需要的自定义源和图层,并直接使用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.但这使得快照程序再次重新加载整个样式,这非常慢(在我的设置中,大约需要 1 秒),这不符合我的要求。 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.当然,另一种可能是直接在snapshotter返回的位图中绘制线条/图标,但这非常有限(例如,我无法在文本下方绘制)并且当地图倾斜时会变得复杂。

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.如果我可以使用 Mapbox 注释插件,那就太完美了。 Unfortunately, to instantiate an annotation manager like SymbolManager I need a MapView / MapboxMap .不幸的是,要实例化像SymbolManager这样的注释管理器,我需要一个MapView / MapboxMap I assume that internally MapSnapshotter also holds some kind of MapboxMap , but it's definitely not accessible through the public interface.我假设MapSnapshotter内部也包含某种MapboxMap ,但它绝对不能通过公共接口访问。 Any help is highly appreciated!任何帮助表示高度赞赏!

Mapbox's Android MapSnapshotter does not currently support capturing runtime styling added to the map. Mapbox 的Android MapSnapshotter目前不支持捕获添加到地图的运行时样式。 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.因此,即使您可以设置注释管理器(您已正确指出由于依赖于底层MapView / MapboxMap而无法完成),从snapshot.getBitmap()检索的图像也不包含注释。

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.因此,此建议可能无法满足您的用例,但仍然值得一提:您可以尝试使用Mapbox Studio将标记添加到地图样式本身,然后将生成的地图样式传递给快照程序的withStyle参数。

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.或者,您可以实例化MapboxMap对象而不是MapSnapshotter ,然后使用此 Stack Overflow 帖子中的建议代码, 代码并非特定于 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.如果您将包含 Mapbox 地图对象的Layout传递给链接帖子的答案中提供的takeScreenShot方法,您应该能够生成一个Bitmap其中包含您的地图和使用注释管理器或其他形式的客户端添加到所述地图的注释-添加了地图层。 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.同时,我建议您密切关注 Mapbox 的 Android MapSnapshotter文档以了解任何更改以适应此动态用例,并且可能会在mapbox/mapbox-gl-native-android打开一个问题来表达功能请求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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