我正在使用适用于 iOS 的 Instagram API。我想按名称搜索位置,就像在 Instagram 应用程序本身中一样,但可用于搜索位置的唯一端点是按 LAT 和 LONG。
有没有一种方法可以像 Instagram 应用程序本身一样,通过输入名称来搜索位置?
谢谢!
最佳答案
我做过类似的事情,但我必须自己编写代码。我使用 searchBar
获取位置并将该位置作为 注释
固定到 MapView
我使用了 注释
坐标来执行搜索。我故意展示 map 和图钉。这是我使用的代码。
// 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)
关于ios - 在 Instagram API 中按名称搜索位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49363575/