【问题标题】:How to get someones current location in Swift iOS Programming? [duplicate]如何在 Swift iOS 编程中获取某人的当前位置? [复制]
【发布时间】:2016-04-09 07:22:46
【问题描述】:

我正试图弄清楚如何在 XCODE 中使用 SWIFT 为 iOS 应用程序获取某人的当前位置。

我正在关注本教程 here,但由于某种原因,我的代码无法正常工作。我认为这是因为我使用了更新的 Xcode 版本控制。出于某种原因,我的代表有不同的参数,我不知道为什么,但是当我构建我的程序时,它甚至没有问我是否要允许定位服务。

这是我的代码

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

        self.locationManager.delegate = self;
        self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
        self.locationManager.requestWhenInUseAuthorization();
        self.locationManager.startUpdatingLocation();
    }

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

    // GPS STUFF
    // UPDATE LOCATION
    func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        CLGeocoder().reverseGeocodeLocation(manager.location!) { (placemarks, ErrorType) -> Void in
            if(ErrorType != nil)
            {
                print("Error: " + ErrorType!.localizedDescription);
                return;
            }

            if(placemarks?.count > 0)
            {
                let pm = placemarks![0] ;
                self.displayLocationInfo(pm);
            }
        }
    }

    // STOP UPDATING LOCATION
    func displayLocationInfo(placemark: CLPlacemark)
    {
        self.locationManager.stopUpdatingLocation();
        print(placemark.locality);
        print(placemark.postalCode);
        print(placemark.administrativeArea);
        print(placemark.country);
    }

另外,在 info.plist 中我还添加了 NSLocationWhenInUseUsageDescription。

任何建议都会很棒

【问题讨论】:

    标签: ios xcode swift gps location


    【解决方案1】:

    在您的Info.plist 中,添加以下内容:

    或者您可以在Info.plist 中添加这样的内容:

    <key>NSLocationAlwaysUsageDescription</key>
        <string>To get current location</string>
    

    这将要求您允许定位服务。

    希望对您有所帮助。

    【讨论】:

    • 不,那也没用。
    • 我的 locationManager 函数有什么问题吗?
    • 你忘记在 viewDidLoad 中填写 mapView.showsUserLocation = true,试试这个。
    • 查看我的更新答案!!
    • 你做的和教程一样吗?请再做一次。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-06
    • 1970-01-01
    • 1970-01-01
    • 2014-10-12
    • 1970-01-01
    相关资源
    最近更新 更多