如何在 kotlin 中更改 tomtom 地图的标记
How to change tomtom map's marker in kotlin
正在尝试将自定义图标标记添加到 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))
}
代码给我这个错误:
必填:图标! , 发现: Drawable?
调试后导致错误的行是
map.addMarker(MarkerBuilder(currentLatLng).
markerBalloon(balloon)
.icon(i))
如果您的可绘制文件夹中有一个典型的图像文件(例如 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)))
正在尝试将自定义图标标记添加到 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))
}
代码给我这个错误:
必填:图标! , 发现: Drawable?
调试后导致错误的行是
map.addMarker(MarkerBuilder(currentLatLng).
markerBalloon(balloon)
.icon(i))
如果您的可绘制文件夹中有一个典型的图像文件(例如 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)))