nickcoding
nickcoding

Reputation: 485

Swift: HEREMaps geocoding sharedSdkEngineNotInstantiated error being thrown

So I'm using the HERE iOS SDK. For some reason, the try block isn't working, I keep getting the error printing out that is 'sharedSdkEngineNotInstantiated'. The getCoordinates function is what I'm trying to test after instantiation--I included it so that you guys aren't left guessing what the end goal is. Thanks for any and all help!

class functions: NSObject, ObservableObject {
    var searchEngine: SearchEngine?

    override init() {
        do {
            try searchEngine = SearchEngine()
        } catch let engineInstantiationError {
            print("Failed to make the search engine. The cause was \(engineInstantiationError)")
        }
    }

    func getCoordinates5(from address: String) {
        guard searchEngine != nil else {
            return
        }
        let searchOptions = SearchOptions(maxItems: 10)
    
        let query = TextQuery(address, near: GeoCoordinates(latitude: 40.7128, longitude: 74.0060))
        _ = searchEngine!.search(textQuery: query, options: searchOptions) { (searchError, searchResultItems) in
            guard searchResultItems != nil else {
                return
            }
            for index in 0..<searchResultItems!.count {
                let number = index + 1
                print("Location \(number): \(searchResultItems![index])")
            }
        }
    }
}

Upvotes: 0

Views: 161

Answers (1)

Daniele Tavernelli
Daniele Tavernelli

Reputation: 144

As explained here, you should instantiate the sdk manually since probably when your code is executed you don't have any map loaded yet (showing the map will automatically instantiate the sdk). So all you need is

  // We must explicitly initialize the HERE SDK if no MapView is present.
do {
    try SDKInitializer.initializeIfNecessary()
} catch {
    fatalError("Failed to initialize HERE SDK")
}

before instantiating the SearchEngine

Upvotes: 1

Related Questions