Trying to add a custom icon marker to tomtom maps on android project
here the function am using for this :
private fun addingStation() {
val i = ResourcesCompat.getDrawable(getResources(), R.drawable.ic_dialog_alert, null)
val database = FirebaseDatabase.getInstance()
val myRef = database.getReference("station")
// Read from the database
myRef.addValueEventListener(object : ValueEventListener {
override fun onDataChange(dataSnapshot: DataSnapshot) {
for (ds in dataSnapshot.getChildren()) {
val name = ds.child("station_name").getValue(String::class.java)
val stationLongitude = ds.child("station_longitude").getValue(String::class.java)
val stationLatitude = ds.child("station_latitude").getValue(String::class.java)
val longitude = stationLongitude.let { java.lang.Double.parseDouble(it!!) }
val latitude = stationLatitude.let { java.lang.Double.parseDouble(it!!) }
val currentLatLng = LatLng(latitude, longitude)
val balloon = SimpleMarkerBalloon(name)
map.addMarker(MarkerBuilder(currentLatLng).
markerBalloon(balloon)
.icon(i))
}
}
override fun onCancelled(error: DatabaseError) {
// Failed to read value
println("failed")
}
})
val currentLatLng = LatLng(31.233334, 30.033333)
val balloon = SimpleMarkerBalloon("cairo are Here")
map.addMarker(MarkerBuilder(currentLatLng).markerBalloon(balloon))
}
The code gives me this error :
Required:Icon! , Found: Drawable?
after debugging the line that causes the error is
map.addMarker(MarkerBuilder(currentLatLng).
markerBalloon(balloon)
.icon(i))
If you have a typical image file (png for example) in your drawable folder, you can use Icon.Factory.fromResources
function:
tomtomMap.addMarker(MarkerBuilder(latLng)
.icon(Icon.Factory.fromResources(this, R.drawable.pin)))
If your image is defined as an android vector drawable, you can create a BitmapDrawable
from it and use it inside Icon.Factory.fromDrawable
function:
val drawable = this.resources.getDrawable(R.drawable.marker, theme)
val 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)
val bitmapDrawable = BitmapDrawable(this.resources, bitmap)
tomtomMap.addMarker(MarkerBuilder(latLng)
.icon(Icon.Factory.fromDrawable("name", bitmapDrawable)))