2017-12-30 22 views
0
func searchBarSearchButtonClicked(_ searchBar: UISearchBar){ 
    searchBar.resignFirstResponder() 
    dismiss(animated: true, completion: nil) 
    if self.mapView.annotations.count != 0{ 
     annotation = self.mapView.annotations[0] 
     self.mapView.removeAnnotation(annotation) 
    } 
    localSearchRequest = MKLocalSearchRequest() 
    localSearchRequest.naturalLanguageQuery = searchBar.text 
    localSearch = MKLocalSearch(request: localSearchRequest) 
    localSearch.start { (localSearchResponse, error) -> Void in 

     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) 
      return 
     } 
     self.pointAnnotation = MKPointAnnotation() 
     self.pointAnnotation.title = searchBar.text 
     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!) 

答えて

0

使用

let span = MKCoordinateSpanMake(0.003, 0.003) 
let region = MKCoordinateRegionMake(self.pointAnnotation.coordinate, span) 
mapView.setRegion(region, animated:true) 

代わりの

mapView.centerCoordinate = self.pointAnnotation.coordinate 
関連する問題