regionDidChangeAnimated无法正常工作

时间:2015-10-14 01:33:04

标签: ios swift swift2 ios9

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

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 个答案:

答案 0 :(得分:1)

确保将自己设置为IB中的地图视图代理。另外,我尝试将regionDidChangeAnimated移出viewDidLoad。