2015-10-17 16 views
6

iPad için ilk hızlı uygulamasında çalışıyorum. Şimdiye kadar, alt araç çubuğunda bir düğme ile temel bir harita görünümüne sahibim. Bu, bir kez tıklandığında kullanıcıların yerini yenilemek ve odaklanmak istiyorum.Harita düğmesi yenileme konumu

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

}

nasıl yenileme düğmesi bu yaptırabilirsin :

Şu anda bu kodu var? Ben Swift/xcode :)

teşekkür ederiz yeni olduğum gibi ben gerçekten yardıma ihtiyacım

Bu işlevle konumunuzu güncelleyebilir
+0

emin değilim. Neden eylemi denetleyiciye koymuyorsunuz ve hizmetlerinizdeki/yöneticilerinizdeki verileri yenilemiyorsunuz? – razor118

+0

Bunu yapmak için nasıl özür dilerim? Ben xcode için gerçekten yeniyim :(@ razor118 Ben – Oscar

cevap

5

@ Orkhan, bu şekilde yapabileceğinizi söyledi.

Eğer bu eylemi gerçekleştirmek istiyorsanız, sadece basit bir şekilde viewController'a ctrl-sürükleyin ve "Eylem" i seçin.

enter image description here

Bundan sonra işleyicisi kod ekleyebilirsiniz.

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

Merhaba, bu denemeye çalışırken son konumu yenilemek için yenileme düğmesine diliyorum ama ben ilk satırda, "çözülmemiş tanımlanmış 'konumlar' kullanımı olsun :(Neden bu oluyor? Yardımınız için teşekkür ederiz. – Oscar

+0

Güncelleme. LocationManager'da sadece son konumunuzu kaydedin. Düğme eyleminde haritaya son konumu ayarlarız. – razor118

+0

Bu kodu nereye koyarım? "Func locationManager (yönetici: CLLocationManager, didUpdateLocations yerleri: [CLLocation] ]) {" işlevi? tekrar teşekkürler – Oscar

1

: Hedef iOS 8 ise

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

, ihtiyacınız Info.plist'inizde NSLocationAlwaysUsageDescription anahtarını eklemek için.

3

Aşağıdaki 3 satır kod deneyin:

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

DÜZENLEMEYİ

Kontrol aşağıdaki:

Eğer yöntemin sol tarafındaki nokta görebiliyor musunuz?

enter image description here

senin bağlantıları müfettiş bkz istiyorum. Yenile düğmesi ve refresh yöntemi doğru şekilde bağlandı mı? Seni doğru anlamış eğer

enter image description here

+0

Hala değişiklik yok :( – Oscar