繁体   English   中英

SWIFT:核心位置返回nil-如何调用didUpdateLocations?

[英]SWIFT: Core Location returning nil - how to call didUpdateLocations?

我的核心位置有问题,我遵循了https://stackoverflow.com/a/24696878/6140339的设置,并将我的所有代码放在AppDelegate中,但是我不知道如何调用它,因此我创建了另一个函数,其功能与didUpdateLocations相同,并在我的findMyLocation按钮中调用了该函数,并且仅返回nil。

我尝试在Simulator中设置自定义位置,但仍然没有,我尝试使用调试器并设置位置,甚至尝试在iphone上对其进行测试,以查看是否可以获取位置,但仍然没有。

有没有办法从我的按钮调用didUpdateLocations? 还是我只是在做其他我错失的错误。

这是我的viewController中的代码:

import UIKit
import Social
import CoreLocation

class FirstViewController: UIViewController{

//let social = socialFunctions()
let locationManager = CLLocationManager()
let location = locationFunctions()
var locationFixAchieved = false
var currentLocation = CLLocation()

@IBOutlet weak var locationLabel: UILabel!

@IBAction func findMyLocation(sender: AnyObject) {
    updateLocation()
    print("Location: \(locationManager.location)")
}

@IBAction func postToFacebookButton(sender: UIButton) {
    postToFacebook()
}

@IBAction func postTweetButton(sender: UIButton) {
    postToTwitter()
}

override func preferredStatusBarStyle() -> UIStatusBarStyle {
    return .LightContent
}
override func viewDidLoad() {
    super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
}

//MARK: - SOCIAL FUNCTIONS
func postToFacebook(){
    if(SLComposeViewController.isAvailableForServiceType(SLServiceTypeFacebook)){
        let socialController = SLComposeViewController(forServiceType: SLServiceTypeFacebook)
        //creates post with pre-desired text
        socialController.setInitialText("")
        presentViewController(socialController, animated: true, completion: nil)
    }
}

func postToTwitter(){
    if(SLComposeViewController.isAvailableForServiceType(SLServiceTypeTwitter)){
        let socialController = SLComposeViewController(forServiceType: SLServiceTypeTwitter)
        //creates post with pre-desired text
        socialController.setInitialText("")
        presentViewController(socialController, animated: true, completion: nil)
    }
}

//MARK: - LOCATION FUNCTIONS
func updateLocation() {
    let locations = [CLLocation]()
    if (locationFixAchieved == false) {
        locationFixAchieved = true
        let locationArray = locations as NSArray
        let locationObj = locationArray.lastObject as? CLLocation
        let coord = locationObj?.coordinate
        if coord?.latitude != nil {
            locationLabel.text = ("Location \r\n Latitude: \(coord?.latitude) \r\n Longitude: \(coord?.longitude)")
            print("Latitude: \(coord?.latitude)")
            print("Longitude: \(coord?.longitude)")
        } else {
            locationLabel.text = ("Could not find location")
            print("LAT & LONG are nil")
        }
    }
}

}

这是我添加到我的appDelegate中的代码

import UIKit
import CoreLocation
let fvc = FirstViewController()

@UIApplicationMain

class AppDelegate: UIResponder, UIApplicationDelegate, CLLocationManagerDelegate {

var window: UIWindow?
var locationManager: CLLocationManager!
var seenError : Bool = false
var locationFixAchieved: Bool = false
var locationStatus : NSString = "Not Started"

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject : AnyObject]?) -> Bool {
    initLocationManager();
    return true
}

func initLocationManager() {
    seenError = false
    locationFixAchieved = false
    locationManager = CLLocationManager()
    locationManager.delegate = self
    CLLocationManager.locationServicesEnabled()
    locationManager.desiredAccuracy = kCLLocationAccuracyBest

    locationManager.requestAlwaysAuthorization()
}

func locationManager(manager: CLLocationManager, didFailWithError error: NSError) {
    locationManager.stopUpdatingLocation()
    if (error == true) {
        if (seenError == false) {
            seenError = true
            print(error)
        }
    }
}

func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        let locations = [CLLocation]()
        if (locationFixAchieved == false) {
            locationFixAchieved = true
            let locationArray = locations as NSArray
            let locationObj = locationArray.lastObject as? CLLocation
            let coord = locationObj?.coordinate

            print("Latitude: \(coord?.latitude)")
            print("Longitude: \(coord?.longitude)")
            //fvc.locationLabel.text = ("Location \r\n Latitude: \(coord?.latitude) \r\n Longitude: \(coord?.longitude)")
    }
}

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 location Access"
        shouldIAllow = true
    }
    NSNotificationCenter.defaultCenter().postNotificationName("LabelHasBeenUpdated", object: nil)
    if (shouldIAllow == true) {
        NSLog("Location Allowed")
        //Start location services
        locationManager.startUpdatingLocation()
    } else {
        NSLog("Denied access: \(locationStatus)")
    }
}

按照dan的评论(谢谢),我要做的就是删除第一行,它显示了坐标,我还没有弄清楚如何调用我的函数来更改标签文本,但是当我找到它时,我会发布它出来。 编辑:在下面发布解决方案

我变了

func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    let locations = [CLLocation]()
    if (locationFixAchieved == false) {
        locationFixAchieved = true
        let locationArray = locations as NSArray
        let locationObj = locationArray.lastObject as? CLLocation
        let coord = locationObj?.coordinate

        print("Latitude: \(coord?.latitude)")
        print("Longitude: \(coord?.longitude)")
        //fvc.locationLabel.text = ("Location \r\n Latitude: \(coord?.latitude) \r\n Longitude: \(coord?.longitude)")
}

}

删除let locations = [CLLocation]()

这是我按下按钮时的称呼方式。

func updateLocation() {
    let manager = CLLocationManager()
    let locValue : CLLocationCoordinate2D = (manager.location?.coordinate)!;
    let long = locValue.longitude
    let lat = locValue.latitude
    print(long)
    print(lat)
    locationLabel.text = ("Location \r\nLatitude: \(lat) \r\nLongitude: \(long)")
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM