【问题标题】:How to generate marker array in Kotlin如何在 Kotlin 中生成标记数组
【发布时间】:2021-05-13 18:58:19
【问题描述】:

我有 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)

有什么想法吗?

【问题讨论】:

    标签: android kotlin google-maps-android-api-2


    【解决方案1】:

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

    首先获取 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
    
    }
    

    【讨论】:

    【解决方案2】:

    创建

    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 循环将标记设置到谷歌地图中

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

    【讨论】:

    • 您好,感谢您的回答,但我不理解您分享的任何一半代码(还是新手)您介意完成您的代码吗?我已经分享了所有需要的变量。
    • 只需将标记添加到 markerArrayList 并使用 for 循环访问所有标记,并在 for 循环中在谷歌地图中设置标记
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-08
    • 2021-12-13
    • 2012-09-23
    • 2021-10-26
    • 2020-01-25
    • 1970-01-01
    相关资源
    最近更新 更多