我正在使用Instagram API for iOS。我想按名称搜索位置,就像Instagram应用程序本身一样,但是唯一可用于搜索位置的端点是LAT和LONG。
有没有办法让它像Instagram应用程序本身那样,通过输入名称来搜索位置?
THX!
答案 0 :(得分:0)
我做过类似的事情,但我必须自己编写代码。我使用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
,您仍然可以使用此代码获取Lat和Long的位置
let location = CLLocationCoordinate2D(latitude:
localSearchResponse!.boundingRegion.center.latitude,
longitude: localSearchResponse!.boundingRegion.center.longitude)