I am having trouble implementing adding a marker using MapBox in my fragment whose code is
package com.example.safenet.fragment
import android.content.Context
import android.graphics.Bitmap
import android.graphics.Canvas
import android.graphics.drawable.BitmapDrawable
import android.graphics.drawable.Drawable
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.annotation.DrawableRes
import androidx.appcompat.content.res.AppCompatResources
import com.example.safenet.MainActivity
import com.example.safenet.R
import com.mapbox.android.core.permissions.PermissionsListener
import com.mapbox.android.core.permissions.PermissionsManager
import com.mapbox.geojson.Point
import com.mapbox.maps.MapView
import com.mapbox.maps.Style
import com.mapbox.maps.plugin.annotation.annotations
import com.mapbox.maps.plugin.annotation.generated.PointAnnotationManager
import com.mapbox.maps.plugin.annotation.generated.PointAnnotationOptions
import com.mapbox.maps.plugin.annotation.generated.createPointAnnotationManager
class heat_wave_main : Fragment() {
private lateinit var mapView2: MapView
private var pointAnnotationManager: PointAnnotationManager? = null
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_heat_wave_main, container, false)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
// Get the PermissionsManager instance from the activity
val permissionsManager = (requireActivity() as MainActivity).permissionsManager
// Use the PermissionsManager instance as needed
if (PermissionsManager.areLocationPermissionsGranted(requireContext())) {
// Permission sensitive logic called here, such as activating the Maps SDK's LocationComponent to show the device's location
} else {
permissionsManager.requestLocationPermissions(requireActivity())
}
mapView2 = view.findViewById(R.id.mapViewHeat)
mapView2.getMapboxMap().loadStyleUri(
Style.MAPBOX_STREETS
) { style ->
addAnotationToMap(style)
}
}
private fun addAnotationToMap(style: Style) {
bitmapFromDrawableRes(requireContext(),R.drawable.baseline_local_activity_24)?.let{bitmap->
val annotationApi = mapView2.annotations
pointAnnotationManager = annotationApi.createPointAnnotationManager(mapView2)
// Set options for the resulting symbol layer.
val pointAnnotationOptions: PointAnnotationOptions = PointAnnotationOptions()
// Define a geographic coordinate.
.withPoint(Point.fromLngLat(18.06, 59.31))
// Specify the bitmap you assigned to the point annotation
// The bitmap will be added to map style automatically.
.withIconImage(bitmap)
pointAnnotationManager?.create(pointAnnotationOptions)
}
}
private fun bitmapFromDrawableRes(context: Context, @DrawableRes resourceId: Int) =
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
}
}
}
it keeps on saying error as Type mismatch: inferred type is MapView but AnnotationConfig? was expected in pointAnnotationManager = annotationApi.createPointAnnotationManager(mapView2)
the Main Activity file is
package com.example.safenet
import android.os.Bundle
import android.widget.FrameLayout
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.fragment.app.Fragment
import com.example.safenet.fragment.cyclone_main
import com.example.safenet.fragment.flood_main
import com.example.safenet.fragment.heat_wave_main
import com.google.android.material.bottomnavigation.BottomNavigationView
import com.mapbox.android.core.permissions.PermissionsListener
import com.mapbox.android.core.permissions.PermissionsManager
import com.mapbox.maps.CameraOptions
import com.mapbox.maps.MapView
import com.mapbox.maps.Style
import com.mapbox.maps.plugin.PuckBearing
import com.mapbox.maps.plugin.locationcomponent.createDefault2DPuck
import com.mapbox.maps.plugin.locationcomponent.location
import com.mapbox.maps.plugin.viewport.viewport
class MainActivity : AppCompatActivity(), PermissionsListener {
private lateinit var bottomNavigation: BottomNavigationView
private lateinit var frameLayout: FrameLayout
lateinit var permissionsManager: PermissionsManager
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
enableEdgeToEdge()
setContentView(R.layout.activity_main)
ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main_activity)) { v, insets ->
val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
insets
}
bottomNavigation = findViewById(R.id.bottomNavView)
frameLayout = findViewById(R.id.frame_layout_main)
loadFragment(heat_wave_main(), true)
bottomNavigation.setOnNavigationItemSelectedListener { item ->
val items = item.itemId
if (items == R.id.nav_heat) {
loadFragment(heat_wave_main(), false)
} else if (items == R.id.nav_cyclone) {
loadFragment(cyclone_main(), false)
} else if (items == R.id.nav_flood) {
loadFragment(flood_main(), false)
}
true
}
permissionsManager = PermissionsManager(this)
if (!PermissionsManager.areLocationPermissionsGranted(this)) {
permissionsManager.requestLocationPermissions(this)
}
}
private fun loadFragment(fragment: Fragment, isActive: Boolean) {
val fragmentManager = supportFragmentManager
val fragmentTransaction = fragmentManager.beginTransaction()
if (isActive) {
fragmentTransaction.add(R.id.frame_layout_main, fragment)
} else {
fragmentTransaction.replace(R.id.frame_layout_main, fragment)
}
fragmentTransaction.commit()
}
override fun onExplanationNeeded(permissionsToExplain: List<String>) {
TODO("Not yet implemented")
}
override fun onPermissionResult(granted: Boolean) {
TODO("Not yet implemented")
}
}
i was expecting to show a marker