0

尝试在android项目上的tomtom地图中添加自定义图标标记

这里的函数用于此:

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

    }

代码给了我这个错误:

要求:图标!, 发现:可绘制?

在调试导致错误的行之后

                    map.addMarker(MarkerBuilder(currentLatLng).
                        markerBalloon(balloon)
                        .icon(i))
4

1 回答 1

1

如果你的drawable文件夹中有一个典型的图像文件(例如png),你可以使用Icon.Factory.fromResources函数:

tomtomMap.addMarker(MarkerBuilder(latLng)
    .icon(Icon.Factory.fromResources(this, R.drawable.pin)))

如果您的图像被定义为 android 矢量可绘制对象,您可以从中创建一个BitmapDrawable并在函数内部使用它Icon.Factory.fromDrawable

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)))
于 2019-10-08T12:41:56.813 回答