【问题标题】:Custom markers not visible in mapbox-android自定义标记在 mapbox-android 中不可见
【发布时间】:2020-02-10 14:59:43
【问题描述】:

我正在使用 mapbox-sdk android 进行位置跟踪。我想在指定位置的地图上添加一些自定义标记。但是下面的代码对我不起作用。

MarkerOptions options = new MarkerOptions();
options.title("pos");
IconFactory iconFactory = IconFactory.getInstance(MainActivity.this);
Icon icon = iconFactory.fromResource(R.drawable.home);
options.icon(icon);
options.position(new LatLng(80.27, 13.09));
mapboxMap.addMarker(options); 

我使用 mapox-sdk:6.0.1

【问题讨论】:

    标签: android mapbox-android mapbox-marker


    【解决方案1】:

    使用这个:

    private fun addMarkerIconsToMap(loadedMapStyle: Style) {
        BitmapUtils.getBitmapFromDrawable(getDrawable(R.drawable.red_marker))?.let {
            loadedMapStyle.addImage("icon-id", it)
        }
        if(!locationList.isNullOrEmpty()){
            val feature: ArrayList<Feature> = ArrayList()
            for (x in 0 until locationList.size){
                feature.add(Feature.fromGeometry(Point.fromLngLat(locationList[x].long, locationList[x].lat)))
            }
            loadedMapStyle.addSource(GeoJsonSource("source-id",
                FeatureCollection.fromFeatures(feature)
                )
            )
            loadedMapStyle.addLayer(SymbolLayer("layer-id", "source-id").withProperties(
                iconImage("icon-id"),
                iconOffset(arrayOf(0f, -8f))
                )
            )
        }
    }
    

    locationList 内部是 long:Double 和 lat:Double 的数组

    你的活动:

    class YourActivity : AppCompatActivity(), OnMapReadyCallback {
        private lateinit var mapboxMap: MapboxMap
        ...
        
    }
    

    然后覆盖:

    override fun onMapReady(mapboxMap: MapboxMap) {
        mapboxMap.setStyle(Style.MAPBOX_STREETS){
            addMarkerIconsToMap(it)
        }
    
        this.mapboxMap = mapboxMap
    }
    

    【讨论】:

      【解决方案2】:

      这就是我添加自定义标记的方式(从 Compose 的角度来看)。 (您可以根据您的程序结构从 viewModel 中执行此操作)

       AndroidView(
          factory = { mapView }, modifier = Modifier.fillMaxWidth()        
      ) { mView ->
          val bitmap = bitmapFromDrawableRes(context, R.drawable.chicken_marker)!!
          val annotation = mView.annotations
          val pointManager = annotation.createPointAnnotationManager()
          
          list.forEach { point ->
              val pointOptions = PointAnnotationOptions()
                  .withPoint(point)
                  .withIconImage(bitmap)
      
              pointManager.create(pointOptions.apply { iconSize = 0.8 })
          }
      

      这里是用来获取drawable(".svg",".png",".jpeg")的函数

      private fun bitmapFromDrawableRes(context: Context, @DrawableRes resourceId: Int): Bitmap? =
          convertDrawableToBitmap(AppCompatResources.getDrawable(context, resourceId))
      
      private fun convertDrawableToBitmap(sourceDrawable: Drawable?): Bitmap? {
          if (sourceDrawable == null) {
              return null
          }
          return if (sourceDrawable is BitmapDrawable) {
              sourceDrawable.bitmap
          } else {
      // copying drawable object to not manipulate on the same reference
              val constantState = sourceDrawable.constantState ?: return null
              val drawable = constantState.newDrawable().mutate()
              val bitmap: Bitmap = Bitmap.createBitmap(
                  drawable.intrinsicWidth, drawable.intrinsicHeight,
                  Bitmap.Config.ARGB_8888
              )
              val canvas = Canvas(bitmap)
              drawable.setBounds(0, 0, canvas.width, canvas.height)
              drawable.draw(canvas)
              bitmap
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-10-19
        • 2019-12-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-04-18
        • 2015-03-24
        相关资源
        最近更新 更多