简体   繁体   English

如何在 Mapbox 中为静态图像添加折线叠加层?

[英]How to add a polyline overlay to a static image in Mapbox?

I'm trying to save a map with a route as a static image.我正在尝试将带有路线的地图保存为静态图像。 I'm currently able to save the map as a static map with some annotations as overlay, but I'm unable to figure out how to add the route to the static map.我目前能够将地图保存为静态地图,并带有一些注释作为叠加层,但我无法弄清楚如何将路线添加到静态地图。

This is the code I currently have:这是我目前拥有的代码:

List<StaticMarkerAnnotation> markers = new ArrayList<>();
List<StaticPolylineAnnotation> polylines = new ArrayList<>();

markers.add(StaticMarkerAnnotation.builder().name(StaticMapCriteria.LARGE_PIN)
             .lnglat(Point.fromLngLat(pointList.getFirst().longitude(), pointList.getFirst().latitude()))
             .label("a")
             .build());

markers.add(StaticMarkerAnnotation.builder().name(StaticMapCriteria.LARGE_PIN)
             .lnglat(Point.fromLngLat(pointList.getLast().longitude(),pointList.getLast().latitude()))
             .label("b")
             .build());

polylines.add(StaticPolylineAnnotation.builder().polyline(currentRoute.geometry()).build()); // DirectionsRoute currentRoute

String staticImage = MapboxStaticMap.builder()
             .accessToken(getString(R.string.mapbox_access_token))
             .width(mapView.getMeasuredWidth())
             .height((int) (250*getResources().getDisplayMetrics().density))
             .retina(true)
             .cameraAuto(true)
             .staticMarkerAnnotations(markers)
             .staticPolylineAnnotations(polylines) // does not work!
             .build()
             .url()
             .toString();

Without the .staticPolylineAnnotations(polylines) it creates successfully creates a static image (without route).如果没有 .staticPolylineAnnotations(polylines),它会成功创建一个静态图像(没有路由)。 With the .staticPolylineAnnotations(polylines), the created staticImage string results in {"message":"Latitude must be between -85.0511-85.0511."}.使用 .staticPolylineAnnotations(polylines),创建的 staticImage 字符串结果为 {"message":"Latitude must be between -85.0511-85.0511."}。

I guess I'm passing my currentRoute incorrectly, but I don't know the right way.我想我错误地传递了我的 currentRoute,但我不知道正确的方法。

For getting the Route image in Android First add your all Lat and Long into a List and after that convert your complete list of lat and long into encoded poly line string with the help of Google Poly line Encoding techniques.为了在 Android 中获取路线图像,首先将所有纬度和经度添加到一个列表中,然后在谷歌多边形线编码技术的帮助下将完整的纬度和经度列表转换为编码的多边形线串。

-> After that create Map box account and use static image API of map box with valid token -> 之后创建地图框帐户并使用具有有效令牌的地图框的静态图像API

-> Now use StaticPolylineAnnotation: -> 现在使用 StaticPolylineAnnotation:

List{StaticPolylineAnnotation} list1 = new ArrayList<>(); List{StaticPolylineAnnotation} list1 = new ArrayList<>(); (Replace {} with <>) list.add(StaticPolylineAnnotation.builder().polyline(polilineString).strokeWidth(1.0).strokeColor(56, 69, 181).build()); (用 <> 替换 {}) list.add(StaticPolylineAnnotation.builder().polyline(polilineString).strokeWidth(1.0).strokeColor(56, 69, 181).build()); Note: PolilineString is your encoded string got from Google Poly line Encoding techniques注意: PolilineString 是您从 Google Poly line Encoding 技术获得的编码字符串

-> MapboxStaticMap staticImage = MapboxStaticMap.builder().user("mapboxaccountusename") .accessToken("Mapbox Token") .styleId("Your Style id") .staticPolylineAnnotations(list1) .cameraAuto(true) .retina(true) .build(); -> MapboxStaticMap staticImage = MapboxStaticMap.builder().user("mapboxaccountusename") .accessToken("Mapbox Token") .styleId("Your Style id") .staticPolylineAnnotations(list1) .cameraAuto(true) .retina(true) 。建造();

->get url of image using String imageUrl = staticImage.url().toString(); -> 使用 String imageUrl = staticImage.url().toString() 获取图像的 url;

This error message {"message": "Latitude must be between -85.0511-85.0511."} usually appears when you have the latitude and longitude in a reversed order.此错误消息 {"message": "Latitude must be between -85.0511-85.0511."} 通常出现在纬度和经度顺序相反时。 It worth testing your coordinates in the Mapbox direction API playground to validate it before adding them to your implementation.在将它们添加到您的实现之前,值得在Mapbox 方向 API 游乐场中测试您的坐标以对其进行验证。

You could also explore using the MapSnapshotter for Android to save your map with a route.您还可以探索使用Android 版 MapSnapshotter来保存带有路线的地图。

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

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