MapView Initialization
Despite the editions differences SDKs have similar general steps, required to execute SDK MapView initialization in application:
- [ ] Acquire credentials.
- [ ] Setup credentials.
- [ ] Add the MapView using Storyboard (or programmatically).
- [ ] Configure MapView.
Sample code for Navigate Edition MapView
initialization can be found at GitHub
Acquire credentials
The Navigate Edition credentials can be used for any application id, while for Premium Edition each app requires a unique set of credentials generated per specific application bundle id. New credentials should be requested for migration, because existed for Premium Edition will not work in Navigate Edition and vice versa. Please contact your sales representative for credentials request.
Setup credentials
The similarity between editions is that SDK initialization code should be set at the earliest moment of application launch, before any call of HERE SDK API.
The Premium Edition setup code has SDK module import and API call for programmatic authentication.
import NMAKit
let credentials = (
appId: "{YOUR_APP_ID}",
appCode: "{YOUR_APP_CODE}",
licenseKey: "{YOUR_LICENSE_KEY}"
)
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
NMAApplicationContext.setAppId(credentials.appId, appCode: credentials.appCode, licenseKey: credentials.licenseKey)
return true
}
The Premium Edition NMAApplicationContext.setAppId(: appcode: licenseKey:)
method call can be implemented in Navigate Edition using next code block:
let sdkOptions = SDKOptions(accessKeyId:id,
accessKeySecret:secret,
cachePath: cachePath)
let sdkNativeEngine = SDKNativeEngine(options: sdkOptions)
In addition to setup credentials programmatically the Navigate Edition gives an application developer option for fast credentials setup using Info.plist
, which can be used for internal purpose.
Add the MapView using Storyboard (or programmatically)
To add MapView
using Storyboard developer need to add UIView
object. Then configure this UIView
object inside the Interface Builder window according to SDK edition.
- For Premium Edition set Class
NMAMapView
. - For Navigate Edition set Class
MapView
and Module heresdk
.
To add MapView
programmatically:
1) Generate the MapView instance using ( NMAMapView(frame:)
method for Premium OR MapView(frame:)
method for Navigate Edition ) with non-zero frame.
2) Add instantiated MapView object on parentView using parentView.addSubview(mapView)
called.
After MapView
is instantiated it can be configured to display the map area. Example of code to display the map at specific location are listed below.
Code for Premium Edition:
import NMAKit
class ViewController: UIViewController, NMAMapViewDelegate {
@IBOutlet weak var mapView: NMAMapView!
override func viewDidLoad() {
super.viewDidLoad()
let geoCenter = NMAGeoCoordinates.init(latitude: 52.518043, longitude: 13.405991)
mapView.delegate = self
mapView.set(geoCenter: geoCenter, animation: .none)
}
func mapViewDidDraw(_ mapView: NMAMapView) {
print("Callback: MapView \(mapView) was drawn")
}
}
The Premium Edition code sets the delegate
and uses delegate method mapViewDidDraw:
, while Navigate Edition uses completion handlers for actions that occurs one time and delegate for reoccurring events. The Navigate Edition code for MapView load has MapError
, which helps to identify the initialization issue. Also, the Navigate Edition MapView
has grouped and encapsulated functionality into new classes MapScene
, Camera
, Gestures
. To show the area on MapView
you need to call loadScene: completion:
first. When the scene is ready you can adjust camera view point. The example of MapView
configuration is listed below:
@IBOutlet weak var mapView: MapView!
override func viewDidLoad() {
super.viewDidLoad()
mapView.mapScene.loadScene(mapScheme: MapScheme.normalDay, completion: onLoadScene)
}
private func onLoadScene(mapError: MapError?) {
guard mapError == nil else {
print("Error: Map scene not loaded, \(String(describing: mapError))")
return
}
let camera = mapView.camera
let distanceInMeters = MapMeasure(kind: .distance, value: 1000 * 10)
camera.lookAt(point: GeoCoordinates(latitude: 52.518043, longitude: 13.405991),
zoom: distanceInMeters)
}
Now, when you implemented all these steps - build and run the application to see the map on screen.