[英]How to add marker on google map with Kotlin?
I am a beginner in Android programming.我是 Android 编程的初学者。 I want to add a marker at the point by tapping the Map screen.
我想通过点击地图屏幕在该点添加一个标记。 So I have tried coding below.
所以我尝试在下面编码。 But it doesn't work.
但它不起作用。
override fun onMapReady(googleMap: GoogleMap) {
mMap = googleMap
mMap.setOnMapClickListener(object :GoogleMap.OnMapClickListener {
override fun onMapClick(latlng :LatLng) {
val location = LatLng(latlng.latitude,latlng.longitude)
mMap.addMarker(MarkerOptions().position(location))
}
})
}
googleMap!!.addMarker(
MarkerOptions()
.position(
LatLng(
mapViewModel!![i].mLongitude,
mapViewModel!![i].mLatitude
)
)
.icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_mc_one))
.title("" + mapViewModel!![i].mOrganizationName.capitalize())
.snippet("" + mapViewModel!![i].mOrganizationTagLine.capitalize())
)
Before add Marker
on GoogleMap I would suggest to clean the map and animate the camera to new tapped position.在GoogleMap上添加
Marker
之前,我建议清理地图并将相机动画到新的点击位置。
Hope this helps you:希望这对你有帮助:
override fun onMapReady(googleMap: GoogleMap) {
mMap = googleMap
mMap.setOnMapClickListener(object :GoogleMap.OnMapClickListener {
override fun onMapClick(latlng :LatLng) {
// Clears the previously touched position
mMap.clear();
// Animating to the touched position
mMap.animateCamera(CameraUpdateFactory.newLatLng(latLng));
val location = LatLng(latlng.latitude,latlng.longitude)
mMap.addMarker(MarkerOptions().position(location))
}
})
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.