目前我有这个代码:
import UIKitimport MapKitimport CoreLocationclass VIEwController: UIVIEwController,MKMapVIEwDelegate,CLLocationManagerDelegate {var location: CLLocation!let locationManager = CLLocationManager()@IBOutlet weak var mapVIEw: MKMapVIEw!@IBOutlet weak var refresh: UIbarbuttonItem!@IBAction func refresh(sender: AnyObject) { 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)}overrIDe func vIEwDIDLoad() { super.vIEwDIDLoad() self.locationManager.delegate = self self.locationManager.desiredAccuracy = kCLLocationAccuracyBest self.locationManager.requestWhenInUseAuthorization() self.locationManager.startUpdatingLocation() self.mapVIEw.showsUserLocation = true}overrIDe func dIDReceiveMemoryWarning() { super.dIDReceiveMemoryWarning() // dispose of any resources that can be recreated.}// location delegate methodsfunc 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: 1,longitudeDelta: 1)) self.mapVIEw.setRegion(region,animated: true) self.locationManager.stopUpdatingLocation()}func locationManager(manager: CLLocationManager,dIDFailWithError error: NSError){ print("Error code: " + error.localizedDescription)}
}
如何获取刷新按钮?我真的需要一些帮助,因为我是Swift / xcode的新手:)
谢谢
正如@Orkhan所说,你可以这样做.如果你想做这个动作你只需简单地按住Ctrl键拖动到vIEwController并选择“Action”.
之后,您可以将代码添加到处理程序.
var location: CLLocation! @IBAction func refreshLocation(sender: AnyObject) { let center = CLLocationCoordinate2D(latitude: location.coordinate.latitude,longitude: location.coordinate.longitude) let region = MKCoordinateRegion(center: center,longitudeDelta: 0.01)) self.map.setRegion(region,animated: true)}func locationManager(manager: CLLocationManager!,dIDUpdateLocations locations: [AnyObject]!) { self.location = locations.last as CLLocation}总结
以上是内存溢出为你收集整理的swift – 地图按钮刷新位置全部内容,希望文章能够帮你解决swift – 地图按钮刷新位置所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)