首页 > 解决方案 > 如何在 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)

任何想法?

标签: androidkotlingoogle-maps-android-api-2

解决方案


这是向地图添加标记的解决方案

首先获取两个位置点,例如 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

}

推荐阅读