【问题标题】:Get reference of my location button in Huawei mapkit在华为 mapkit 中获取我的位置按钮的参考
【发布时间】:2020-12-10 11:55:25
【问题描述】:

我正在使用华为 MapKit,我想添加我的自定义MyLocationButton,但我不想使用位置管理器来获取我的位置并移动到它,我只想huaweiMapMyLocationButton.performClick(),例如,用谷歌地图我可以做到这一点

val locationButtonParent = view?.findViewById<View>(Integer.parseInt("1"))?.parent as? View?
val locationButton = locationButtonParent?.findViewById<View>(Integer.parseInt("2"))
locationButton.performClick()

我想要这样的华为地图,我能够浏览地图视图的子视图,我找到了 ui 设置视图(缩放、指南针、位置)并按照我能够得到的位置:

val mapFragment = childFragmentManager.findFragmentById(R.id.map) as SupportMapFragment
val fragmentView = mapFragment.view    
val myLocationButton =
            ((((fragmentView as ViewGroup).getChildAt(0) as ViewGroup).getChildAt(1) as ViewGroup).getChildAt(
                1
            ) as ViewGroup).getChildAt(0)

但这没有用

【问题讨论】:

    标签: android huawei-mobile-services huawei-map-kit


    【解决方案1】:

    您在HUAWEI Map Kit中使用的MapView图层不正确。无需根据 Google 的图层解析地图。 请尝试使用以下代码测试implementation' com.huawei.hms:maps:5.0.5.301 '并添加自定义的MyLocationButton

    //...
                    MapView mapView = mMapView;
                    dumpMapViewClickLoctionBtn(mapView, "location");
            }
        }
    
        private void dumpMapViewClickLoctionBtn(ViewGroup viewGroup, String id) {
            for (int j = 0; j < viewGroup.getChildCount(); j++) {
                View view = viewGroup.getChildAt(j);
                if (view.toString().contains(id)) {
                    view.performClick();
                }
                if (view instanceof ViewGroup) {
                    ViewGroup v = (ViewGroup) view;
                    dumpMapViewClickLoctionBtn(v,id);
                }
            }
    }
    

    【讨论】:

    • 谢谢你帮了很多忙,但是。我没有使用MabView,所以我在片段中使用了常规的view 属性,所以它就像view?.let{dumpMapViewClickLocationBtn(it,"location")}
    【解决方案2】:

    HuaweiMap.class中有一个函数moveCamera(CameraUpdate var1) 所以还有另一种方法:

    1. 获取当前位置的经纬度
    2. 在onMapReady()的回调中使用函数moveCamera
    public void onMapReady(HuaweiMap map) {   
    map.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(48.893, 2.334),10));
    }
    

    (48.893, 2.334) 替换为设备位置。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-27
      • 1970-01-01
      • 1970-01-01
      • 2023-04-02
      • 2016-08-27
      • 1970-01-01
      相关资源
      最近更新 更多