Show a map
MapEngine holds what the map shows, and MapView draws it with MapLibre. Your State makes the engine, hands it to the view, and closes it.
import 'dart:async';
import 'package:flutter/widgets.dart';
import 'package:mountaya_client/mountaya_client.dart';
import 'package:mountaya_elements_flutter/mountaya_elements_flutter.dart';
class TrailMap extends StatefulWidget {
const TrailMap({super.key, required this.mountaya});
final Mountaya mountaya;
@override
State<TrailMap> createState() => _TrailMapState();
}
class _TrailMapState extends State<TrailMap> {
late final MapEngine map = MapEngine(widget.mountaya);
@override
void initState() {
super.initState();
unawaited(map.setTerrain());
}
@override
void dispose() {
map.close();
super.dispose();
}
@override
Widget build(BuildContext context) => MapView(engine: map);
}setTerrain() shows the Tile API's first terrain. Every request the map makes carries your client's credential.
import 'package:mountaya_elements_flutter/mountaya_elements_flutter.dart';
import 'package:mountaya_geo/mountaya_geo.dart';
// Your State calls it from initState, on the engine it holds.
Future<void> showTour(MapEngine map) async {
await map.setTerrain();
// The camera frames the itinerary once it is drawn; a later call takes its place.
await map.setItinerary(
'0b6d1a4e-6d3f-4f7a-9c2e-1f0a5b8c3d21',
fit: const CameraFit(padding: PixelPadding.all(48)),
);
// Your zones, each at the rank its source layer declares.
await map.setZones(shown: true);
}import 'package:mountaya_elements_flutter/mountaya_elements_flutter.dart';
import 'package:mountaya_geo/mountaya_geo.dart';
void frameChamonix(MapEngine map) {
// A move states the members it changes; the bearing and the pitch are kept.
map.ease(const CameraTarget(center: [6.8712, 45.9231], zoom: 12));
// The box around Chamonix and Argentière: west, south, east, north.
map.fitBounds(
[6.80, 45.85, 7.00, 46.00],
const CameraFit(padding: PixelPadding.all(48)),
);
map.resetNorth();
}MapView hands you each tap, and the camera each time it comes to rest:
import 'dart:async';
import 'package:flutter/widgets.dart';
import 'package:mountaya_client/mountaya_client.dart';
import 'package:mountaya_elements_flutter/mountaya_elements_flutter.dart';
class TrailMap extends StatefulWidget {
const TrailMap({super.key, required this.mountaya});
final Mountaya mountaya;
@override
State<TrailMap> createState() => _TrailMapState();
}
class _TrailMapState extends State<TrailMap> {
late final MapEngine map = MapEngine(widget.mountaya);
late final StreamSubscription<MapEvent> _events;
String? _picked;
@override
void initState() {
super.initState();
// Every gesture, and the features each lands on, as a stream.
_events = map.events().listen((event) {
if (event case MapEventFeatureTap(:final features)) {
setState(() => _picked = features.firstOrNull?.layer);
}
});
}
@override
void dispose() {
unawaited(_events.cancel());
map.close();
super.dispose();
}
@override
Widget build(BuildContext context) => Stack(
children: [
MapView(
engine: map,
onTap: (tap) => setState(() => _picked = tap.marker),
// At rest only, after the reader moved it or you did.
onCameraChanged: (camera) => debugPrint('zoom ${camera.zoom}'),
),
if (_picked case final picked?) Text(picked),
],
);
}A refused key or a failed tile arrives on events(). Listen to it and print each:
import 'dart:async';
import 'package:flutter/widgets.dart';
import 'package:mountaya_client/mountaya_client.dart';
import 'package:mountaya_elements_flutter/mountaya_elements_flutter.dart';
class TrailMap extends StatefulWidget {
const TrailMap({super.key, required this.mountaya});
final Mountaya mountaya;
@override
State<TrailMap> createState() => _TrailMapState();
}
class _TrailMapState extends State<TrailMap> {
late final MapEngine map = MapEngine(widget.mountaya);
late final StreamSubscription<MapEvent> _errors;
@override
void initState() {
super.initState();
_errors = map.events().listen((event) {
switch (event) {
case MapEventAccessRefused(:final status, :final url):
debugPrint('Refused ($status) at $url');
case MapEventFailure(:final url, :final cause):
debugPrint('Could not load $url: $cause');
}
});
}
@override
void dispose() {
unawaited(_errors.cancel());
map.close();
super.dispose();
}
@override
Widget build(BuildContext context) => MapView(engine: map);
}A long press opens a readout of the point: what the map draws there, the terrain's altitude, slope and aspect, and the coordinates. Turn it off to show your own:
import 'dart:async';
import 'package:flutter/widgets.dart';
import 'package:mountaya_client/mountaya_client.dart';
import 'package:mountaya_elements_flutter/mountaya_elements_flutter.dart';
class TrailMap extends StatefulWidget {
const TrailMap({super.key, required this.mountaya});
final Mountaya mountaya;
@override
State<TrailMap> createState() => _TrailMapState();
}
class _TrailMapState extends State<TrailMap> {
late final MapEngine map = MapEngine(widget.mountaya);
late final StreamSubscription<MapEvent> _presses;
double? _ground;
@override
void initState() {
super.initState();
// The press is still told, and the ground's elevation is a Data API call away.
_presses = map.events().listen((event) async {
if (event case MapEventLongTap(:final tap)) {
final ground = await map.elevationAt(tap.position);
if (mounted) setState(() => _ground = ground);
}
});
}
@override
void dispose() {
unawaited(_presses.cancel());
map.close();
super.dispose();
}
@override
Widget build(BuildContext context) => Stack(
children: [
MapView(engine: map, readout: false),
if (_ground case final ground?) Text('${ground.round()} m'),
],
);
}The map draws the Mountaya watermark in the top-left corner of its safe area, unless your organization holds white label. Keep your own widgets clear of it:
import 'package:flutter/widgets.dart';
import 'package:mountaya_client/mountaya_client.dart';
import 'package:mountaya_elements_flutter/mountaya_elements_flutter.dart';
import 'package:mountaya_geo/mountaya_geo.dart';
class TrailMap extends StatefulWidget {
const TrailMap({super.key, required this.mountaya});
final Mountaya mountaya;
@override
State<TrailMap> createState() => _TrailMapState();
}
class _TrailMapState extends State<TrailMap> {
late final MapEngine map = MapEngine(widget.mountaya);
@override
void dispose() {
map.close();
super.dispose();
}
@override
Widget build(BuildContext context) => Stack(
children: [
Positioned.fill(child: MapView(engine: map)),
SafeArea(
child: Padding(
// Starts below the watermark's frame.
padding: EdgeInsets.fromLTRB(12, watermarkClearance + 8, 12, 12),
child: SearchControl(map: map),
),
),
],
);
}The credit button opens what the map credits. Each source's attribution is listed there.
A waypoint edit places a waypoint where the reader taps. Then save it with the Map Studio API:
import 'package:mountaya_client/mountaya_client.dart';
import 'package:mountaya_client/rest_schema.dart' as rest_schema;
import 'package:mountaya_elements_flutter/mountaya_elements_flutter.dart';
const itineraryId = '0b6d1a4e-6d3f-4f7a-9c2e-1f0a5b8c3d21';
const waypointId = '5f2b8c1e-7a4d-4e9b-b3c6-0d8e1f2a4b67';
// Each tap on MapView places the waypoint until the edit ends.
void moveWaypoint(MapEngine map) => map.beginWaypointEdit(waypointId);
// Ends the edit, and saves the waypoint where the reader placed it.
Future<void> saveWaypoint(MapEngine map, Mountaya mountaya) async {
final position = map.endWaypointEdit()?.position;
if (position == null) return;
await mountaya.studio.itineraries.patchItineraryWaypoint(
itinerary_id: itineraryId,
waypoint_id: waypointId,
body: rest_schema.WaypointUpdateParams(geojson: GeoJsonPoint(position)),
);
}