ramo
ramo

Reputation: 619

Here Maps iOS SDK issue (Swift3, Xcode 8) has a nil currentPosition

I am using Here Maps SDK in my project in Swift 3 (in Xcode 8 for iOS 10) to grab the currentPosition from NMAPositioningManager. However, while isActive is true for the NMAPositioningManager, the currentPosition is always nil. I have a valid position and valid credentials H(ERE_MAPS_APPID, HERE_MAPS_APP_CODE, and HERE_MAPS_LICENSE_KEY).

I checked the SDKDemo with Xcode 8 for iOS 10, which runs fine, but their code is in Objective-C. Any comment would be appreciated.

if let posMan : NMAPositioningManager = NMAPositioningManager.shared() {
            if let currPos = posMan.currentPosition{
                if let geoCoordCenter = currPos.coordinates {
                    self.mapView.setGeoCenter(geoCoordCenter, with: NMAMapAnimation.none)
                }
            }
        }

Upvotes: 1

Views: 280

Answers (1)

Marco
Marco

Reputation: 2190

Make sure positioning is started (I guess you did, but it's not shown in your snipped, so pasting it here again for reference):

NMAPositioningManager.shared().startPositioning()

Then normally it takes some time till your position is available, so you can't query dirctly PositionManager. I suggest to register for the position update callbacks:

NotificationCenter.default.addObserver(self, selector: #selector(ViewController.positionDidUpdate), name: NSNotification.Name.NMAPositioningManagerDidUpdatePosition, object: NMAPositioningManager.shared())
NotificationCenter.default.addObserver(self, selector: #selector(ViewController.didLosePosition), name: NSNotification.Name.NMAPositioningManagerDidLosePosition, object: NMAPositioningManager.shared())

Then do in the callback with the position whatever you want:

func positionDidUpdate(){
        if let p = NMAPositioningManager.shared().currentPosition {
            mapView.setGeoCenter(p.coordinates, with: NMAMapAnimation.bow)
        }
}

func didLosePosition(){
    print("Position lost")
}

Let me know if it's still not working for you like this, and I'll add more complete code.

Upvotes: 4

Related Questions