简体   繁体   English

如何在 kotlin 中更改 tomtom 地图的标记

[英]How to change tomtom map's marker in kotlin

Trying to add a custom icon marker to tomtom maps on android project尝试在 android 项目的 tomtom 地图中添加自定义图标标记

here the function am using for this:这里 function 用于此:

    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:如果您的可绘制文件夹中有一个典型的图像文件(例如 png),您可以使用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:如果您的图像被定义为 android 矢量绘图,您可以从中创建一个BitmapDrawable并在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)))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM