ViewportState class sealed
A base class representing different ways to position the camera on a map.
The ViewportState class defines various strategies for controlling the camera's position, orientation, and behavior within a map widget. By selecting a specific viewport state, you can customize how the camera responds to user interactions or focuses on particular areas of interest.
Supported Viewport States
-
Default Style Viewport: Sets the camera to the parameters defined in the map style's root property. This is the default state when no other viewport is specified.
-
Camera Viewport: Allows you to directly set the camera's position using parameters like
center
coordinate,zoom
,bearing
,pitch
, andanchor
. -
Overview Viewport: Focuses the camera on a specified geometry with the minimum zoom level needed to display it entirely. This is useful for directing the user's attention to a route line or area of interest.
-
Follow Puck Viewport: Automatically tracks the user's current position on the map, keeping the location indicator (puck) centered or in a specified position.
-
Idle Viewport: Activated when the user interacts with the map (e.g., pan or zoom gestures). You can also set it to interrupt ongoing viewport transition animations.
Setting the Viewport
You can set the viewport state via the constructor parameter of the MapWidget
:
final disneyland = Point(coordinates: Position(-117.918976, 33.812092));
MapWidget(
viewport: CameraViewportState(center: disneyland),
);
Updating the Viewport
You can update the viewport state at any time, with or without animations, to change how the camera behaves in response to user interactions or application events. Here's how you might implement this in a stateful widget:
class MyWidgetState extends State<SimpleMapExample> {
ViewportState _viewport = CameraViewportState(center: disneyland);
@override
Widget build(BuildContext context) {
return Column(children: [
MapWidget(viewport: _viewport),
TextButton(
onPressed: () {
setState(() {
_viewport = CameraViewportState(
center: Point(coordinates: Position(48.868, 2.782)),
zoom: 12,
);
});
},
child: Text("Jump to Paris Disneyland"),
),
TextButton(
onPressed: () {
setStateWithViewportAnimation(() {
_viewport = FollowPuckViewportState(
zoom: 16,
bearing: FollowPuckViewportStateBearingHeading(),
pitch: 60,
);
});
},
child: Text("Animate to User"),
),
]);
}
}
See Also
- CameraViewportState: For directly controlling the camera's position.
- OverviewViewportState: For focusing on specific geometries.
- FollowPuckViewportState: For tracking the user's location.
- IdleViewportState: For handling user interactions.
Note: The ViewportState is a sealed class; use one of its subclasses to define the desired camera behavior.
By leveraging different viewport states, you can create dynamic and responsive map experiences tailored to your application's needs.
Properties
- hashCode → int
-
The hash code for this object.
no setterinherited
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
Methods
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
toString(
) → String -
A string representation of this object.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited