簡體   English   中英

如何使用自定義標記選項在 HMS map 中顯示當前位置?

[英]How to show current location in HMS map with custom marker option?

我在我的應用程序中使用了 HMS Map。 我是 select 某個位置,使用定位工具包搜索並使用自定義標記顯示在 map 中。 我可以完美地獲得緯度和經度。 我也可以在幾天前在 map 中顯示所選位置。 但目前無法顯示。 我沒有更改代碼中的任何內容。 那么應該是什么問題呢?

private fun setMapWithCurrentLoc(lastLocation: android.location.Location?) {
    if (lastLocation != null) {
        val location = LatLng(lastLocation.latitude, lastLocation.longitude)
        if (map != null) {
            map!!.clear()
            map!!.addCircle(
                    CircleOptions()
                            .radius(500.0)
                            .center(location)
                            .clickable(false)
                            .fillColor(ContextCompat.getColor(requireContext(), R.color.purple_trans))
                            .strokeColor(ContextCompat.getColor(requireContext(), R.color.transparent))
                            .strokeWidth(1f)
            )
            map!!.addMarker(
                    MarkerOptions()
                            .position(location)
                            .icon(
                                    BitmapDescriptorFactory.fromBitmap(
                                            Utils.createCustomMarker(
                                                    requireContext(),
                                                    childUrl!!
                                            )
                                    )
                            )
                            .draggable(true)
            )
            map!!.animateCamera(
                    CameraUpdateFactory.newLatLngZoom(
                            LatLng(location.latitude, location.longitude),
                            currentZoomSetting.toFloat()
                    )
            )

        }
    }
}

嘗試更改一些代碼並在帶有com.huawei.hms.maps.SupportMapFragment的 Activity 中進行測試,它工作正常:

override fun onMapReady(paramHuaweiMap: HuaweiMap?) {
    Log.i(TAG, "onMapReady: ")
    hMap = paramHuaweiMap
    hMap!!.isMyLocationEnabled = true
    setMapWithCurrentLoc(LatLng(48.893478, 2.334595))
}

private fun setMapWithCurrentLoc(location: LatLng) {
    var map = hMap
    if (map != null) {
        map!!.clear()
        map!!.addCircle(
            CircleOptions()
                .radius(500.0)
                .center(location)
                .clickable(false)
                .fillColor(ContextCompat.getColor(this, R.color.colorAccent))
                .strokeColor(ContextCompat.getColor(this, R.color.colorAccent))
                .strokeWidth(1f)
        )
        map!!.addMarker(
            MarkerOptions()
                .position(location)
                .icon(
                    BitmapDescriptorFactory.fromResource(
                        R.mipmap.ic_launcher
                    )
                )
                .draggable(true)
        )

        map!!.animateCamera(
            CameraUpdateFactory.newLatLngZoom(
                LatLng(location.latitude, location.longitude),
                11f
            )
        )
    }
}

實現 'com.huawei.hms:maps:6.0.0.301' 可以在您的代碼中對此進行測試以查看是否有效。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM