Skip to content

Latest commit

 

History

History
 
 

ios

Open Swiss Maps SDK




iOS

Installation

Open Mobile Maps is available through Swift Package Manager.

For App integration within Xcode, add this package to your App target. To do this, follow the step by step tutorial Adding Package Dependencies to Your App and add the package with the url:

https://github.com/geoadmin/lib-open-swiss-maps-sdk.git

Swift Package

Once you have your Swift package set up, adding Open Mobile Maps as a dependency is as easy as adding it to the dependencies value of your Package.swift.

dependencies: [
    .package(url: "https://github.com/geoadmin/lib-open-swiss-maps-sdk.git", .upToNextMajor(from: "2.0.0"))
]

How To use

Display Swisstopo Map in a ViewController

The SDK provides a the view SwisstopoMapView which is a UIView subclass. By default the ch.swisstopo.pixelkarte-farbelayer is displayed.

import UIKit
import SwisstopoMapSDK

class MapViewController: UIViewController {
  lazy var mapView = SwisstopoMapView()
  override func loadView() {
    view = mapView
  }
}
Initializing the view with a different baselayer type
lazy var mapView = SwisstopoMapView(baseLayerType: .SWISSIMAGE)
Changing the baselayer type after initializing the view
mapView.setBaseLayerType(type: .PIXELKARTE_GRAUSTUFEN)
Adding multiple layer
let drohnenLayer = mapView.addSwisstopoLayer(type: .DROHNEN)
drohnenLayer.setAlpha(0.25)

See available layers for a list of all supported layers.

Adding layer using WMTS identifier

There are numerous additional layer types available via the Swisstopo wmts service. They can be added similarly to the predefined SwisstopoLayerTypes.

let neophytenLayer = mapView.addSwisstopoLayer(identifier: "ch.bafu.neophyten-haargurke")

Browse the Geocatalog to find available data.

Adjusting the camera

The camera position can easily be adjusted by manipulating the Camera2dInterface received from the map. E.g. to set a custom location:

mapView.camera.move(toCenterPosition: .init(systemIdentifier: MCCoordinateSystemIdentifiers.epsg4326(),
                                                x: 8.378232525377973,
                                                y: 46.962592372639634,
                                                z: 0), animated: true)

One can also change the default values for the camera's bounds and it's zoom range:

mapView.camera.setBounds(MCRectCoord(topLeft: MCCoord(systemIdentifier: MCCoordinateSystemIdentifiers.epsg2056(), x: 2485071.58, y: 1299941.79, z: 0.0),
                                     bottomRight: MCCoord(systemIdentifier: MCCoordinateSystemIdentifiers.epsg2056(), x: 2828515.82, y: 1075346.31, z: 0.0)))
mapView.camera.setMinZoom(5000000.0)
mapView.camera.setMaxZoom(250.0)
Reacting to clicks on a layer
import UIKit
import SwisstopoMapSDK
import MapCoreSharedModule

class MapViewController: UIViewController {
  lazy var mapView = SwisstopoMapView(baseLayerType: .SWISSIMAGE)
  
  override func loadView() {
    view = mapView
  }
  
  override func viewDidLoad() {
    super.viewDidLoad()
    mapView.baseLayer.setCallbackHandler(self)
  }
}

extension MapViewController: MCTiled2dMapRasterLayerCallbackInterface {
  func onClickConfirmed(_ coord: MCCoord) -> Bool {
    // react to click
    // return true if interaction was consumend
    return true
  }
  func onLongPress(_ coord: MCCoord) -> Bool {
    // react to longpress
    // return true if interaction was consumend
    return true
  }
}

Adding a gps layer

A gps layer can conveniently be added to the SwisstopoMapView by calling:

mapView.addGpsLayer()
Style

The layer can be visually customized at creation time by also supplying a MCGpsStyleInfo:

public extension MCGpsStyleInfo {
    static var customStyle: MCGpsStyleInfo {
        guard let pointImage = UIImage(named: "ic_gps_point", in: .module, compatibleWith: nil),
              let pointCgImage = pointImage.cgImage,
              let pointTexture = try? TextureHolder(pointCgImage),
              let headingImage = UIImage(named: "ic_gps_direction", in: .module, compatibleWith: nil),
              let headingCgImage = headingImage.cgImage,
              let headingTexture = try? TextureHolder(headingCgImage) else {
                  fatalError("gps style assets not found")
              }

        return MCGpsStyleInfo(pointTexture: pointTexture,
                       headingTexture: headingTexture,
                              accuracyColor:  UIColor(red: 112/255,
                                                      green: 173/255,
                                                      blue: 204/255,
                                                      alpha: 0.2).mapCoreColor)
    }
}
Modes and Heading

The GpsLayer has four different modes to react to location updates:

DISABLED: No gps indicator is rendered on the map.

STANDARD: The indicator is drawn at the current location, along with the current heading (if enabled and a texture is provided)

FOLLOW: In addition to the same behavior as STANDARD, upon a location update, the camera is animated to keep the indicators position centered in the map.

FOLLOW_AND_TURN: While following the indicators location updates (as in FOLLOW), the camera is rotated to have the map oriented in the direction of the current heading.

How to build

Build Instructions