View on GitHub

Change a map’s basemap. A basemap is beneath all layers on a map and is used to provide visual reference for the operational layers.

Image of set basemap

Use case

Basemaps should be selected contextually. For example, in maritime applications, it would be more appropriate to use a basemap of the world’s oceans as opposed to a basemap of the world’s streets.

How to use the sample

Tap the map button to show or hide the basemap gallery. Select a basemap in the gallery to apply it to the map.

How it works

  1. Create an ArcGISMap object with an initial basemap style.
  2. Set the map to the ArcGISMapViewController object.
  3. Create a BasemapGalleryController and set the map to the geoModel property.
  4. Create a BasemapGallery widget using the BasemapGalleryController.

Relevant API

  • ArcGISMap
  • ArcGISMapViewController
  • Basemap
  • BasemapGallery
  • BasemapGalleryController

Additional information

This sample uses the BasemapGallery toolkit component from the ArcGIS Maps SDK for Flutter Toolkit. The component supports selecting 2D and 3D basemaps from ArcGIS Online, a user-defined portal, or a collection of basemaps.

Tags

basemap, map

Sample code

set_basemap.dart
//
// 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 'package:arcgis_maps/arcgis_maps.dart';
import 'package:arcgis_maps_sdk_flutter_samples/common/common.dart';
import 'package:arcgis_maps_toolkit/arcgis_maps_toolkit.dart';
import 'package:flutter/material.dart';
class SetBasemap extends StatefulWidget {
const SetBasemap({super.key});
@override
State<SetBasemap> createState() => _SetBasemapState();
}
class _SetBasemapState extends State<SetBasemap> with SampleStateSupport {
// Create a controller for the map view and a map with an imagery basemap.
final _mapViewController = ArcGISMapView.createController();
final _arcGISMap = ArcGISMap.withBasemapStyle(BasemapStyle.arcGISImagery);
// Create a controller for the basemap gallery component.
late final BasemapGalleryController _basemapGalleryController;
// Track whether the basemap gallery panel is visible.
var _showBasemapGallery = false;
@override
void initState() {
super.initState();
// Configure the gallery to update the map's basemap automatically.
_basemapGalleryController = BasemapGalleryController()
..geoModel = _arcGISMap
..viewStyle = BasemapGalleryViewStyle.automatic;
}
@override
void dispose() {
_basemapGalleryController.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: LayoutBuilder(
builder: (context, constraints) {
// Use half of the available width for the gallery panel.
final galleryWidth = constraints.maxWidth / 2;
return Stack(
children: [
// Show the map in the background.
ArcGISMapView(
controllerProvider: () => _mapViewController,
onMapViewReady: onMapViewReady,
),
// Show the basemap gallery as an overlay panel.
if (_showBasemapGallery)
SafeArea(
child: Align(
alignment: Alignment.topRight,
child: Container(
width: galleryWidth,
margin: const EdgeInsets.fromLTRB(16, 16, 16, 80),
decoration: BoxDecoration(
color: Theme.of(context).colorScheme.surface,
border: Border.all(
color: Theme.of(context).colorScheme.outline,
),
borderRadius: BorderRadius.circular(8),
),
child: Theme(
data: Theme.of(context).copyWith(
textTheme: Theme.of(context).textTheme.copyWith(
bodySmall: Theme.of(
context,
).textTheme.bodySmall?.copyWith(fontSize: 11),
),
),
child: BasemapGallery(
controller: _basemapGalleryController,
),
),
),
),
),
],
);
},
),
// Toggle the basemap gallery panel.
floatingActionButton: FloatingActionButton(
onPressed: () =>
setState(() => _showBasemapGallery = !_showBasemapGallery),
shape: const RoundedRectangleBorder(),
child: const Icon(Icons.map),
),
);
}
void onMapViewReady() {
// Set the map's initial viewpoint.
_arcGISMap.initialViewpoint = Viewpoint.withLatLongScale(
latitude: 33.7,
longitude: -118.4,
scale: 1000000,
);
// Assign the map to the map view controller.
_mapViewController.arcGISMap = _arcGISMap;
}
}