地图按钮刷新位置

Map button refresh location

我正在为 iPad 处理我的第一个 swift 申请。到目前为止,我有一个基本的地图视图,底部工具栏中有一个按钮,我想在单击后刷新并聚焦到用户位置。

目前我有这个代码:

import UIKit
import MapKit
import CoreLocation

class 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 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: 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 的新手 :)

谢谢

您可以使用此功能更新您的位置:

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

    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.map.setRegion(region, animated: true)
}

如果你的目标是iOS 8,你需要在你的Info.plist中添加NSLocationAlwaysUsageDescription键。

正如@Orkhan 所说,您可以通过这种方式完成。

如果您想执行操作,只需按住 ctrl 并拖动到 viewController 和 select "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, span: MKCoordinateSpan(latitudeDelta: 0.01, longitudeDelta: 0.01))

    self.map.setRegion(region, animated: true)
}

func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {
    self.location = locations.last as CLLocation
}

试试下面的 3 行代码:

@IBAction func refresh(sender: AnyObject) {
    mapView.setCenterCoordinate(mapView.userLocation.coordinate, animated: true)
}

编辑

检查以下内容:

你能看到方法左边的点吗?

我想看看你的连接检查器。刷新按钮和 refresh 方法是否连接正确?