Radesh
Radesh

Reputation: 13555

mapboxMap.clear() is deprecated

I use mapBox and after update to version 7.1.1 some function are deprecated but there is nothing to replace.

In this line addMarker and IconFactory and MarkerOptions are deprecated

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

And also clear() function is deprecated

mapboxMap.clear()

I added this

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

But there is nothing similar / helpful to clear map or add marker in doc / example

Upvotes: 3

Views: 2249

Answers (3)

shubomb
shubomb

Reputation: 832

In v10 , we can remove annotation (marker) using manager

pointAnnotationManager?.deleteAll()

Upvotes: 1

Manoj Perumarath
Manoj Perumarath

Reputation: 10194

For adding marker using new methods, modify your gradle with

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'
}

Using Markers

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()
}

For clearing the map you can use the delete function in SymbolManager

As per this thread 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);

This will be available from annotation-0.6.0

Upvotes: 1

android_dev
android_dev

Reputation: 4188

It will be released in annotation plugin 0.6.0. For now you can use this to clear all symbols or circles:

// 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)
}

Upvotes: 0

Related Questions