Woosmap for Flutter - Advanced Markers and InfoWindow
Draw advanced markers with a styled pin or your own HTML, anchor them precisely, and attach an InfoWindow. Added in version 2.9 of the Woosmap Map-Js Flutter plugin.
Version 2.9 of the plugin adds AdvancedMarker, a binding for the Maps JS AdvancedMarkerElement, and InfoWindow.
AdvancedMarker is a separate class from Marker because the two wrap unrelated JavaScript types. It takes AdvancedMarkerOptions, emits a smaller set of events, and has no icon, label, opacity, rotation or alignment of its own — its visual is a DOM node, which you supply either as a styled pin or as HTML. What it adds over the classic marker is that HTML visual, precise anchoring through anchorLeft and anchorTop, and altitude — it is the only marker that reads LatLng.altitude.
Checking that advanced markers are available
Advanced markers need the marker library, which the plugin loads for you. Check the capability before relying on them, because a map that lacks it will not build the element.
final MapCapabilities capabilities = await _controller!.getMapCapabilities();
if (!capabilities.isAdvancedMarkersAvailable) {
return;
}
Adding an advanced marker
Create an AdvancedMarker with its options and call add(). Without a pin or content it draws the SDK’s default pin.
final AdvancedMarker marker = AdvancedMarker.create(
AdvancedMarkerOptions(
position: LatLng(lat: 51.52, lng: -0.13),
title: 'Bloomsbury',
),
_controller!,
click: (dynamic value) {
debugPrint('advanced marker clicked');
},
);
marker.add();
An advanced marker emits click, mouseover, mouseout, dragstart, drag and dragend. Every callback is invoked with null rather than an event object: the SDK calls its listeners with no arguments, so there is nothing for the bridge to forward.
Read what you need from the marker itself. Declare it late final and assign it on the next line — writing final marker = AdvancedMarker.create(... marker ...) does not compile, because marker would be read inside its own initializer.
late final AdvancedMarker marker;
marker = AdvancedMarker.create(
AdvancedMarkerOptions(
position: LatLng(lat: 51.52, lng: -0.13),
draggable: true,
),
_controller!,
dragend: (_) async {
final LatLng? position = await marker.getPosition();
debugPrint('dropped at ${position?.lat}, ${position?.lng}');
},
);
marker.add();
getPosition() returns null when the map has no position to report — for a marker that has been removed, say. Note also that dragging rewrites the position from the pointer, which carries no altitude, so a dragged marker drops back to ground level.
click only fires while clickable is true, which is the plugin’s default.
Styling the pin with PinOptions
PinOptions is the advanced marker’s equivalent of an icon. Every field is a plain value, so no markup crosses the bridge.
final AdvancedMarker marker = AdvancedMarker.create(
AdvancedMarkerOptions(position: LatLng(lat: 51.52, lng: -0.13)),
_controller!,
pin: PinOptions(
background: '#f50057',
borderColor: '#B31412',
glyphColor: '#FFFFFF',
glyphText: '1',
scale: 1.2,
),
);
marker.add();
glyphText and glyphSrc cover a text or image glyph. The upstream glyph option, which also accepts an arbitrary DOM element, is not exposed — use content below when you need a fully custom visual.
Using your own HTML as the marker
content replaces the pin with markup of your own.
final AdvancedMarker marker = AdvancedMarker.create(
AdvancedMarkerOptions(position: LatLng(lat: 51.52, lng: -0.13)),
_controller!,
content: '<img src="https://images.woosmap.com/marker-red.png" '
'width="24" height="34" alt="store">'
'<b>Store</b>',
);
marker.add();
// Replace it later, and read back what the map kept.
await marker.setContent('<b>Closed today</b>');
final String markup = await marker.getContent();
The markup is sanitised before it is displayed. The element takes a DOM node rather than a string, so the plugin parses your HTML in the map document and cleans it first, against the same allowlist InfoWindowOptions.content uses. What survives is text formatting, headings, lists, tables, a and img. Every on* handler is stripped, href and src keep only http, https schemes, and anything else — script, style, svg, iframe, custom elements — is dropped whole.
Two consequences worth planning for:
- The allowlist has no
styleattribute, and the map document carries no stylesheet of yours, so aclasshas nothing to bind to. Size the content through the markup itself, as theimgabove does. getContent()returns the sanitised markup, not the string you passed in.
content and pin both set the element’s visual and are mutually exclusive. Passing both throws an ArgumentError rather than letting one silently overwrite the other.
Anchoring the marker to its point
anchorLeft and anchorTop move the marker relative to the position it marks. They are CSS lengths or percentages, because the element renders at translate(anchorLeft, anchorTop).
final AdvancedMarker marker = AdvancedMarker.create(
AdvancedMarkerOptions(
position: LatLng(lat: 51.52, lng: -0.13),
anchorLeft: '-50%',
anchorTop: '-50%',
),
_controller!,
);
marker.add();
// Both can be changed afterwards; the element redraws on assignment.
await marker.setAnchorLeft('calc(-50% + 10px)');
await marker.setAnchorTop(null); // back to the SDK default
Left unset, the SDK uses -50% and -100%, which rests the marker’s bottom edge on the point like a pin on its tip. Use -50% for anchorTop to centre it on the point instead.
A value that is not a valid CSS length, percentage or calc() is rejected in Dart with an ArgumentError. That is deliberate: the element would otherwise drop its whole transform and park the marker in the map’s corner with nothing logged.
Placing a marker above the ground
LatLng takes an optional altitude, in metres. Only AdvancedMarker reads it, and only on a tilted map — the classic Marker drops it.
final AdvancedMarker marker = AdvancedMarker.create(
AdvancedMarkerOptions(
position: LatLng(lat: 51.52, lng: -0.13, altitude: 500),
),
_controller!,
);
marker.add();
await _controller!.moveCamera(tilt: 45, heading: 90, zoom: 14);
Viewed from directly above, a marker at altitude sits exactly where its ground-level twin does; tilt the camera and the two separate. Dragging a marker rewrites its position from the pointer, which carries no altitude, so a dragged marker drops back to ground level.
Collision behaviour
collisionBehavior is bound, with a setter and a getter.
await marker.setCollisionBehavior(
CollisionBehavior.requiredAndHidesOptional);
final CollisionBehavior? behaviour = await marker.getCollisionBehavior();
Note that the current SDK stores the value and never acts on it, so markers still draw over one another whatever you set. Reading it back is the only observable effect. Use zIndex for stacking that works.
Adding an InfoWindow
An InfoWindow is a floating panel anchored either to a marker — classic or advanced — or to a bare position.
final InfoWindow infoWindow = InfoWindow.create(
InfoWindowOptions(
content: '<b>Bloomsbury</b><p>Open until 18:00</p>',
maxWidth: 260,
),
_controller!,
);
await infoWindow.add();
await infoWindow.open(anchor: marker);
// Or open it at a position, with no marker to follow.
await infoWindow.open(position: LatLng(lat: 51.52, lng: -0.13));
await infoWindow.setContent('<b>Closed</b>');
await infoWindow.close();
InfoWindowOptions takes content, disableAutoPan, maxWidth and pixelOffset, and the window exposes open({anchor, position}), close(), setContent(), getContent(), setPosition(), setOffset() and remove().
Content is HTML and is sanitised in the map document before it is displayed. The SDK assigns it straight to innerHTML and sanitises nothing of its own, so the plugin cleans it first, immediately before that point. The allowlist is the one described under content above. Treat it as rich text rather than as a page: plain text is always safe, and anything outside the allowlist simply will not appear.
Two behaviours to know:
- A non-zero
pixelOffsetis not applied when the anchor is anAdvancedMarker. Anchor to a classicMarker, or open at a position, when the offset matters. - The SDK’s
closeclickanddomreadyevents are not bound yet.