java - 如何使用 Mapbox 绘制箭头

标签 java android mapbox

使用 Mapbox Android SDK 和注释插件,是否可以向线条添加箭头?如果没有,有什么方法可以建议一条线的方向吗?理想情况下,我希望有两个标记,它们之间有一个箭头,以便用户知道要行进的方向。

谢谢

最佳答案

不幸的是,Mapbox Lines 不支持此功能。但是,使用新的 Mapbox API v7 和注释插件,您可以执行以下操作来获得您所需要的内容。
1. 使用 LineManager 在 Mapview 上绘制一条线
2. 计算直线的方位角(以度为单位)
3. 将可绘制箭头旋转计算出的角度(可绘制箭头可以是向上的箭头)
4. 使用 SymbolManager 在 Mapview 上绘制符号。该符号将放置在线条的中间,并用作旋转的可绘制图像

只需将此代码放在 Mapview Activity 中的任何位置

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

关于java - 如何使用 Mapbox 绘制箭头,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54409187/

相关文章:

java - Spring +springmvc+mybatis :Injection of autowired dependencies failed

android - 使用 ExoPlayer 播放 YouTube 视频

java - Mapbox map 在 Android 上不显示

java - 无法使用 selenium java 选择单选按钮

java - 在 spring 中调用 jackson 之前获取 http post 请求正文

java - 找到最窄间隔的算法,其中 m 将覆盖一组数字

java - 是否可以在android中以编程方式关闭静音模式?

尽管我选择了网络摄像头作为来源,但 Android 虚拟设备摄像头无法正常工作

ios - 我应该如何在 Mapbox for iOS SDK 中使用 userLocation 找到正确的位置数据? (迅速)

javascript - 在传单中,fitBounds 是否有回调?