Display your current position on the map, as well as switch between different types of auto pan modes.
Use case
When using a map within a GIS, it may be helpful for a user to know their own location within a map, whether that's to aid the user's navigation or to provide an easy means of identifying/collecting geospatial information at their location.
How to use the sample
Tap the "Location Settings" button to open the settings. Toggle "Show Location" to turn on or off the location display. The "Auto-Pan Mode" menu has the following options to control how the viewpoint changes as the location changes:
- Off - Turns off
autoPanMode
. - Recenter - Sets the
autoPanMode
torecenter
. - Navigation - Sets the
autoPanMode
tonavigation
. - Compass - Sets the
autoPanMode
tocompassNavigation
.
How it works
- Create an
ArcGISMapViewController
. - Get the
LocationDisplay
object from thelocationDisplay
property on the controller. - Use
start()
andstop()
on theLocationDisplay
object as necessary.
Relevant API
- ArcGISMap
- ArcGISMapViewController
- LocationDisplay
- LocationDisplay.autoPanMode
Additional information
Location permissions are required for this sample.
Tags
compass, GPS, location, map, mobile, navigation
Sample Code
//
// Copyright 2024 Esri
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
import 'dart:async';
import 'dart:math';
import 'package:arcgis_maps/arcgis_maps.dart';
import 'package:flutter/material.dart';
import '../../utils/sample_state_support.dart';
class ShowDeviceLocation extends StatefulWidget {
const ShowDeviceLocation({super.key});
@override
State<ShowDeviceLocation> createState() => _ShowDeviceLocationState();
}
class _ShowDeviceLocationState extends State<ShowDeviceLocation>
with SampleStateSupport {
// Create a controller for the map view.
final _mapViewController = ArcGISMapView.createController();
// A flag for when the settings bottom sheet is visible.
var _settingsVisible = false;
// Create the system location data source.
final _locationDataSource = SystemLocationDataSource();
// A subscription to receive status changes of the location data source.
StreamSubscription? _statusSubscription;
var _status = LocationDataSourceStatus.stopped;
// A subscription to receive changes to the auto-pan mode.
StreamSubscription? _autoPanModeSubscription;
var _autoPanMode = LocationDisplayAutoPanMode.recenter;
// A flag for when the map view is ready and controls can be used.
var _ready = false;
@override
void dispose() {
// When exiting, stop the location data source and cancel subscriptions.
_locationDataSource.stop();
_statusSubscription?.cancel();
_autoPanModeSubscription?.cancel();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: SafeArea(
top: false,
child: Stack(
children: [
Column(
children: [
Expanded(
// Add a map view to the widget tree and set a controller.
child: ArcGISMapView(
controllerProvider: () => _mapViewController,
onMapViewReady: onMapViewReady,
),
),
Center(
child: ElevatedButton(
onPressed: _status == LocationDataSourceStatus.failedToStart
? null
: () => setState(() => _settingsVisible = true),
child: const Text('Location Settings'),
),
),
],
),
// Display a progress indicator and prevent interaction until state is ready.
Visibility(
visible: !_ready,
child: SizedBox.expand(
child: Container(
color: Colors.white30,
child: const Center(child: CircularProgressIndicator()),
),
),
),
],
),
),
// The Settings bottom sheet.
bottomSheet: _settingsVisible ? buildSettings(context) : null,
);
}
// The build method for the Geometry Settings bottom sheet.
Widget buildSettings(BuildContext context) {
return Container(
padding: EdgeInsets.fromLTRB(
20.0,
20.0,
20.0,
max(
20.0,
View.of(context).viewPadding.bottom /
View.of(context).devicePixelRatio,
),
),
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
Row(
children: [
Text(
'Location Settings',
style: Theme.of(context).textTheme.titleLarge,
),
const Spacer(),
IconButton(
icon: const Icon(Icons.close),
onPressed: () => setState(() => _settingsVisible = false),
),
],
),
Row(
children: [
const Text('Show Location'),
const Spacer(),
// A switch to start and stop the location data source.
Switch(
value: _status == LocationDataSourceStatus.started,
onChanged: (_) {
if (_status == LocationDataSourceStatus.started) {
_mapViewController.locationDisplay.stop();
} else {
_mapViewController.locationDisplay.start();
}
},
),
],
),
Row(
children: [
const Text('Auto-Pan Mode'),
const Spacer(),
// A dropdown button to select the auto-pan mode.
DropdownButton(
value: _autoPanMode,
onChanged: (value) {
_mapViewController.locationDisplay.autoPanMode = value!;
},
items: const [
DropdownMenuItem(
value: LocationDisplayAutoPanMode.off,
child: Text('Off'),
),
DropdownMenuItem(
value: LocationDisplayAutoPanMode.recenter,
child: Text('Recenter'),
),
DropdownMenuItem(
value: LocationDisplayAutoPanMode.navigation,
child: Text('Navigation'),
),
DropdownMenuItem(
value: LocationDisplayAutoPanMode.compassNavigation,
child: Text('Compass'),
),
],
),
],
),
],
),
);
}
void onMapViewReady() async {
// Create a map with the Navigation Night basemap style.
_mapViewController.arcGISMap =
ArcGISMap.withBasemapStyle(BasemapStyle.arcGISNavigationNight);
// Set the initial system location data source and auto-pan mode.
_mapViewController.locationDisplay.dataSource = _locationDataSource;
_mapViewController.locationDisplay.autoPanMode =
LocationDisplayAutoPanMode.recenter;
// Subscribe to status changes and changes to the auto-pan mode.
_statusSubscription = _locationDataSource.onStatusChanged.listen((status) {
setState(() => _status = status);
});
setState(() => _status = _locationDataSource.status);
_autoPanModeSubscription =
_mapViewController.locationDisplay.onAutoPanModeChanged.listen((mode) {
setState(() => _autoPanMode = mode);
});
setState(
() => _autoPanMode = _mapViewController.locationDisplay.autoPanMode,
);
// Attempt to start the location data source (this will prompt the user for permission).
try {
await _locationDataSource.start();
} on ArcGISException catch (e) {
if (mounted) {
showDialog(
context: context,
builder: (_) => AlertDialog(content: Text(e.message)),
);
}
}
// Set the ready state variable to true to enable the UI.
setState(() => _ready = true);
}
}