# Creating Device Maps Source: https://www.nianticspatial.com/docs/nsdk/how-to/vps/creating_device_maps/?platform=swift # How to Create a Device Map > **Caution:** > > **Attention!** > > This feature is experimental and may not work as expected. For more information about it, see the [Device Mapping Feature page](https://www.nianticspatial.com/docs/nsdk/features/device_mapping/). Before you can use a device map in your AR application, you will need to create and save it. In this tutorial, we will go over how to create and save a device map in your Swift project, as well as some tips for how to get the most out of device mapping. For the full source code, please download the [NSDK Samples](https://github.com/nianticspatial/nsdk-samples-csharp) project. ## 1. Creating a Device Map Map construction will run while a `NsdkDeviceMappingSession` is active, and mapping data is automaticlly added to internal NSDK storage. An `NsdkMapStorage` instance can be used to interact with the storage system from your Swift app. ```swift mapStorage = nsdkSession.createMapStorage() mappingSession = nsdkSession.createDeviceMappingSession() mappingSession.startMapping() ``` ## 2. Check on Mapping Progress The time it takes to construct a valid map will vary depending on the environment and also the local device's processing power. The `NsdkMapStorage` class provides information about the state of the map being constructed. ```swift // Function call periodically private func checkForMapUpdates() { guard let buffer = mapStorage.mapUpdate() else { // No update available at this time, which is normal during mapping return } print("Received map update with \(buffer.dataSize) bytes") } ``` Once a map is valid, you will be able to place an anchor at its origin. ```swift if let rootAnchor = mappingSession.createRootAnchor() { // Can save the map now, or continue to grow the map } ``` ## Optional: Show the Mesh While Mapping When mapping larger areas, it can be useful to visualize the growing map's coverage and get a sense of where you still need to scan. Once a root anchor has been obtained, `NsdkMapStorage.extractMapMetadata(anchorPayload:map:)` can be used to obtain the map's feature points. In addition to the map buffers returned incrementally by `NsdkMapStorage.mapUpdate()` seen in the previous section, a buffer containing the entire map is returned by `NsdkMapStorage.mapData()`. ```swift do { let mapMetadata = try mapStorage.extractMapMetadata(anchorPayload: rootAnchor, map: buffer) guard let metadata = mapMetadata else { print("Failed to extract map metadata. Check logs for error.") return } // Visualize points described in mapMetadata.points } catch { print("Failed to extract map metadata. Error: \(error)") return } ``` Feature points are extracted relative to the pose of the anchor passed in for the `anchorPayload` argument. So in order to render the points on screen, you must first track the pose of that anchor. See the guide [Localizing and Tracking anchors with VPS](https://www.nianticspatial.com/docs/nsdk/how-to/vps/tracking_anchors_swift/#tracking-existing-anchors) for how to do that. Then get the location of feature points relative to that anchor: ```swift let points = mapMetadata.points for i in stride(from: 0, to: points.count, by: 3) { let center = SIMD3(points[i], points[i + 1], points[i + 2]) } ``` The visualization code can be found in full in the sample project linked at the top of this page. ## Device Mapping Tips - Because device mapping relies on a single scan, localization and tracking accuracy depend heavily on how well you scan the area. If you remember one tip, let it be this: **if you are having localization issues, don't be afraid to re-scan.** - Lighting matters a lot when scanning an area. If the lighting during localization doesn't match the scan, you will need to re-scan. (This includes both outdoor and indoor lighting.) - For best results, follow these guidelines when scanning: - Focus on distinctive objects in the area that are unlikely to move between scanning and localization, such as furniture, household fixtures, or statues. - Avoid looking primarily at flat surfaces without color variation, such as the ground, grass, walls, and floors. - Move around while scanning! The more viewpoints you see an object from, the better the map of it will be. ## Sharing the Map Serialized map data can be saved for use on the local device or even share with other devices. To get the map data: ```swift guard let buffer = mapStorage.mapData() else { print("Failed to get map from storage") return } ``` To add the map data to a fresh NSDK session: ```swift let mapData = try Data(contentsOf: mapDataURL) let mapBuffer = NsdkBuffer(data: mapData) do { try mapStorage.addMap(map: mapBuffer) } catch { print("Failed to add map to storage: \(error)") } ``` Once a device map has been added to storage, the `NsdkVpsSession` APIs can be used to create and track anchors to the map.