如何在Delphi FireMonkey的Android谷歌地图中隐藏标记点击后的导航与GPS按钮
Delphi 12.1 FireMonkey TMapView 隐藏谷歌地图导航工具栏
问题
用Delphi 12.1 + FireMonkey的TMapView组件开发Android应用,点击谷歌地图上的标记后,组件右下角会弹出带“导航”和“GPS指针”的工具栏,需要把这个工具栏隐藏掉。
Android原生里可以通过map.getUiSettings().setMapToolbarEnabled(false);禁用这个工具栏,但FireMonkey的TMapView没提供直接调用的API。
思路
Android端的JGoogleMap接口(位于Androidapi.JNI.PlayServices.Maps单元)具备所需的setMapToolbarEnabled方法,该接口实例被TAndroidMapView内部持有,但外部无法直接访问,因此只能通过RTTI反射获取实例并调用方法。
可行的RTTI实现
之前的RTTI代码未成功,大概率是字段查找或类型转换的问题,以下是调整后的可用代码:
class function TFrameAndroidViewMap.DisableMapToolBarRtti(const MapView: TMapView): Boolean; begin Result := False; var RttiContext := TRttiContext.Create; try // 获取TMapView的RTTI类型信息 var MapViewRttiType := RttiContext.GetType(MapView.ClassType); // 拿到内部的FMapView字段(对应TMapViewBase实例) var FMapViewField := MapViewRttiType.GetField('FMapView'); if not Assigned(FMapViewField) then Exit; var MapViewBaseInstance := FMapViewField.GetValue(MapView).AsObject as TMapViewBase; if not Assigned(MapViewBaseInstance) then Exit; // 再拿到TMapViewBase内部的FGoogleMap字段(JGoogleMap实例) var MapViewBaseRttiType := RttiContext.GetType(MapViewBaseInstance.ClassType); var FGoogleMapField := MapViewBaseRttiType.GetField('FGoogleMap'); if not Assigned(FGoogleMapField) then Exit; var GoogleMapValue := FGoogleMapField.GetValue(MapViewBaseInstance); var GoogleMap: JGoogleMap; if GoogleMapValue.TryAsType<JGoogleMap>(GoogleMap) and Assigned(GoogleMap) then begin // 禁用导航工具栏 GoogleMap.getUiSettings.setMapToolbarEnabled(False); Result := True; end; finally RttiContext.Free; end; end;
关键注意点
- 务必在
TMapView的OnMapReady事件触发后调用此方法,因为FGoogleMap实例只有在地图完全加载完成后才会初始化,提前调用会获取不到有效实例。 - 代码依赖FireMonkey的私有字段,后续Delphi版本更新若修改了字段命名,需要同步调整代码。
内容的提问来源于stack exchange,提问作者GDK - Remco
相关产品推荐
相关产品推荐

