Get reference of my location button in Huawei mapkit

369 Views Asked by At

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

2

There are 2 best solutions below

1
On BEST ANSWER

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);
            }
        }
}
0
On

There is a function named moveCamera(CameraUpdate var1) in the HuaweiMap.class so there is another way :

  1. get the lantitude and lontitude of current location
  2. use the function moveCamera in the callback of onMapReady()
public void onMapReady(HuaweiMap map) {   
map.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(48.893, 2.334),10));
}

(48.893, 2.334) replace with the device locaion.