gpt4 book ai didi

xcode - 位置不打印到控制台(Swift + Xcode 7 + iOS 9)

转载 作者:可可西里 更新时间:2023-11-01 01:38:49 27 4
gpt4 key购买 nike

我最近更新到使用 iOS 9 的 xCode 7,当我运行以下应用程序时,该位置应该打印到控制台,但没有发生。

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

代码应该是正确的,我已经导入了所有必要的框架,并在 Info.plist 文件中设置了 NSLocationWhenInUseUsageDescription 和 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)")
}
}

关于xcode - 位置不打印到控制台(Swift + Xcode 7 + iOS 9),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32829817/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com