ios - regionDidChangeAnimated 不工作

标签 ios swift swift2 ios9

当用户使用手势或搜索移动 map (一般移动 map )时,我试图运行一个简单的打印功能我已经按照苹果引用指南进行操作,但出现错误。我关注了其他堆栈溢出帖子和其他谷歌搜索结果。我不知道是不是因为我在其他地方的代码中使用了 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")
}



}

最佳答案

确保您在 IB 中将自己设置为 map View 的委托(delegate)人。另外,我会尝试将regionDidChangeAnimated移出viewDidLoad。

关于ios - regionDidChangeAnimated 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33115205/

相关文章:

ios - 无法访问所有 PFFacebookUtils 方法

ios - 我们如何将崩溃日志存储在iOS应用程序中

ios - 具有大量数据 Swift 的 CollectionView

swift - applyImpulse 不起作用

ios - 大于 60 秒时,Alamofire 超时不起作用

ios - 通过 NSPredicate 比较两个组件

ios - 在 Swift 中确定 SKPhysicsBody 的宽度

ios - 使用 [indexPath.section][indexPath.row] 的下标使用不明确

swift - 初始化器的结果未被使用

ios - Swift 2.2 打破可选/解包可选