Articles in this section
Category / Section

How to show tooltip at the tapped position until the next tap is received in Maps?

2 mins read

In Syncfusion® Flutter Maps, you can show tooltip at the tapped position until you tap on another position.

Step 1: Here, going to show marker at the tooltip position to avoid tooltip automatically hide after 3 seconds. Get the tapped pixel position using the handleEvent override method in MapZoomPanBehavior.

class _CustomMapZoomPanBehavior extends MapZoomPanBehavior {
  late MapTapCallback onTap;
 
  @override
  void handleEvent(PointerEvent event) {
    if (event is PointerUpEvent) {
      onTap(event.localPosition);
    } 
    super.handleEvent(event);
  }
}
 
typedef MapTapCallback = void Function(Offset position);

 

Step 2: Initialize the maps along with markerBuilder and shapeTooltipBuilder callbacks. Convert the pixel position into coordinate point using the MapShapeLayerController.pixelToLatLng method in the updateMarkerChange method and show the tapped shape details in a marker using the index get from the shapeTooltipBuilder callback.

https://help.syncfusion.com/flutter/maps/markers#adding-markers-dynamically

late MapLatLng _markerPosition;
late _CustomMapZoomPanBehavior _zoomPanBehavior;
late MapShapeLayerController _controller;
late List<Model> _data;
late MapShapeSource _mapSource;
int _tappedIndex = 0;
 
@override
void initState() {
  _controller = MapShapeLayerController();
  _zoomPanBehavior = _CustomMapZoomPanBehavior()
    ..onTap = updateMarkerChange;
  _data = <Model>[
    Model('Asia', '44,579,000 sq. km.'),
    Model('Africa', '30,370,000 sq. km.'),
    Model('Europe', '10,180,000 sq. km.'),
    Model('North America', '24,709,000 sq. km.'),
    Model('South America', '17,840,000 sq. km.'),
    Model('Australia', '8,600,000 sq. km.'),
  ];
  
  _mapSource = MapShapeSource.asset(
    "assets/world_map.json",
    shapeDataField: "continent",
    dataCount: _data.length,
    primaryValueMapper: (int index) => _data[index].continent,
  );
  super.initState();
}
 
void updateMarkerChange(Offset position) {
  _markerPosition = _controller.pixelToLatLng(position);
}
 
@override
Widget build(BuildContext context) {
  return Scaffold(
    body: SfMaps(
      layers: [
        MapShapeLayer(
          source: _mapSource,
          zoomPanBehavior: _zoomPanBehavior,
          controller: _controller,
          shapeTooltipBuilder: (BuildContext context, int index) {
            _tappedIndex = index;
            if (_controller.markersCount > 0) {
              _controller.clearMarkers();
            }
            _controller.insertMarker(0);
            return SizedBox();
          },
          markerBuilder: (BuildContext context, int index) {
            return MapMarker(
              latitude: _markerPosition.latitude,
              longitude: _markerPosition.longitude,
              child: Container(
                width: 130,
                height: 45,
                color: Colors.blue,
                padding: const EdgeInsets.all(5),
                child: Text(
                    _data[_tappedIndex].continent + '\n' +
                      _data[_tappedIndex].area,
                      style: TextStyle(
                        color: Colors.white,
                        fontSize: Theme
                            .of(context)
                            .textTheme
                            .bodyText2!
                            .fontSize),
                ),
              ),
            );
          },
        ),
      ],
    ),
  );
}
 
class Model {
  const Model(this.continent, this.area);
  final String continent;
  final String area;
}

 

Output:

Syncfusion Flutter Maps - Showing tooltip at tapped position

 

Check the below links for more features in Syncfusion® Flutter Maps,

Live samples,

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied