gpt4 book ai didi

ios - regionDidChangeAnimated 不工作

转载 作者:行者123 更新时间:2023-11-29 01:34:33 26 4
gpt4 key购买 nike

当用户使用手势或搜索移动 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com