2016-11-25 14 views

答えて

3

あなたが宣言する必要が続いてCLLocationManagerDelegate方法であなたが得ることができる

// Ask for Authorisation from the User. 
    self.locationManager.requestAlwaysAuthorization() 

    // For use in foreground 
    self.locationManager.requestWhenInUseAuthorization() 

    if CLLocationManager.locationServicesEnabled() { 
     locationManager.delegate = self 
     locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters 
     locationManager.startUpdatingLocation() 
    } 

をユーザの現在位置座標:

func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { 
    var locValue:CLLocationCoordinate2D = manager.location.coordinate 
    print("locations = \(locValue.latitude) \(locValue.longitude)") 
} 

ユーザーが場所を拒否した場合は、場所を変更するオプションを与える設定からの受け入れ:

func locationManager(manager: CLLocationManager, didFailWithError error: NSError) { 
    print("error::: \(error)") 
    locationManager.stopUpdatingLocation() 
    let alert = UIAlertController(title: "Settings", message: "Allow location from settings", preferredStyle: UIAlertControllerStyle.Alert) 
    self.presentViewController(alert, animated: true, completion: nil) 
    alert.addAction(UIAlertAction(title: TextMessages().callAlertTitle, style: .Default, handler: { action in 
     switch action.style{ 
     case .Default: UIApplication.sharedApplication().openURL(NSURL(string: UIApplicationOpenSettingsURLString)!) 
     case .Cancel: print("cancel") 
     case .Destructive: print("destructive") 
     } 
    })) 
} 
0

いいえ、アラートの中でアプリ内で通知することはできます。「使いやすくするために、あなたの現在地を使用するための許可を与えることをおすすめします。また、アプリケーションのロケーション権限にリダイレクトする「設定に移動」ボタンを追加します。 viewDidLoadで

let locationManager = CLLocationManager() 

():ユーザの現在位置を取得するための

+0

これは真剣に答えですか? –

+0

@NumanKaraaslan Yep。そして注意深く読むと、これは受け入れられた答えの短い変形であることがわかります。 – Lexorus

関連する問題