Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
未打印到控制台的位置(Swift&x2B;Xcode 7&x2B;iOS 9)_Xcode_Swift_Ios8_Core Location_Ios9 - Fatal编程技术网

未打印到控制台的位置(Swift&x2B;Xcode 7&x2B;iOS 9)

未打印到控制台的位置(Swift&x2B;Xcode 7&x2B;iOS 9),xcode,swift,ios8,core-location,ios9,Xcode,Swift,Ios8,Core Location,Ios9,我最近更新了使用iOS9的xCode 7,当我运行下面的应用程序时,该位置应该打印到控制台上,但这并没有发生 应用程序将成功构建,地图显示,但位置数据不会打印。即使移动地图,它仍然不会打印 代码应该是正确的,我已经导入了所有必要的框架,并在Info.plist文件中设置了nsLocationWhenUsageDescription和NSLocationAlwaysUsageDescription以及它们的值 代码如下: // ViewController.swift // Maps iOS9

我最近更新了使用iOS9的xCode 7,当我运行下面的应用程序时,该位置应该打印到控制台上,但这并没有发生

应用程序将成功构建,地图显示,但位置数据不会打印。即使移动地图,它仍然不会打印

代码应该是正确的,我已经导入了所有必要的框架,并在Info.plist文件中设置了nsLocationWhenUsageDescription和NSLocationAlwaysUsageDescription以及它们的值

代码如下:

//  ViewController.swift
//  Maps iOS9
//
//  Created by Alex Ngounou on 9/28/15.
//  Copyright © 2015 Alex Ngounou. All rights reserved.

import UIKit
import MapKit
import CoreLocation

class ViewController: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate {    

@IBOutlet weak var myMap: MKMapView!
var locationManager = CLLocationManager()


override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.


    locationManager.delegate = self
    locationManager.desiredAccuracy = kCLLocationAccuracyBest
    locationManager.requestAlwaysAuthorization()
    locationManager.startUpdatingLocation() // starts accessing the user's position


    // 18.328278, -65.318353 Playa Flamenco
    let latitude: CLLocationDegrees = 18.328278
    let longitude: CLLocationDegrees = -65.318353

    let latDelta: CLLocationDegrees = 0.01
    let longDelta: CLLocationDegrees = 0.01

    let location: CLLocationCoordinate2D = CLLocationCoordinate2DMake(latitude, longitude)
    let span: MKCoordinateSpan = MKCoordinateSpanMake(latDelta, longDelta)

    let region: MKCoordinateRegion = MKCoordinateRegionMake(location, span)

    myMap.setRegion(region, animated: true)

    let annotation = MKPointAnnotation()
    annotation.coordinate = location
    annotation.title = "Playa de Flamenco"
    annotation.subtitle = "Culebra Island"
    myMap.addAnnotation(annotation)

    let uilpgr = UILongPressGestureRecognizer(target: self, action: "action:")
    uilpgr.minimumPressDuration = 1.0

    myMap.addGestureRecognizer(uilpgr)
}

func action(gestureRecognizer: UILongPressGestureRecognizer) {
    // touchpoint
    let touchPoint = gestureRecognizer.locationInView(self.myMap)

    // touchpoint to location
    let newLocation: CLLocationCoordinate2D = myMap.convertPoint(touchPoint, toCoordinateFromView: self.myMap)

    // annotation
    let newAnnotation = MKPointAnnotation()
    newAnnotation.coordinate = newLocation
    newAnnotation.title = "New Poing"
    newAnnotation.subtitle = "added via user's touch"
    myMap.addAnnotation(newAnnotation)
}



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



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

尝试以下代码片段:

// Location Manager helper stuff
func initLocationManager() {

    locationManager = CLLocationManager()
    locationManager.delegate = self
    locationManager.locationServicesEnabled

    locationManager.requestAlwaysAuthorization()
}

// Location Manager Delegate stuff
// If failed
func locationManager(manager: CLLocationManager!, didFailWithError error: NSError!) {
    locationManager.stopUpdatingLocation()
    if (error) {
       print(error)
    }
}

func locationManager(manager: CLLocationManager!, didUpdateLocations locations: AnyObject[]!) {
        println("locations = \(locations)")
}

// authorization status
func locationManager(manager: CLLocationManager!,
    didChangeAuthorizationStatus status: CLAuthorizationStatus) {
        var shouldIAllow = false

        switch status {
        case CLAuthorizationStatus.Restricted:
            locationStatus = "Restricted Access to location"
        case CLAuthorizationStatus.Denied:
            locationStatus = "User denied access to location"
        case CLAuthorizationStatus.NotDetermined:
            locationStatus = "Status not determined"
        default:
            locationStatus = "Allowed to location Access"
            shouldIAllow = true
        }
        NSNotificationCenter.defaultCenter().postNotificationName("LabelHasbeenUpdated", object: nil)
        if (shouldIAllow == true) {
            NSLog("Location to Allowed")
            // Start location services
            locationManager.startUpdatingLocation()
        } else {
            NSLog("Denied access: \(locationStatus)")
        }
}
我的iOS模拟器的“调试/定位”设置不正确。一旦我把它改成“城市自行车骑行”,一切都很好

谢谢你的帮助