android - 如何在 Kotlin 中生成标记数组
问题描述
我有 2 个单独的位置数据,我需要制作它们的数组(我想我需要制作它们的数组,也许你有更好的主意!)以便在谷歌地图中为这两个位置添加标记。
代码
位置部分已评论
override fun onMapReady(googleMap: GoogleMap) {
mMap = googleMap
// Try to obtain the map from the SupportMapFragment.
if (ContextCompat.checkSelfPermission(
requireContext(),
android.Manifest.permission.ACCESS_FINE_LOCATION
) ==
PackageManager.PERMISSION_GRANTED &&
ContextCompat.checkSelfPermission(
requireContext(),
android.Manifest.permission.ACCESS_COARSE_LOCATION
) ==
PackageManager.PERMISSION_GRANTED) {
googleMap.setMyLocationEnabled(true);
googleMap.getUiSettings().setMyLocationButtonEnabled(true);
} else {
Toast.makeText(context, "Allow location access", Toast.LENGTH_LONG).show();
}
mFusedLocationClient = context?.let { LocationServices.getFusedLocationProviderClient(it) }!!
mFusedLocationClient.lastLocation
.addOnSuccessListener { location: Location? ->
if (location != null) {
// Location 1 (current location of user)
val driverLatLng = LatLng(location.latitude, location.longitude)
mMap!!.moveCamera(CameraUpdateFactory.newLatLngZoom(driverLatLng, 15f))
// Zoom in, animating the camera.
mMap!!.animateCamera(CameraUpdateFactory.zoomIn())
}
}
//new
// Location 2
val geoCoder = Geocoder(context)
var address = geoCoder.getFromLocationName(customerAddressArgument, 1)!![0]
val customerLatLng= LatLng(address.latitude, address.longitude)
mMap!!.addMarker(MarkerOptions().position(customerLatLng).title("Customer Location"))
val cameraPosition = CameraPosition.Builder()
.target(customerLatLng) // Sets the center of the map to Mountain View
.zoom(17f) // Sets the zoom
.bearing(90f) // Sets the orientation of the camera to east
.tilt(30f) // Sets the tilt of the camera to 30 degrees
.build() // Creates a CameraPosition from the builder
mMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition))
}
我需要的是制作数组:val driverLatLng = LatLng(location.latitude, location.longitude)
和val customerLatLng = LatLng(address.latitude, address.longitude)
任何想法?
解决方案
这是向地图添加标记的解决方案
首先获取两个位置点,例如 location2 和 location2
val markerOptions = MarkerOptions()
// First marker
markerOptions.position(location1).icon(BitmapDescriptorFactory.fromBitmap
(BitmapFactory.decodeResource(resources, R.mipmap.ic_user_location)))
mMap.addMarker(markerOptions)
// second marker
markerOptions.position(location2).icon(BitmapDescriptorFactory.fromBitmap
(BitmapFactory.decodeResource(resources, R.mipmap.ic_user_location)))
mMap.addMarker(markerOptions)
// Move camera
mMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition))
// resize icon
private fun resizeIcon():Bitmap{
val height = 120
val width = 60
val b: Bitmap = BitmapFactory.decodeResource(resources, R.drawable.ic_map_marker)
val smallMarker = Bitmap.createScaledBitmap(b, width, height, false)
return smallMarker
}
推荐阅读
- excel - 在 VBA 中复制和粘贴表格的行
- javascript - 如何在 JavaScript 中使用 Teachable Machine 在视频而不是网络摄像头上运行姿势估计推断?
- python-3.x - 编辑python脚本
- angular - 嵌套延迟加载模块上的 RouteConfigLoadStart
- c - CLion Profiler:停止进程后出现“无分析器数据”消息
- java - 使用多个 jms 侦听器处理多个队列的可能性
- reactjs - 为什么我不能在导航栏按钮和关闭按钮之间切换?
- c++ - 如何以更通用的方式转换 unique_ptr 的向量
- javascript - redux reducer 哪个更快:switch 还是 map?
- url-parameters - 如何根据 URL 参数计算公式