Get started

Search

SearchControl is a search box over Mountaya's unified search, with its results under it. Handed your map, it asks the Data API through the map's client and moves the camera onto a pick.

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});

  // A client whose session token the Data API takes.
  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: [
      MapView(engine: map),
      Positioned(left: 16, top: 16, right: 16, child: SearchControl(map: map)),
    ],
  );
}

Search calls the Data API, which needs a session token. Hand the client a SessionTokenEngine, as Authentication in the SDK shows.

Configure the search

import 'package:flutter/widgets.dart';
import 'package:mountaya_client/data_schema.dart' as data_schema;
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});

  // A client whose session token the Data API takes.
  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: [
      MapView(engine: map),
      Positioned(
        left: 16,
        top: 16,
        right: 16,
        child: SearchControl(
          map: map,
          // Places, itineraries and zones, in that order, five of each, in French.
          targets: const [
            data_schema.SearchTarget.places,
            data_schema.SearchTarget.itineraries,
            data_schema.SearchTarget.zones,
          ],
          language: data_schema.Language.fr,
          limit: SearchLimit.of(5),
        ),
      ),
    ],
  );
}

Handle a pick

onSelected takes a pick over, for an app that routes it its own way:

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});

  // A client whose session token the Data API takes.
  final Mountaya mountaya;

  @override
  State<TrailMap> createState() => _TrailMapState();
}

class _TrailMapState extends State<TrailMap> {
  late final MapEngine map = MapEngine(widget.mountaya);
  String? _opened;

  @override
  void dispose() {
    map.close();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) => Stack(
    children: [
      MapView(engine: map),
      Positioned(
        left: 16,
        top: 16,
        right: 16,
        child: SearchControl(
          map: map,
          onSelected: (row) {
            setState(() => _opened = row.title);
            // The highlight the control would draw, drawn by you.
            map.setSearchHighlight(pickTarget(row.payload).mark);
          },
        ),
      ),
      if (_opened case final opened?) Positioned(bottom: 16, left: 16, child: Text(opened)),
    ],
  );
}

Build your own search box

SearchEngine is the search under the control, with no widget. Build your own list on its snapshots:

import 'package:mountaya_client/data_schema.dart' as data_schema;
import 'package:mountaya_client/mountaya_client.dart';
import 'package:mountaya_geo/mountaya_geo.dart';

const publishableKey = String.fromEnvironment('MOUNTAYA_PUBLISHABLE_KEY');
const sessionToken = String.fromEnvironment('MOUNTAYA_SESSION_TOKEN');

final mountaya = Mountaya(
  credential: .publishable(key: publishableKey, sessionToken: const .value(sessionToken)),
);
final search = SearchEngine(
  configuration: SearchConfiguration(
    fetch: (input, signal) => mountaya.data.search(
      input: input,
      options: RequestOptions(signal: signal),
    ),
    language: data_schema.Language.fr,
    targets: const [
      data_schema.SearchTarget.places,
      data_schema.SearchTarget.itineraries,
      data_schema.SearchTarget.zones,
    ],
    limit: SearchLimit.of(5),
  ),
);
search.setQuery('refuge');
final settled = await search.subscribe().firstWhere((state) => state.isSettled);
for (final row in searchListState(settled, SearchFilter.places).rows) {
  print(row.title);
}
search.close();
await mountaya.close();

Next steps

Last updated