2016-04-13 29 views
0

Şu anda kullanıcıların uygulamada konum bilgilerini almamı gerektiren bir proje üzerinde çalışıyorum. Başlangıçta ben sadece çalışan bir harita görünümünü göstermek için kodlanmış, ancak o zaman on line hata EXC_BAD_INSTRUCTION almaya devam location.I kullanıcıları göstermekSwift Xcode'da şu anki yerini bulma Bulunduğunuz yer buluyorum

self.mapView.showsUserLocation yapmak istedim = true

import UIKit 
import MapKit 
import CoreLocation 

class MapViewController: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate 
{ 
    @IBOutlet weak var mapView: MKMapView! 

    let locationManager = CLLocationManager() 

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

    // MARK: - Location Delegate Methods 

    func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { 

     let location = locations.last 
     let center = CLLocationCoordinate2DMake(location!.coordinate.latitude, location!.coordinate.latitude) 

     let region = MKCoordinateRegion(center: center, span:MKCoordinateSpan(latitudeDelta: 1, longitudeDelta: 1)) 

     self.mapView.setRegion(region, animated: true) 

     self.locationManager.startUpdatingLocation() 
    } 
+2

Büyük olasılıkla: self.mapView sıfırdır, çünkü Interface Builder'da Harita Görünümü'ne bağlı değildir – Gereon

cevap

0

Bu size yardımcı olabilir.

import UIKit 
import CoreLocation 
import MapKit 

class ViewController: UIViewController, CLLocationManagerDelegate,NSURLConnectionDelegate,UITableViewDataSource,UITableViewDelegate{ 

let locationManager = CLLocationManager() 

var latitude = 0.00; 
var Longitude = 0.00; 




override func viewDidLoad() { 
    super.viewDidLoad() 
    locationManager.delegate = self 
    locationManager.requestLocation() 
    locationManager.requestWhenInUseAuthorization() 
    locationManager.requestAlwaysAuthorization() 
    locationManager.desiredAccuracy = kCLLocationAccuracyBest 
    locationManager.startUpdatingLocation() 

    // Do any additional setup after loading the view, typically from a nib. 
} 


@IBOutlet weak var myLabel: UILabel! 



override func didReceiveMemoryWarning() { 
    super.didReceiveMemoryWarning() 
    // Dispose of any resources that can be recreated. 
} 




func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { 
    if let location11 = locations.first { 
     print("Found User's location: \(location11)") 
     print("Latitude: \(location11.coordinate.latitude) Longitude: \(location11.coordinate.longitude)") 
     latitude = location11.coordinate.latitude 
     Longitude = location11.coordinate.longitude 
     startConnection() 
    } 
} 


func locationManager(manager: CLLocationManager, didFailWithError error: NSError) { 
    print("Failed to find user's location: \(error.localizedDescription)") 
} 

} 

Siz plist dosyasında izinler ekleyin.

+1

Projenize eklemek istediğiniz belirli anahtarlar: hedef: niks290192 için yukarıdaki örnek: ' Gizlilik - Konum Her Zaman Kullanım Açıklaması ve 'Gizlilik - Konum Kullanımda Kullanılırken Kullanılır ' Değerler, kullanıcının uygulamasının neden konum servisleri gerektirdiğini kısaca açıklayan bir ileti dizisi olmalıdır. Daha fazla bilgi için: https://developer.apple.com/reference/corelocation/cllocationmanager – Jalakoo