2017-04-11 3 views
0

を表示します。 私はInfo.plistファイルに設定:は(表示されませんmyLocationButton</strong><strong>その後、使用する場合にのみ<strong>NSLocationAlwaysUsageDescription</strong>許可を)私は自分のアプリケーションがアクティブおよびバックグラウンドモードで場所を取るしたい唯一の場所許可警告

<key>NSLocationAlwaysUsageDescription</key> 
<string>$(PRODUCT_NAME) location use</string> 
<key>NSLocationWhenInUseUsageDescription</key> 
<string>$(PRODUCT_NAME) location use</string> 
<key>UIBackgroundModes</key> 

をそしてMapViewController

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

を追加しますが、アプリケーションの起動時に、最初の場所許可警告が表示され、アプリケーションが後に第2の許可警告が表示されます再オープン。

更新:

override func viewDidLoad() { 
     super.viewDidLoad() 
...  
locationManager.delegate = self 
locationManager.requestWhenInUseAuthorization() 
viewMap.addObserver(self, forKeyPath: "myLocation", options: NSKeyValueObservingOptions.new, context: nil) 
     self.startLocationUpdates() 
... 
} 

func startLocationUpdates() { 
    self.locationManager.delegate = self 
    self.locationManager.desiredAccuracy = kCLLocationAccuracyBest 
    self.locationManager.activityType = CLActivityType.automotiveNavigation 
    self.locationManager.distanceFilter = distanceFilterMetr 
    self.locationManager.requestAlwaysAuthorization() 
    self.locationManager.startUpdatingLocation() 
} 

override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) { 

     let myLocation: CLLocation = change?[NSKeyValueChangeKey.newKey] as! CLLocation 
     viewMap.camera = GMSCameraPosition.camera(withTarget: myLocation.coordinate, zoom: observeZoom) 
     mapRoute.coordinateLatitude = myLocation.coordinate.latitude 
     mapRoute.coordinateLongitude = myLocation.coordinate.longitude 
     viewMap.delegate = self 
     viewMap.settings.myLocationButton = true 
     viewMap.settings.compassButton = true 
     didFindMyLocation = true 
} 
+0

「常に」承認を要求するだけです。これにより、アプリはフォアグラウンドとバックグラウンドで位置情報を使用できるようになります。 – Paulw11

+0

@ Paulw11 NSLocationAlwaysUsageDescription権限のみを使用しmyLocationButtonが表示されない場合(場所の更新はボタンが表示されない)iOS 10.3 swift 3 – Roman

+1

どうすればそのボタンを表示するかを決定していますか?フォアグラウンドとバックグラウンドの場所には常に認証が必要です。 – Paulw11

答えて

0

あなたのクラスで、このデリゲートを追加:クラス内の今

CLLocationManagerDelegate 

var locationManager:CLLocationManager! 
var map = GMSMapView() 
var currentLatitude:Double! 
var currentLongitude:Double! 

をその後、あなたのコードでこれを追加します。

override func loadView() { 

    print("loadView called") 

    // Enable some map settings 

    map.isMyLocationEnabled = true 
    map.settings.myLocationButton = true 
    map.settings.compassButton = true 
    map.settings.scrollGestures = true 
    map.settings.zoomGestures = true 
    map.delegate = self 

    view = map 
} 

override func viewDidLoad() { 

    super.viewDidLoad() 

    print("ViewDidLoad called") 

    // Configuring location manager. 

    locationManager = CLLocationManager() 
    locationManager.delegate = self 
    locationManager.requestAlwaysAuthorization() 
    locationManager.desiredAccuracy = kCLLocationAccuracyBest 
    locationManager.startUpdatingLocation() 
    locationManager.startMonitoringSignificantLocationChanges() 
} 

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { 

    print("locationManager function called") 

    // Fetch current location coordinates 

    let locValue:CLLocationCoordinate2D = (locationManager.location?.coordinate)! 
    currentLatitude = locValue.latitude 
    currentLongitude = locValue.longitude 
    print("Current Location = \(currentLatitude!), \(currentLongitude!)") 

    // Zoom to current location 

    let target = CLLocationCoordinate2D(latitude: currentLatitude!, longitude: currentLongitude!) 
    map.camera = GMSCameraPosition.camera(withTarget: target, zoom: 17) 

    locationManager.stopUpdatingLocation() 

} 

このコードを追加したら、Info.plistを右クリックし、「ソースコードとして開く」をクリックします。これをInfo.plistに追加してください。

<key>LSApplicationQueriesSchemes</key> 
<array> 
    <string>googlechromes</string> 
    <string>comgooglemaps</string> 
</array> 
<key>LSRequiresIPhoneOS</key> 
<true/> 
<key>NSBluetoothPeripheralUsageDescription</key> 
<string></string> 
<key>NSCameraUsageDescription</key> 
<string></string> 
<key>NSContactsUsageDescription</key> 
<string></string> 
<key>NSLocationUsageDescription</key> 
<string></string> 
<key>NSLocationWhenInUseUsageDescription</key> 
<string></string> 
<key>NSLocationAlwaysUsageDescription</key> 
<string></string> 
<key>NSMicrophoneUsageDescription</key> 
<string></string> 
<key>NSMotionUsageDescription</key> 
<string></string> 
<key>NSPhotoLibraryUsageDescription</key> 
<string></string> 
<key>NSRemindersUsageDescription</key> 
<string></string> 
<key>NSSiriUsageDescription</key> 
<string></string> 
<key>NSSpeechRecognitionUsageDescription</key> 
<string></string> 
<key>NSVideoSubscriberAccountUsageDescription</key> 
<string></string> 
関連する問題