ios - 访问 MKLocalSearch 响应项(快速)

标签 ios swift mklocalsearch

当我执行 println(localSearchResponse) 时,我得到一个 MapItem 对象,其中包含大量有关位置的详细信息。在这个例子中,它是 UCSD。这是我的日志中显示的输出。

<MKLocalSearchResponse: 0x1c53d640> {
    boundingRegion = "<center:+32.87514836, -117.23958822 span:+0.00725621, +0.00825332>";
    mapItems =     (
        "<MKMapItem: 0x1c538090> {\n    isCurrentLocation = 0;\n    name = \"University of California, San Diego\";\n    phoneNumber = \"+18585342230\";\n    placemark = \"University of California, San Diego, 9500 Gilman Dr, La Jolla, CA  92093-5004, United States @ <+32.87529400,-117.23961000> +/- 0.00m, region CLCircularRegion (identifier:'<+32.87514837,-117.23958825> radius 557.57', center:<+32.87514837,-117.23958825>, radius:557.57m)\";\n    url = \"http://www.ucsd.edu\";\n}"
    );
}

注意它如何输出 placemark = University of California... 并有地址吗?如何获取这个值并将其存储到变量中?这是我的代码:

localSearchRequest = MKLocalSearchRequest()
localSearchRequest.naturalLanguageQuery = addressTextField.text
localSearch = MKLocalSearch(request: localSearchRequest)
localSearch.startWithCompletionHandler { (localSearchResponse, error) -> Void in

    if localSearchResponse == nil{
        var alert = UIAlertView(title: nil, message: "Place not found", delegate: self, cancelButtonTitle: "Try again")
        alert.show()
        return
    }

    //prints the MKLocalSearchResponse with name, phoneNumber, placemark
    println(localSearchResponse)

    //Get latitude and longitude
    var newRecordLat = localSearchResponse.boundingRegion.center.latitude
    var newRecordLong = localSearchResponse.boundingRegion.center.longitude

    //How do I get the address, which is "placemark" in the MKLocalSearchResponse?
    var newRecordAddress = localSearchResponse.mapItems...???

    //store values to Parse
    self.latToParse = newRecordLat
    self.longToParse = newRecordLong
}

这是 MKSearchResponse 的文档

这是 MKMapItem 的文档

最佳答案

答案是:

var newRecordAddress = (localSearchResponse.mapItems[0] as! MKMapItem).placemark

此对象包含您需要的所有信息。在演示项目中检查了它

仅地址:

var newRecordAddress = (localSearchResponse.mapItems[0] as! MKMapItem).placemark
let addressOnly = newRecordAddress.name + ", " + newRecordAddress.title

newRecordAddress.name 是地名 newRecordAddress.title 是你需要的地址

关于ios - 访问 MKLocalSearch 响应项(快速),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31488686/

相关文章:

swift - 为什么 MKLocalSearch for "nearby restaurants"返回的位置在不同的城市?

ios - 没有 MKMapView 的 MKLocalSearch

ios - UITableViewCell 中的 UITextView 防止 segue

android - 移动应用程序可以模糊屏幕吗?

ios - 如何从 iOS 应用程序使用 POST 发送 NSData?

swift - UICollectionView 单元格不出现

swift - 向下转换后附加到字典中的数组时出现不可变值错误

ios - NSNumberFormatter 分组

iphone - NSXMLParser 在无效 XML 上崩溃

ios - 如何使用 MKLocalSearch 请求获取附近的地点、照片