Map styles | Mobile SDK | 2GIS Documentation

Map styles

To work with map styles, you first need to create an IStyleFactory object by calling the makeStyleFactory() method.

let styleFactory = try sdk.makeStyleFactory()

To create an SDK-compatible map style, use the Export function in Style Editor and add the downloaded file to your project.

DGis.xcframework contains a file with default map styles. If your application will use a custom style, you can delete the default map styles file. To do this, delete the files DGis.xcframework/ios-arm64/DGis.framework/Assets/ru.dgis.sdk/common-styles.2gis and DGis.xcframework/ios-arm64_x86_64-simulator/DGis.framework/Assets/ru .dgis.sdk/common-styles.2gis.

To create a map with a custom style, you need to use the loadResource() or loadFile() method of IStyleFactory and specify the resulting object as the styleFuture map option.

// Create a style factory object.
let styleFactory = try sdk.makeStyleFactory()

// Set the map style in map settings.
var mapOptions = MapOptions.default
mapOptions.styleFuture = styleFactory.loadResource(name: "custom_style_file.2gis", bundle: .main)

// Create a map with the specified settings.
let mapFactory = try sdk.makeMapFactory(options: mapOptions)

The loadResource() and loadFile() methods return a deferred result (Future) so as not to delay the loading of the map. If the style has already been loaded (see the next section for more details), you can convert it into a Future object using the makeReadyValue() method.

var mapOptions = MapOptions.default
mapOptions.styleFuture = Future.makeReadyValue(style)

To change the style of an existing map, use the setStyle() method.

Unlike the styleFuture map option, setStyle() accepts a fully loaded Style object instead of a Future object. Therefore, setStyle() should be called after the Future has been resolved.

// Create a style factory object.
let styleFactory = try sdk.makeStyleFactory()

// Load a new map style. The loadFile() method only accepts the file:// URI scheme.
self.cancellable = styleFactory.loadFile(url: styleFileURL).sink(
	receiveValue: { [map = self.map] style in
		// After the style has been loaded, use it to change the current map style.
		map.setStyle(style: style)
	},
	failure: { error in
		print("Failed to load style from <\(fileURL)>. Error: \(error)")
	})

Each map style can contain several themes that you can switch between without having to load an additional style. You can specify which theme to use by setting the appearance map option when creating the map.

In iOS 13.0 and later, you can also use the automatic switching between light and dark themes (see Dark Mode).

// Map settings.
var mapOptions = MapOptions.default

// Name of the light theme.
let lightTheme: Theme = "day"

// Name of the dark theme.
let darkTheme: Theme = "night"

if #available(iOS 13.0, *) {
	// Automatically switch between the light and dark themes.
	mapOptions.appearance = .automatic(light: lightTheme, dark: darkTheme)
} else {
	// Use only the light theme.
	mapOptions.appearance = .universal(lightTheme)
}

// Create a map with the specified settings.
let mapFactory = sdk.makeMapFactory(options: mapOptions)

To change the theme after the map has been created, use the appearance property of IMapView:

// Get the map view.
let mapView = mapFactory.mapView

// Change the theme to dark.
mapView.appearance = .universal(darkTheme)