使用Swift查找纬度和经度

时间:2022-11-23 09:03:25

I like to know if it is possible to find the Latitude and the Longitude on iPhone that is not connected to WIFI and using cellular data while inside a building with Swift?

我想知道是否可以在iPhone上找到未连接到WIFI的Latitude和Longitude,并在使用Swift的建筑物内使用蜂窝数据?

IF not what is the alternative way of determine that?

如果不是确定的替代方法是什么?

2 个解决方案

#1


3  

Try this code in order to get the Latitude and the Longitude:

尝试使用此代码以获取纬度和经度:

First create an instance of CLLocationManager and Request Authorization

首先创建CLLocationManager和请求授权的实例

var locManager = CLLocationManager()
locManager.requestWhenInUseAuthorization()

then check if the user allowed authorization.

然后检查用户是否允许授权。

var currentLocation = CLLocation!

if( CLLocationManager.authorizationStatus() == CLAuthorizationStatus.AuthorizedWhenInUse ||
        CLLocationManager.authorizationStatus() == CLAuthorizationStatus.Authorized){

      currentLocation = locManager.location

}

to use it just do this

使用它只是这样做

label1.text = "\(currentLocation.coordinate.longitude)"
label2.text = "\(currentLocation.coordinate.latitude)"

#2


3  

For iOS 8 and swift2.3 add your project's info.plist privacy for always location use and then use this code

对于iOS 8和swift2.3,添加项目的info.plist隐私以便始终使用位置,然后使用此代码

let locationManager = CLLocationManager()
var userLatitude:CLLocationDegrees! = 0
var userLongitude:CLLocationDegrees! = 0

use this code in viewDidLoad -

在viewDidLoad中使用此代码 -

self.locationManager.requestAlwaysAuthorization()
self.locationManager.requestWhenInUseAuthorization()

if CLLocationManager.locationServicesEnabled()

{
    locationManager.delegate = self
    locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
    locationManager.startMonitoringSignificantLocationChanges()

    userLatitude  = locationManager.location?.coordinate.latitude
    userLongitude  = locationManager.location?.coordinate.longitude
}

#1


3  

Try this code in order to get the Latitude and the Longitude:

尝试使用此代码以获取纬度和经度:

First create an instance of CLLocationManager and Request Authorization

首先创建CLLocationManager和请求授权的实例

var locManager = CLLocationManager()
locManager.requestWhenInUseAuthorization()

then check if the user allowed authorization.

然后检查用户是否允许授权。

var currentLocation = CLLocation!

if( CLLocationManager.authorizationStatus() == CLAuthorizationStatus.AuthorizedWhenInUse ||
        CLLocationManager.authorizationStatus() == CLAuthorizationStatus.Authorized){

      currentLocation = locManager.location

}

to use it just do this

使用它只是这样做

label1.text = "\(currentLocation.coordinate.longitude)"
label2.text = "\(currentLocation.coordinate.latitude)"

#2


3  

For iOS 8 and swift2.3 add your project's info.plist privacy for always location use and then use this code

对于iOS 8和swift2.3,添加项目的info.plist隐私以便始终使用位置,然后使用此代码

let locationManager = CLLocationManager()
var userLatitude:CLLocationDegrees! = 0
var userLongitude:CLLocationDegrees! = 0

use this code in viewDidLoad -

在viewDidLoad中使用此代码 -

self.locationManager.requestAlwaysAuthorization()
self.locationManager.requestWhenInUseAuthorization()

if CLLocationManager.locationServicesEnabled()

{
    locationManager.delegate = self
    locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
    locationManager.startMonitoringSignificantLocationChanges()

    userLatitude  = locationManager.location?.coordinate.latitude
    userLongitude  = locationManager.location?.coordinate.longitude
}