2017-04-11 1 views
0

Je veux que ma demande de prendre position dans les modes actifs et de fond (si l'utilisation ne NSLocationAlwaysUsageDescription la permission alors myLocationButton ne montre pas). Je réglerai Info.plist:ne montre qu'une seule autorisation Lieu alerte

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

Et ajouter MapViewController

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

Mais quand l'application démarre, seule la première alerte d'autorisation de localisation est affiché, et la seconde alerte d'autorisation apparaît après l'application est rouvert.

Mise à jour:

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

Vous avez seulement besoin de demander une autorisation "always". Cela permet à votre application d'utiliser l'emplacement au premier plan et l'arrière-plan. – Paulw11

+0

@ Paulw11 J'écris à propos de si utilisation seulement NSLocationAlwaysUsageDescription permission puis myLocationButton ne montrent pas (mise à jour de la position, mais le bouton ne montre pas) iOS 10.3 swift 3 – Roman

+1

Comment décidez-vous si vous souhaitez afficher ce bouton? Je peux vous assurer que l'authentification Always est tout ce qui est nécessaire pour l'emplacement de premier plan et d'arrière-plan. – Paulw11

Répondre

0

Ajouter ce délégué dans votre classe:

CLLocationManagerDelegate 

maintenant à l'intérieur vous classe:

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

Ensuite ajoutez dans votre code:

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() 

} 

Après avoir ajouté ce code, faites un clic droit sur votre Info.plist et "Ouvrir comme code source". Ajoutez ceci dans votre 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>