mapboxMap.clear()已弃用

时间:2019-03-19 11:28:23

标签: android mapbox mapbox-plugin

我使用mapBox,并且在更新到7.1.1版之后,不赞成使用某些功能,但是没有可替换的内容。

在此行addMarkerIconFactoryMarkerOptions已弃用

mapboxMap.addMarker(MarkerOptions()
                .position(LatLng(lat, lng))
                .icon(IconFactory.getInstance(context)
                .fromResource(R.drawable.ic_marker)))

并且不建议使用clear()函数

mapboxMap.clear()

我添加了

implementation 'com.mapbox.mapboxsdk:mapbox-android-plugin-annotation-v7:0.5.0'

但是在文档/示例中清除地图或添加标记没有什么类似/有用的

2 个答案:

答案 0 :(得分:1)

要使用新方法添加标记, 用

修改您的gradle
andorid{
compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
  }
} 

dependencies {
implementation 'com.mapbox.mapboxsdk:mapbox-android-sdk:7.2.0'
implementation 'com.mapbox.mapboxsdk:mapbox-android-plugin-markerview-v7:0.2.0'
implementation 'com.mapbox.mapboxsdk:mapbox-android-plugin-annotation-v7:0.5.0'
}

使用标记

class MarkerActivity : AppCompatActivity()  {

private val random = Random()
private var markerViewManager: MarkerViewManager? = null
private var marker: MarkerView? = null
private lateinit var mapBox: MapboxMap

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    Mapbox.getInstance(
        this,
        "Your key"
    )

    setContentView(R.layout.activity_main)
    mapView.onCreate(savedInstanceState)
    mapView.getMapAsync { mapboxMap ->
        mapboxMap.setStyle(Style.MAPBOX_STREETS) { _ ->
            this.mapBox = mapboxMap
            mapboxMap.moveCamera(CameraUpdateFactory.zoomTo(2.0))

            markerViewManager = MarkerViewManager(mapView, mapboxMap)
            createRandomMarkers()
         }
      }
    }

    private fun createRandomMarkers() {
    markerViewManager?.let {
        for (i in 0..5) {
            val imageView = ImageView(this@MarkerActivity)
            imageView.setImageResource(R.mipmap.ic_launcher)
            imageView.layoutParams = FrameLayout.LayoutParams(50, 50)
            val markerView = MarkerView(createRandomLatLng(), imageView)
            it.addMarker(markerView)
        }
    }
}

 private fun createRandomLatLng(): LatLng {
    return LatLng(
        random.nextDouble() * -200.0 + 90.0,
        random.nextDouble() * -300.0 + 180.0
    )
}

override fun onStart() {
    super.onStart()
    mapView.onStart()
}

override fun onPause() {
    super.onPause()
    mapView.onPause()
}

override fun onStop() {
    super.onStop()
    mapView.onStop()
}

override fun onLowMemory() {
    super.onLowMemory()
    mapView.onLowMemory()
}

override fun onDestroy() {
    super.onDestroy()
    markerViewManager?.onDestroy()
    mapView.onDestroy()
}

要清除地图,可以使用SymbolManager

中的删除功能

根据此线程 thread

List<Symbol> symbols = new ArrayList<>();
LongSparseArray<Symbol> symbolArray = symbolManager.getAnnotations();
for (int i = 0; i < symbolArray.size(); i++) {
symbols.add(symbolArray.valueAt(i));
}
symbolManager.delete(symbols);

可从 annotation-0.6.0

获得

答案 1 :(得分:0)

它将在注释插件0.6.0中发布。现在,您可以使用它清除所有符号或圆圈:

// FIXME Temp function https://github.com/mapbox/mapbox-plugins-android/pull/859/commits/74b9ffa286e63b9c02ed7d2bdefa1ba48bd997a3
private fun deleteAllSymbols() {
    val symbols = mutableListOf<Symbol>()
    val symbolArray = symbolManager.annotations
    for (i in 0 until symbolArray.size()) {
        symbols.add(symbolArray.valueAt(i))
    }
    symbolManager.delete(symbols)
}

// FIXME Temp function https://github.com/mapbox/mapbox-plugins-android/pull/859/commits/74b9ffa286e63b9c02ed7d2bdefa1ba48bd997a3
private fun deleteAllCircles() {
    val circles = mutableListOf<Circle>()
    val circleArray = circleManager.annotations
    for (i in 0 until circleArray.size()) {
        circles.add(circleArray.valueAt(i))
    }
    circleManager.delete(circles)
}