使用Swift查找纬度和经度

时间:2016-01-30 20:11:41

标签: ios swift latitude-longitude

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

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

2 个答案:

答案 0 :(得分:3)

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

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

MyApp.controller('IndexPageController', ['$scope', '$http',  function ($scope, $http) {
  $scope.data="something";
 });

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

var locManager = CLLocationManager()
locManager.requestWhenInUseAuthorization()

使用它只是这样做

var currentLocation = CLLocation!

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

      currentLocation = locManager.location

}

答案 1 :(得分:3)

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

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

在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
}