SWIFT:核心位置返回 nil - 如何调用 didUpdateLocations?
Posted
技术标签:
【中文标题】SWIFT:核心位置返回 nil - 如何调用 didUpdateLocations?【英文标题】:SWIFT: Core Location returning nil - how to call didUpdateLocations? 【发布时间】:2016-06-21 18:08:07 【问题描述】:我对核心位置有疑问,我已按照以下设置 https://***.com/a/24696878/6140339 这个答案,并将我所有的代码放在 AppDelegate 中,但我不知道如何调用它,所以我创建了另一个与 didUpdateLocations 执行相同操作的函数,并在我的 findMyLocation 按钮中调用它,它是只返回零。
我尝试在模拟器中设置自定义位置,仍然为零,我尝试使用调试器并设置位置,我什至尝试在我的 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)")
【问题讨论】:
didUpdateLocations
方法中的第一行会丢弃传入的位置并将其替换为空数组
@dan let locations = [CLLocation]()
?那我怎么称呼这个位置呢?
【参考方案1】:
按照 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)")
【讨论】:
以上是关于SWIFT:核心位置返回 nil - 如何调用 didUpdateLocations?的主要内容,如果未能解决你的问题,请参考以下文章