androidhuawei-mobile-serviceshuawei-map-kit

Get reference of my location button in Huawei mapkit


I'm using Huawei MapKit and I want to add my custom MyLocationButton but I don't want to use the location manager to get my location and move to it, I just want to huaweiMapMyLocationButton.performClick(), for example, with google maps I can do this

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

I want something like this for huawei maps, I was able to go through the children of the map's view and I found the ui settings views ( Zoom, Compass, Location ) and following the location I was able to get this :

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)

But this didn't work


Solution

  • The MapView layer you used in HUAWEI Map Kit is incorrect. There is no need to parse the map based on the Google’s layer. Please try to use the following code to test the implementation' com.huawei.hms:maps:5.0.5.301 ' and add the customized 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);
                }
            }
    }