Ios 显示用户位置按钮-Swift

Ios 显示用户位置按钮-Swift,ios,swift,dictionary,location,Ios,Swift,Dictionary,Location,我已经在代码中设置了一个MKCordinateSpan。我还实现了一个按钮,可以直接返回到地图上的位置。就像一个“我的位置按钮”。它是名为locationManagerButton()的函数。如何将跨度设置为与其他函数中相同的值。这是我的密码: import UIKit import MapKit class MapController: UICollectionViewController, UICollectionViewDelegateFlowLayout, CLLocationMan

我已经在代码中设置了一个MKCordinateSpan。我还实现了一个按钮,可以直接返回到地图上的位置。就像一个“我的位置按钮”。它是名为locationManagerButton()的函数。如何将跨度设置为与其他函数中相同的值。这是我的密码:

import UIKit
import MapKit

class MapController:  UICollectionViewController, UICollectionViewDelegateFlowLayout, CLLocationManagerDelegate, MKMapViewDelegate {

var window: UIWindow?
var mapView: MKMapView?
let locationManager = CLLocationManager()

let distanceSpan: Double = 500


override func viewDidLoad() {
    super.viewDidLoad()

    navigationItem.title = "Current Location"

    self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
    self.view.backgroundColor = UIColor.whiteColor()

    self.mapView = MKMapView(frame: CGRectMake(0, 20, (self.window?.frame.width)!, (self.window?.frame.height)!))
    self.view.addSubview(self.mapView!)

    self.locationManager.delegate = self
    self.locationManager.desiredAccuracy = kCLLocationAccuracyBest
    self.locationManager.requestWhenInUseAuthorization()
    self.locationManager.startUpdatingLocation()
    self.mapView!.showsUserLocation = true


    //Show User Location Button
    let button: UIButton = UIButton(type: UIButtonType.Custom)
    button.setImage(UIImage(named: "MyLocationButton"), forState: UIControlState.Normal)
    button.addTarget(self, action: #selector(locationManagerButton), forControlEvents: UIControlEvents.TouchUpInside)
    button.frame = CGRectMake(0, 0, 30, 30)
    let barButton = UIBarButtonItem(customView: button)
    self.navigationItem.leftBarButtonItem = barButton


}

func locationManager(manager: CLLocationManager, didUpdateToLocation newLocation: CLLocation, fromLocation oldLocation: CLLocation) {
    if let mapView = self.mapView {
        let region = MKCoordinateRegionMakeWithDistance(newLocation.coordinate, self.distanceSpan, self.distanceSpan)
        mapView.setRegion(region, animated: true)
        mapView.showsUserLocation = true
    }
}

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()

    let locValue:CLLocationCoordinate2D = manager.location!.coordinate
    print("locations = \(locValue.latitude) \(locValue.longitude)")



}

func locationManagerButton() {
    mapView!.setCenterCoordinate(mapView!.userLocation.coordinate, animated: true)


}


func locationManager(manager: CLLocationManager, didFailWithError error: NSError)
{
    print("Errors: " + error.localizedDescription)
}


}

尝试将您的函数更改为此

func locationManagerButton(manager: CLLocationManager, 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))

    mapView!.setRegion(region, animated: true)
    mapView!.setCenterCoordinate(mapView!.userLocation.coordinate, animated: true)


}

尝试将您的函数更改为此

func locationManagerButton(manager: CLLocationManager, 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))

    mapView!.setRegion(region, animated: true)
    mapView!.setCenterCoordinate(mapView!.userLocation.coordinate, animated: true)


}

嗨,当我按下按钮时,我发现了这个错误NSInvalidArgumentException',原因:'-[UITouchesEvent copyWithZone:]:无法识别的选择器发送到实例0x7fbc7b409fb0'嗨,我按下按钮时出现此错误。'NSInvalidArgumentException',原因:'-[UITouchesEvent copyWithZone:]:未识别的选择器发送到实例0x7fbc7b409fb0'