2015-10-17 8 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で最後の場所を保存するだけで、最後の場所をマップに設定します – razor118

+0

このコードはどこに置かれますか? "func locationManager(manager:CLLocationManager、didUpdateLocations locations:[CLLocation ]){」機能?おかげで再び – Oscar

1

をありがとう:あなたが必要とする、あなたのターゲットはiOSの8の場合

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

Info.plistにNSLocationAlwaysUsageDescriptionキーを追加してください。

3

次の3つのラインコード試してみてください。

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

EDIT

を次のように確認してください:

あなたは、メソッドの左側にドットを見ることができますか?

enter image description here

私はあなたの接続インスペクタを見たいです。リフレッシュボタンとrefreshメソッドが正しく接続されていますか?

enter image description here

+0

まだ変更はありません:( – Oscar

関連する問題