2016-08-29 2 views
1

코드에서와 같이 MKCordinateSpan을 설정했습니다. 또한지도에서 내 위치로 바로 돌아가는 버튼을 구현했습니다. '내 위치 버튼'과 같습니다. locationManagerButton()이라는 함수입니다. 다른 기능에서 가지고있는 스팬을 어떻게 설정합니까? 여기 내 코드 :사용자 위치 표시 버튼 - 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