ios Swift - 试图获取当前位置

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37154244/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 09:02:28  来源:igfitidea点击:

Swift - Trying to get current location

iosswiftcore-location

提问by user979331

I am trying to get the long and lat of my users locations, I have done the following:

我正在尝试获取用户位置的经纬度,我已完成以下操作:

imported Core Location

进口核心位置

import CoreLocation

Add Core Location Delegate

添加核心位置委托

class ViewController: UIViewController, CLLocationManagerDelegate, AVCaptureMetadataOutputObjectsDelegate, DTDeviceDelegate {

defined this variable:

定义了这个变量:

var locationManager: CLLocationManager!

and then added this method:

然后添加了这个方法:

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

        print(long, lat)

        //Do What ever you want with it
    }

but the location does not get printed, my method does not even get it.

但是位置没有打印出来,我的方法甚至没有得到它。

I add the items to use Core Location to my plist and the app ask me to use the location services when I first run it. But now location is getting printed....what am I doing wrong?

我将使用 Core Location 的项目添加到我的 plist 中,当我第一次运行该应用程序时,它会要求我使用定位服务。但是现在位置正在打印....我做错了什么?

回答by Khuong

Maybe you forgot to set value for the delegate

也许你忘记设置值 delegate

In your viewDidLoad()add this:

在你viewDidLoad()添加这个:

if CLLocationManager.locationServicesEnabled() {
    locationManager.requestAlwaysAuthorization()
    locationManager.requestWhenInUseAuthorization()
    locationManager.delegate = self
    locationManager.desiredAccuracy = kCLLocationAccuracyBest
    locationManager.startUpdatingLocation()
    mapView.showsUserLocation = true
}

回答by Iyyappan Ravi

Use this Code,

使用此代码,

declare locationManager

声明位置管理器

var locationManager = CLLocationManager()

Set the Delegate in viewDidLoad, shown below code,

在viewDidLoad中设置Delegate,如下代码所示,

override func viewDidLoad() {

    super.viewDidLoad()

    locationManager.delegate = self

    if CLLocationManager.authorizationStatus() == .NotDetermined {
        self. locationManager.requestWhenInUseAuthorization()
    }

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

回答by Jigar Tarsariya

Add this in viewdidload,

在viewdidload中添加这个,

if (CLLocationManager.locationServicesEnabled())
{
     locationManager.delegate = self
     locationManager.desiredAccuracy = kCLLocationAccuracyBest
     if ((UIDevice.currentDevice().systemVersion as NSString).floatValue >= 8)
     {
          locationManager.requestWhenInUseAuthorization()
     }

     locationManager.startUpdatingLocation()
}
else
{
     #if debug
     println("Location services are not enabled");
     #endif           
}

Then add this two delegate method to get location,

然后添加这两个委托方法来获取位置,

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

func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!)
{
    var locationArray = locations as NSArray
    var locationObj = locationArray.lastObject as! CLLocation
    var coord = locationObj.coordinate
    print(coord.latitude)
    print(coord.longitude)

}

This way you will get your current location.

这样你就可以得到你当前的位置。

回答by Andey Satyanarayana

You neeed to initialize the locationManageger and enable the location flag in xcode also, it appaear bottom of the xcode

您还需要初始化 locationManageger 并在 xcode 中启用位置标志,它出现在 xcode 的底部

self.locationManager = CLLocationManager()
self.locationManager.delegate = self
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest

if #available(iOS 8.0, *) {

    if  Bundle.main.infoDictionary?["NSLocationAlwaysUsageDescription"] != nil {

        self.locationManager.requestAlwaysAuthorization()
    } 
    else {

        self.locationManager.requestWhenInUseAuthorization()
    }    
}

self.locationManager.desiredAccuracy = kCLLocationAccuracyBest
self.locationManager.startUpdatingLocation()

回答by Akhi

You can add this code to your viewDidLoad()method:

您可以将此代码添加到您的viewDidLoad()方法中:

locationManager = CLLocationManager()
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.requestWhenInUseAuthorization()
locationManager.startUpdatingLocation()

This will call the locationManagers didUpdate delegate-methods if location services are available for your app

如果位置服务可用于您的应用程序,这将调用 locationManagers didUpdate 委托方法