2017-11-24 3 views
0

は、私は、ユーザーにアクセスしようとしています座標は、使用して座標:ユーザーへのアクセス方法

import MapKit 
import CoreLocation 

override func viewDidLoad() { 
    super.viewDidLoad() 
    let locationManager = CLLocationManager() 
    let userCoordinates = (locationManager.location?.coordinate)! 
} 

しかし、それはシミュレータの読み込み時にクラッシュします。私はシミュレータの場所をAppleに設定し、プライバシーキーをinfo.plistに入力しますが、なぜこれがユーザーの場所を取得しているのかわかりません。

答えて

0

デバイスの現在のジオロケーションを安全に使い始めるには、最初に行う必要があることがいくつかあります。あなたが提供しているコードに基づいています。基本的には他のどのような役割を果たしGoogleマップを使用して:

class YourViewController: UIViewController, CLLocationManagerDelegate { 

    // properties 
    var locationManager = CLLocationManager() 
    var currentCoordinate: CLLocationCoordinate2D? 

    // load view 
    override func loadView() { 
     addLocationManager() 
    } 

    // add location manager 
    func addLocationManager() { 

     locationManager.delegate = self 
     locationManager.desiredAccuracy = kCLLocationAccuracyBest 
     locationManager.distanceFilter = kCLDistanceFilterNone 
     locationManager.requestWhenInUseAuthorization() 
     locationManager.startUpdatingLocation() 

    } 

    // location manager delegate: did change authorization 
    func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) { 

     switch status { 

     case .restricted: 
      print("LOCATION ACCESS RESTRICTED") 

     case .denied: 
      print("LOCATION ACCESS DENIED") 

     case .notDetermined: 
      print("LOCATION ACCESS NOT DETERMINED") 

     case .authorizedAlways: 
      fallthrough 

     case .authorizedWhenInUse: 
      print("LOCATION STATUS GRANTED") 

     } 

    } 


    // location manager delegate: did fail with error 
    func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) { 

     locationManager.stopUpdatingLocation() 
     print("LOCATION ACCESS ERROR: \(error)") 

    } 


    // location manager delegate: did update locations 
    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { 

     let lastLocation = locations.last! 

     // unhide map when current location is available 
     if mapView.isHidden { 
      mapView.camera = GMSCameraPosition.camera(withLatitude: lastLocation.coordinate.latitude, longitude: lastLocation.coordinate.longitude, zoom: 18, bearing: 0, viewingAngle: 0) 
      mapView.isHidden = false 
     } 

     // update current location properties 
     currentCoordinate = lastLocation.coordinate 

    } 


} 

そして、あなたはMapKitをインポートした場合CoreLocationをインポートする必要はありません。

関連する問題