我是Here iOS SDK的新手,我正在尝试通过搜索位置周围的位置来使用places API。我做了一个pod try HEREMapsStarter并尝试了以下代码:

let places = NMAPlaces()
let location = NMAGeoCoordinates(latitude: yyy, longitude: xxx)

let result2 = places.makeSearchRequest(location: location, query: "restaurant")
let result = places.makeHereRequest(location: location, filters: nil)
result?.start(listener: self)
result2?.start(listener: self)


但这不起作用,因为result和result2均为零。我在这里想念什么?

最佳答案

似乎您正在尝试创建places对象:

let places = NMAPlaces()


但是places对象是Singleton,只能通过调用shared()来检索

let places = NMAPlaces.shared()


同样,在使用result?.start(listener: self)方法时,您需要实现NMAResultListener侦听器。简单侦听器的示例:

class MainViewController: UIViewController, NMAResultListener {

    func requestDidComplete(_ request: NMARequest, data: Any?, error inError: Error?) {
            print("data = \(String(describing: data))")
            guard inError == nil else {
                print ("Request error \((inError! as NSError).code)")
                return
            }
            guard data is NMADiscoveryPage, let resultPage = data as? NMADiscoveryPage else {
                print ("invalid type returned \(String(describing: data))")
                return
            }

            let resultsArray: [NMALink] = resultPage.discoveryResults;
            for link in resultsArray
            {
                if let placeLink = link as? NMAPlaceLink {
                    print("PlaceLink position: \(placeLink.position.latitude), \(placeLink.position.longitude)")
                }
            }
        }
....

}


================================================== ==================

假设您正在英国伦敦(51.514545,-0.131666)进行搜索,并如上所述使用NMAResultListener。在两个请求中,侦听器参数self如上所述实现NMAResultListener。

接下来可能是makeSearchRequest的代码:

let geoCoordCenter = NMAGeoCoordinates(latitude:51.514545, longitude: -0.131666)
let searchRequest = NMAPlaces.shared().makeSearchRequest(location: geoCoordCenter, query: "restaurant")
searchRequest?.start(listener: self)


请求完成后,makeSearchRequest将返回结果:

data = Optional(<NMADiscoveryPage: 0x28241a400>)
PlaceLink position: 51.5117, -0.12565
PlaceLink position: 51.51312, -0.13374
....
PlaceLink position: 51.51371, -0.13155
PlaceLink position: 51.51462, -0.12651


以及makeHereRequest的代码:

let geoCoordCenter = NMAGeoCoordinates(latitude:51.514545, longitude: -0.131666)
let hereRequest = NMAPlaces.shared().makeHereRequest(location: geoCoordCenter, filters: nil)
hereRequest?.start(listener: self)


makeHereRequest将返回结果:

data = Optional(<NMADiscoveryPage: 0x282400f00>)
PlaceLink position: 51.514542, -0.131883
PlaceLink position: 51.514542, -0.131883
....
PlaceLink position: 51.51435, -0.13169
PlaceLink position: 51.51444, -0.13194
PlaceLink position: 51.51444, -0.13194


另请注意,根据网络条件和搜索位置,也可能会出现错误结果,例如not found或其他错误。

关于ios - 在这里iOS SDK:Places API make * Request始终返回nil,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/56767998/

10-12 00:39