2015-10-14 39 views
0

我试图运行一个简单的打印功能,当用户使用手势或搜索移动地图(一般移动地图)我跟着苹果参考指南,我得到的错误。我跟着其他堆栈溢出帖子和其他谷歌搜索结果。我不知道是否因为我在其他地方使用swift 2或其他代码时出现了矛盾。如果有人能帮助我,将不胜感激。这里是我的代码regionDidChangeAnimated不工作

import UIKit 
import MapKit 
import CoreLocation 

class ViewController: UIViewController, MKMapViewDelegate, 
CLLocationManagerDelegate, UISearchBarDelegate 
{ 





@IBOutlet weak var mapView: MKMapView! 
@IBOutlet weak var searchBar: UISearchBar! 

let locationManager = CLLocationManager() 




@IBAction func showSearchBar(sender: AnyObject) { 
    searchController = UISearchController(searchResultsController: nil) 
    searchController.hidesNavigationBarDuringPresentation = false 
    self.searchController.searchBar.delegate = self 
    presentViewController(searchController, animated: true, completion: nil) 
} 


@IBAction func locateMe(sender: AnyObject) { 

    self.locationManager.delegate = self 
    self.locationManager.desiredAccuracy = kCLLocationAccuracyBest 
    self.locationManager.requestWhenInUseAuthorization() 
    self.locationManager.startUpdatingLocation() 

    self.mapView.showsUserLocation = true 

} 

@IBAction func photographer(sender: AnyObject) { 
} 

@IBAction func buyer(sender: AnyObject) { 
} 







var searchController:UISearchController! 
var annotation:MKAnnotation! 
var localSearchRequest:MKLocalSearchRequest! 
var localSearch:MKLocalSearch! 
var localSearchResponse:MKLocalSearchResponse! 
var error:NSError! 
var pointAnnotation:MKPointAnnotation! 
var pinAnnotationView:MKPinAnnotationView! 




override func viewDidLoad() 
{ 
    super.viewDidLoad() 




    self.locationManager.delegate = self 
    self.locationManager.desiredAccuracy = kCLLocationAccuracyBest 
    self.locationManager.requestWhenInUseAuthorization() 
    self.locationManager.startUpdatingLocation() 

    self.mapView.showsUserLocation = true 


    func mapView(mapView: MKMapView!, regionDidChangeAnimated animated: Bool) { 
     hobo() 
    } 






} 

override func didReceiveMemoryWarning() 
{ 
    super.didReceiveMemoryWarning() 
    // Dispose of any resources that can be recreated. 
} 






//  MARK: - Location Delegate Methods 

func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) 
{ 
    let location = locations.last 

    let center = CLLocationCoordinate2D(latitude: location!.coordinate.latitude, longitude: location!.coordinate.longitude) 
    let region = MKCoordinateRegion(center: center, span: MKCoordinateSpan(latitudeDelta: 0.01, longitudeDelta: 0.01)) 

    self.mapView.setRegion(region, animated: true) 

    self.locationManager.stopUpdatingLocation() 
} 







func locationManager(manager: CLLocationManager, didFailWithError error: NSError) 
{ 
    print("Error: " + error.localizedDescription) 
} 





func searchBarSearchButtonClicked(searchBar: UISearchBar){ 
    //1 
    searchBar.resignFirstResponder() 
    dismissViewControllerAnimated(true, completion: nil) 
    if self.mapView.annotations.count != 0{ 
     annotation = self.mapView.annotations[0] 
     self.mapView.removeAnnotation(annotation) 
    } 
    //2 
    localSearchRequest = MKLocalSearchRequest() 
    localSearchRequest.naturalLanguageQuery = searchBar.text 
    localSearch = MKLocalSearch(request: localSearchRequest) 
    localSearch.startWithCompletionHandler { (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.presentViewController(alertController, animated: true, completion: nil) 
      return 
     } 
     //3 
     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!) 
    } 
} 





func hobo(){ 
    print("testing") 
} 



} 

回答

1

确保你在IB中设置自己为地图视图的委托。此外,我会尝试移动viewDidLoad regionDidChangeAnimated。

+0

对不起,我对这门语言还是一种新鲜感,你能解释一下你对这个答案的第一部分的含义吗? – RubberDucky4444