gpt4 book ai didi

ios - 获取用户位置和更新

转载 作者:行者123 更新时间:2023-11-30 12:48:33 26 4
gpt4 key购买 nike

我正在尝试在我的 iPhone 应用程序中获取用户位置。它已在 info.plist 中指定所需的权限(即使应用程序未离线也使用位置)。但我仍然没有看到我的委托(delegate)至少被触发一次,这意味着我什至没有获得当前位置。这是代码:

import UIKit
import CoreLocation
class ViewController: UIViewController, CLLocationManagerDelegate {

var locationManager = CLLocationManager();
override func viewDidLoad() {


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

locationManager.delegate = self

if (CLLocationManager.authorizationStatus() == CLAuthorizationStatus.notDetermined) {
self.locationManager.requestAlwaysAuthorization();
}

locationManager.distanceFilter = kCLDistanceFilterNone
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.startUpdatingLocation()
}

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

func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let userLocation:CLLocation = locations[0]
let long = userLocation.coordinate.longitude;
let lat = userLocation.coordinate.latitude;


print(lat)
}

func locationManager(manager: CLLocationManager!, didFailWithError error: NSError!)
{
locationManager.stopUpdatingLocation()
if ((error) != nil)
{
print(error)
}
}

有人可以指出我可能存在的问题吗?

最佳答案

尝试删除 locationManager(manager: CLLocationManager!, didFailWithError error: NSError!) 中的 locationManager.stopUpdatingLocation()。它应该有效。

关于ios - 获取用户位置和更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41306240/

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