在华为 mapkit 中获取我的位置按钮的引用
Get reference of my location button in Huawei mapkit
我正在使用华为 MapKit,我想添加我的自定义 MyLocationButton
但我不想使用位置管理器获取我的位置并移动到它,我只想 huaweiMapMyLocationButton.performClick()
,例如,使用 google 地图我可以做到这一点
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)
但这没有用
您在华为地图服务中使用的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);
}
}
}
HuaweiMap.class 中有一个名为 moveCamera(CameraUpdate var1) 的函数
所以还有另一种方法:
- 获取当前位置的经纬度
- 在onMapReady()的回调中使用moveCamera函数
public void onMapReady(HuaweiMap map) {
map.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(48.893, 2.334),10));
}
(48.893, 2.334) 替换为设备位置。
我正在使用华为 MapKit,我想添加我的自定义 MyLocationButton
但我不想使用位置管理器获取我的位置并移动到它,我只想 huaweiMapMyLocationButton.performClick()
,例如,使用 google 地图我可以做到这一点
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)
但这没有用
您在华为地图服务中使用的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);
}
}
}
HuaweiMap.class 中有一个名为 moveCamera(CameraUpdate var1) 的函数 所以还有另一种方法:
- 获取当前位置的经纬度
- 在onMapReady()的回调中使用moveCamera函数
public void onMapReady(HuaweiMap map) {
map.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(48.893, 2.334),10));
}
(48.893, 2.334) 替换为设备位置。