2016-08-29 50 views
1

我已经设置了一个MKCordinateSpan,就像您可以在代码中一样。我还实现了一个按钮,可以直接返回到我在地图上的位置。就像“我的位置按钮”。它是名为locationManagerButton()的函数。如何将span设置为与其他函数中的相同。这里是我的代码:显示用户位置按钮 - Swift

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


} 

回答

0

尝试改变你的功能。

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) 


} 
+0

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