2015-10-17 50 views
6

我正在爲我的第一個iPad快速應用程序工作。到目前爲止,我有一個基本的地圖視圖,在底部的工具欄中有一個按鈕,我想刷新一下,點擊後將它聚焦到用戶位置。地圖按鈕刷新位置

目前我有這樣的代碼:

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

}

我怎樣才能獲得的刷新按鈕來做到這一點?我真的需要一些幫助,因爲我是新來的斯威夫特/ Xcode的:)

謝謝

+0

我不確定我是否正確理解了你。爲什麼你不把操作放到控制器上並刷新服務/管理器中的數據? – razor118

+0

我該如何去做這件事很抱歉?我真的是新的xcode :(@ razor118我希望刷新按鈕來刷新最新的位置,當按下 – Oscar

回答

5

@Orkhan說你可以這樣做。

如果你想做的動作你只是簡單的CTRL-拖動viewController並選擇「行動」。

enter image description here

之後,你可以將代碼添加到處理程序。

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 
} 
+0

嗨,嘗試這一點,但我得到「使用未解決的確定'位置,在第一行:(爲什麼會發生這種情況?感謝您的幫助 – Oscar

+0

已更新。在locationManager中,您只保存最後一個位置。在按鈕操作中,我們將最後一個位置設置爲map。 – razor118

+0

我在哪裏放置這段代碼?在「func locationManager(manager:CLLocationManager,didUpdateLocations locations:[CLLocation ]){」功能?再次感謝 – Oscar

1

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

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密鑰。

3

請嘗試以下3行代碼:

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

編輯

檢查以下內容:

你可以看到方法左邊的點?

enter image description here

我想看看你的連接檢查。刷新按鈕和refresh方法連接是否正確?

enter image description here

+0

仍然沒有變化:( – Oscar