无法使用MKMapViewDelegate获取用户位置

时间:2016-06-30 22:30:26

标签: ios objective-c iphone swift mkmapview

我一直在阅读有关获取用户位置几个小时的教程,但我似乎无法得到它。我的ViewController课程实施了CLLocationManagerDelegateMKMapViewDelegate

override func viewDidLoad() {
    super.viewDidLoad()
    self.locationManager.requestWhenInUseAuthorization()
    self.mapView.delegate = self
    self.mapView.showsUserLocation = true
    print("Is User Location Visible: \(self.mapView.userLocation.location?.coordinate.latitude)")
    if CLLocationManager.locationServicesEnabled() {
        locationManager.delegate = self
        print("Authorization Status = \(CLLocationManager.authorizationStatus() == .Denied || CLLocationManager.authorizationStatus() == CLAuthorizationStatus.NotDetermined)")
        locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
        locationManager.startUpdatingLocation()
        locationManager.delegate = self
    }
    else{
        print("location services disabled")
    }
}

这似乎可行,因为我已使用showsUserLocationtrue设置为MKMapView,并开始使用CLLocationManager更新位置服务。

但是,viewDidLoad()的打印输出是:

Is User Location Visible: nil
Authorization Status = false

每当我尝试访问用户位置时,都是零。

我也在Info.plist中设置了NSLocationWhenInUseUsageDescription,它成功地问我是否要允许位置服务,我点击了是。

由于

修改

以下是MapViewController.swift的完整代码:

import UIKit
import MapKit

class MapViewController: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate {

var mapView: MKMapView!

let locationManager = CLLocationManager()

override func loadView() {


    // create a map view
    mapView = MKMapView()

    // set the map view we just created as the view for this controller
    view = mapView

    let ​​​​​​​​​​​​​​​​segmentedControl = UISegmentedControl(items: ["Standard", "Hybrid", "Satellite", "My Location"])
    ​​​​​​​​​​​​​​​​segmentedControl.backgroundColor = UIColor.whiteColor().colorWithAlphaComponent(0.5)
    ​​​​​​​​​​​​​​​​segmentedControl.selectedSegmentIndex = 0

    ​​​​​​​​​​​​​​​​segmentedControl.addTarget(self, action: #selector(MapViewController.mapTypeChanged(_:)), forControlEvents: .ValueChanged)

    ​​​​​​​​​​​​​​​​segmentedControl.translatesAutoresizingMaskIntoConstraints = false
    view.addSubview(​​​​​​​​​​​​​​​​segmentedControl)

    let topConstraint = ​​​​​​​​​​​​​​​​segmentedControl.topAnchor.constraintEqualToAnchor(topLayoutGuide.bottomAnchor, constant: 8)


    let margins = view.layoutMarginsGuide
    let leadingConstraint = ​​​​​​​​​​​​​​​​segmentedControl.leadingAnchor.constraintEqualToAnchor(margins.leadingAnchor)
    let trailingConstraint = ​​​​​​​​​​​​​​​​segmentedControl.trailingAnchor.constraintEqualToAnchor(margins.trailingAnchor)

    topConstraint.active = true
    leadingConstraint.active = true
    trailingConstraint.active = true
}

override func viewDidLoad() {
    super.viewDidLoad()
    self.locationManager.requestWhenInUseAuthorization()
    mapView.delegate = self
    mapView.showsUserLocation = true
    print("Is User Location Visible: \(mapView.userLocation.location?.coordinate.latitude)")
    if CLLocationManager.locationServicesEnabled() {
        locationManager.delegate = self
        print("Authorization Status = \(CLLocationManager.authorizationStatus() == .AuthorizedWhenInUse)")
        locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
        locationManager.startUpdatingLocation()
        locationManager.delegate = self
    }
    else{
        print("location services disabled")
    }
}

func mapTypeChanged(segControl: UISegmentedControl) {
    switch segControl.selectedSegmentIndex {
    case 0:
        mapView.mapType = .Standard
    case 1:
        mapView.mapType = .Hybrid
    case 2:
        mapView.mapType = .Satellite
    case 3:
        zoomOnLocation()
    default:
        break
    }
}

//Zoom to last known location
func zoomOnLocation() {
    print("locations = \(self.mapView.userLocation.coordinate.longitude) ")
    var location:CLLocationCoordinate2D = CLLocationCoordinate2DMake(self.mapView.userLocation.coordinate.latitude, self.mapView.userLocation.coordinate.longitude)
    var region:MKCoordinateRegion = MKCoordinateRegionMake(location, MKCoordinateSpanMake(0.5, 0.5))
    print("zooming")
    mapView.setRegion(region, animated: true)
}

//Delegate if Location has Changed
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]){
    var locValue:CLLocationCoordinate2D = manager.location!.coordinate
    print("locations = \(locValue.latitude) \(locValue.longitude)")
}

//Delegate if Authorization Status has Changed
func locationManager(manager: CLLocationManager, didChangeAuthorizationStatus status: CLAuthorizationStatus){
    locationManager.startUpdatingLocation()
}


func mapView(mapView: MKMapView!, didUpdateUserLocation userLocation: MKUserLocation!){
    let coord = userLocation.coordinate
    print("delegate: coord = \(coord.longitude)")
}

func mapViewDidFinishLoadingMap(mapView: MKMapView!){
    print("finished loading mapview")
    self.mapView.showsUserLocation = true

    print("Is User Location Visible: \(self.mapView.userLocationVisible)")

}

func mapView(mapView: MKMapView!, mapViewWillStartLocatingUser userLocation: MKUserLocation!){
    let coord = userLocation.coordinate
    print("delegate: coord = \(coord.longitude)")
}

}

0 个答案:

没有答案