mapboxMap.clear() is deprecated

Issue

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

Solution

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([email protected])
            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

Answered By – Manoj Perumarath

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published