简体   繁体   English

如何使用Mapbox绘制箭头

[英]How to draw arrows with Mapbox

With the Mapbox Android SDK and Annotation plugin, is there anyway to add arrows to lines? 使用Mapbox Android SDK和Annotation插件,是否有向线添加箭头的方法? If not, is there any way to suggest the direction of a line? 如果没有,是否有任何方法可以建议线的方向? Ideally, I would like to have two markers with an arrow between them so the user knows which direction to travel. 理想情况下,我希望有两个标记,它们之间有一个箭头,以便用户知道行进的方向。

Thanks 谢谢

Mapbox doesn't support arrows out-of-the-box, but you can experiment with the Line#setPattern which takes as an argument a name of the image added to the map with the Style#addImage / MapboxMap#addImage . Mapbox不支持开箱即用的箭头,但是您可以尝试使用Line#setPattern ,它使用Style#addImage / MapboxMap#addImage将添加到地图的图像名称作为参数。 You can read more about the requirements of that pattern image here . 您可以在此处阅读有关该图案图像要求的更多信息。 Otherwise, you'll need to roll out a custom solution by correctly positioning and rotating the arrow graphic (a Symbol ) on the map. 否则,您将需要通过在地图上正确定位和旋转箭头图形( Symbol )来推出自定义解决方案。 You can set that up with symbol-placement and icon-rotation-alignment style properties and exposed SymbolManager methods . 您可以使用symbol-placementicon-rotation-alignment样式属性以及公开的SymbolManager方法进行设置

Unfortunately, Mapbox Lines do not support this functionality. 不幸的是,Mapbox Lines不支持此功能。 However, using the new Mapbox API v7 and the Annotations Plugin you can do the following to get exactly what you need. 但是,使用新的Mapbox API v7和Annotations插件,您可以执行以下操作以获取所需的信息。
1. Draw a Line on the Mapview using a LineManager 1.使用LineManager在Mapview上画一条线
2. Calculate the bearing of the Line in degrees 2.计算直线的方位角(以度为单位)
3. Rotate a drawable arrow by the calculated degrees (The drawable can be an arrow pointing up) 3.将计算出的箭头旋转计算出的角度(该绘制可以是向上的箭头)
4. Draw a Symbol on the Mapview using a SymbolManager. 4.使用SymbolManager在Mapview上绘制一个Symbol。 This symbol will be placed in the middle of the line and use as an image the rotated drawable 该符号将放置在线条的中间,并将旋转的可绘制对象用作图像

Just put this code anywhere in your Mapview activity 只需将此代码放在Mapview活动中的任何位置

public void createLineAndArrow(){  

  //Declare the coordinates of the two points of the line
  float latitude1  = 34.1f;
  float longitude1 = 33.2f;
  float latitude2  = 35f;
  float longitude2 = 34.5f;

  //Calculate bearing of line
  double lat1Rad = Math.toRadians(latitude1);
  double lat2Rad = Math.toRadians(latitude2);
  double deltaLonRad = Math.toRadians(longitude2 - longitude1);
  double y = Math.sin(deltaLonRad) * Math.cos(lat2Rad);
  double x = Math.cos(lat1Rad) * Math.sin(lat2Rad) - Math.sin(lat1Rad) * 
             Math.cos(lat2Rad) * Math.cos(deltaLonRad);
  double bearing =  (Math.toDegrees(Math.atan2(y,x))+360)%360;

  //Draw the Line
  List<LatLng> lineVertices = new ArrayList<>();
  lineVertices.add(new LatLng(latitude1,longitude1));
  lineVertices.add(new LatLng(latitude2,longitude2));
  LineOptions lineOptions = new LineOptions().withLatLngs(lineVertices)
                        .withLineColor(ColorUtils.colorToRgbaString(Color.MAGENTA))
                        .withLineWidth(3f);
  LineManager lineManager = new LineManager(mapView, mapboxMap,mapboxMap.getStyle());
  lineManager.create(lineOptions);

  //Rotate the drawable
  Bitmap bmapOriginal = BitmapFactory.decodeResource(getResources(), 
                        R.drawable.arrowup);
  final Bitmap bmap = bmapOriginal.copy(Bitmap.Config.ARGB_8888, true);
  Matrix matrix = new Matrix();
  matrix.postRotate((float)bearing);
  Bitmap rotatedBitmap = Bitmap.createBitmap(bmap , 0, 0, bmap.getWidth(), 
                         bmap.getHeight(), matrix, true);
  Drawable d = new BitmapDrawable(getResources(), rotatedBitmap);

  //Add the drawable to the selected mapbox style
  mapboxMap.getStyle().addImage("rotatedImage",
                        BitmapUtils.getBitmapFromDrawable(d),
                        true);

 //Draw the Symbol in the middle of the Line
 SymbolOptions symbolOptions = new SymbolOptions().withIconImage("rotatedImage")
                          .withGeometry(Point.fromLngLat((longitude2+longitude1)/2, 
                          (latitude2+latitude1)/2));

 SymbolManager symbolManager = new SymbolManager(mapView, mapboxMap, 
                               mapboxMap.getStyle());
 symbolManager.create(symbolOptions);

}  

Image - The above code draws this on a mapview 图片-上面的代码将其绘制在地图视图上

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

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