【发布时间】:2018-03-19 12:57:41
【问题描述】:
我正在使用适用于 iOS 的 Instagram API。我想按名称搜索位置,就像在 Instagram 应用程序本身中一样,但唯一可用于搜索位置的端点是 LAT 和 LONG。
有没有办法让它像 Instagram 应用程序本身一样,通过输入名称来搜索位置?
谢谢!
【问题讨论】:
标签: ios swift location instagram-api
我正在使用适用于 iOS 的 Instagram API。我想按名称搜索位置,就像在 Instagram 应用程序本身中一样,但唯一可用于搜索位置的端点是 LAT 和 LONG。
有没有办法让它像 Instagram 应用程序本身一样,通过输入名称来搜索位置?
谢谢!
【问题讨论】:
标签: ios swift location instagram-api
我做过类似的事情,但我必须自己编写代码。我使用searchBar 获取位置并将位置作为annotation 固定到MapView 我使用annotation 坐标来执行搜索。我故意显示地图和图钉。这是我使用的代码。
// do search for locations
localSearchRequest = MKLocalSearchRequest()
localSearchRequest.naturalLanguageQuery = searchBar.text
localSearch = MKLocalSearch(request: localSearchRequest)
localSearch.start { (localSearchResponse, error) -> Void in
// alert if location not found
if localSearchResponse == nil{
let alertController = UIAlertController(title: nil, message: "Place Not Found", preferredStyle: UIAlertControllerStyle.alert)
alertController.addAction(UIAlertAction(title: "Dismiss", style: UIAlertActionStyle.default, handler: nil))
self.present(alertController, animated: true, completion: nil)
activityIndicator.hide()
return
}
// append annotation if result is found
self.pointAnnotation = MKPointAnnotation()
self.pointAnnotation.title = searchBar.text
// get pin location
self.pointAnnotation.coordinate = CLLocationCoordinate2D(latitude: localSearchResponse!.boundingRegion.center.latitude, longitude: localSearchResponse!.boundingRegion.center.longitude)
self.pinAnnotationView = MKPinAnnotationView(annotation: self.pointAnnotation, reuseIdentifier: nil)
self.mapView.centerCoordinate = self.pointAnnotation.coordinate
self.mapView.addAnnotation(self.pinAnnotationView.annotation!)
如果您不打算使用mapView,您仍然可以使用此代码来获取纬度和经度位置
let location = CLLocationCoordinate2D(latitude:
localSearchResponse!.boundingRegion.center.latitude,
longitude: localSearchResponse!.boundingRegion.center.longitude)
【讨论】: