Flutter中点击Google Maps Marker时InfoWindow不显示
Flutter Google Maps Marker点击后InfoWindow不显示的解决方法
问题核心:你为Marker自定义了onTap回调函数,这会覆盖Google Maps插件默认的Marker点击触发InfoWindow显示的行为,所以即使配置了infoWindow参数,点击后也不会自动弹出。
解决步骤:
- 保存
GoogleMapController实例,用于控制地图交互:
GoogleMapController? _mapController; void _onMapCreated(GoogleMapController controller) { _mapController = controller; }
- 在Marker的
onTap回调中,手动调用showMarkerInfoWindow方法来显示对应的InfoWindow:
修改你的Marker创建代码:
for (var student in _students) { _markers.add( Marker( markerId: MarkerId(student.id), position: LatLng(student.gps.latitude, student.gps.longitude), infoWindow: InfoWindow( title: student.nombre, snippet: student.gps.idRfid, ), onTap: () { _moveCamera(LatLng(student.gps.latitude, student.gps.longitude), zoomIn: true); context.read<MapStateCubit>().showParadas(student); lastUpdateTime = student.gps.lastUpdateTime; // 手动触发InfoWindow显示 _mapController?.showMarkerInfoWindow(MarkerId(student.id)); }, icon: markerBusIcon!, ), ); }
补充优化(可选)
如果需要保证同一时间只显示一个InfoWindow,可以在显示当前之前先关闭其他的:
onTap: () { // 先隐藏所有已显示的InfoWindow _mapController?.hideInfoWindow(); _moveCamera(LatLng(student.gps.latitude, student.gps.longitude), zoomIn: true); context.read<MapStateCubit>().showParadas(student); lastUpdateTime = student.gps.lastUpdateTime; // 显示当前Marker的InfoWindow _mapController?.showMarkerInfoWindow(MarkerId(student.id)); },
额外检查点
- 确保
_mapController在调用showMarkerInfoWindow时已经完成初始化(即_onMapCreated已经执行),空安全判断可以避免报错 - 确认
student.id是唯一值,保证MarkerId不会重复导致识别错误
内容的提问来源于stack exchange,提问作者Cristian David Rosero Suarez
相关产品推荐
相关产品推荐

