如何在 Kotlin 中生成标记数组

How to generate marker array in Kotlin

我有 2 个单独的位置数据,我需要对它们进行排列(我想我需要对它们进行排列,也许你有更好的主意!)以便在 google 地图中为两个位置添加标记。

代码

Locations part are commented

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)

有什么想法吗?

创建

var markerList:ArrayList<Marker> = ArrayList()

创建你的标记

 val marker =  MarkerOptions().position(latlng).anchor(
            0.5f,
            0.5f
        ).title(your tittle).snippet(it.event_id).icon(your marker icon)

//比添加标记到标记列表

markerList.add(标记)

使用for循环将标记设置到google地图

     markerList.forEach{
      // set marker to your google map with it
     }
      

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

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

}