HERE Maps SDK for Flutter 4.6.4实例化TapListener报错求适配方案
HERE Maps SDK for Flutter 4.6.4版本点击监听器适配方案
问题原因
你参考的是4.8.2版本的使用文档,该版本对TapListener做了封装优化,支持直接传入回调函数实例化,但4.6.4探索版中TapListener属于抽象接口,无法直接实例化,必须自定义实现类重写对应方法才能使用。
适配步骤
- 第一步:自定义实现
TapListener接口,重写onTap方法写入点击逻辑
class CustomTapListener extends TapListener { final HereMapController hereMapController; // 可根据需求扩展传入其他参数 CustomTapListener(this.hereMapController); @override void onTap(Point2D touchPoint) { var geoCoordinates = _toString(hereMapController.viewToGeoCoordinates(touchPoint)); print('Tap at: $geoCoordinates'); // 其他自定义点击逻辑可在此处添加 } }
- 第二步:修改原有监听器设置代码,传入自定义实现类的实例
void _setTapGestureHandler() { _hereMapController.gestures.tapListener = CustomTapListener(_hereMapController); }
注意事项
- 不需要监听点击事件时,及时将
_hereMapController.gestures.tapListener赋值为null,避免内存泄漏 - 同版本的
DoubleTapListener、LongPressListener、PanListener等其他手势监听器如果出现同类抽象类无法实例化的报错,都可以用相同的自定义实现类方案适配
提示:如果不需要复用逻辑,也可以用通用回调包装类实现,无需每次单独定义业务类,代码如下:
void _setTapGestureHandler() { _hereMapController.gestures.tapListener = TapListenerImpl(onTap: (touchPoint) { var geoCoordinates = _toString(_hereMapController.viewToGeoCoordinates(touchPoint)); print('Tap at: $geoCoordinates'); }); } class TapListenerImpl extends TapListener { final Function(Point2D) onTapCallback; TapListenerImpl({required this.onTapCallback}); @override void onTap(Point2D touchPoint) => onTapCallback(touchPoint); }
内容的提问来源于stack exchange,提问作者Gicu Mironica
相关产品推荐
相关产品推荐

