【问题标题】:finding current location in Swift Xcode 7.3在 Swift Xcode 7.3 中查找当前位置
【发布时间】:2016-04-13 12:42:04
【问题描述】:

我目前正在处理一个需要我在应用中获取用户位置的项目。最初我将它编码为仅显示一个工作地图视图,但后来想让它显示用户位置。我一直收到错误 EXC_BAD_INSTRUCTION 在线

self.mapView.showsUserLocation = 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()
    }

【问题讨论】:

  • 最可能的原因:self.mapView 为 nil,因为它没有连接到 Interface Builder 中的 Map View

标签: xcode swift mapkit cllocationmanager


【解决方案1】:

这可能会对你有所帮助。

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

 }

在您的 plist 文件中添加权限。

【讨论】:

  • 您要添加到项目中的特定键: target: info.plist 上面 niks290192 的示例将是:Privacy - Location Always Usage DescriptionPrivacy - Location When In Use Usage Description 值应该是向用户简要描述的字符串消息为什么您的应用需要位置服务。更多信息:developer.apple.com/reference/corelocation/cllocationmanager
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-11-18
  • 1970-01-01
  • 2016-04-29
  • 1970-01-01
相关资源
最近更新 更多