Can’t find your location on Google Maps in iOS 8

I am trying to use Google Maps for iOS in a Swift project for iOS 8. I added the Objective-C bridge title and added the Google Maps SDK as described in the documentation . And I tried this example and it works great.

I needed to show my current location. I set the coordinates for a custom location in my iOS 8 simulator and changed the code as shown in this StackOverflow answer. Below is a version of Swift.

import UIKit import Foundation class MapViewController: UIViewController { @IBOutlet var mapView: GMSMapView! var firstLocationUpdate: Bool? override func viewDidLoad() { super.viewDidLoad() let camera = GMSCameraPosition.cameraWithLatitude(-33.86, longitude: 151.20, zoom: 12) mapView = GMSMapView.mapWithFrame(CGRectZero, camera: camera) mapView.settings.compassButton = true mapView.settings.myLocationButton = true mapView.addObserver(self, forKeyPath: "myLocation", options: .New, context: nil) dispatch_async(dispatch_get_main_queue(), { () -> Void in self.mapView.myLocationEnabled = true }) println(mapView.myLocation) view = mapView let marker = GMSMarker() marker.position = CLLocationCoordinate2DMake(-33.86, 151.20) marker.title = "Sydney" marker.snippet = "Australia" marker.map = mapView } override func observeValueForKeyPath(keyPath: String!, ofObject object: AnyObject!, change: [NSObject : AnyObject]!, context: UnsafeMutablePointer<Void>) { firstLocationUpdate = true let location = change[NSKeyValueChangeNewKey] as CLLocation mapView.camera = GMSCameraPosition.cameraWithTarget(location.coordinate, zoom: 14) } } 

I ran it, but it does not indicate the location that I assigned. When I println() print println(mapView.myLocation) location println(mapView.myLocation) , it will return nil .

I assume that this is due to the fact that in iOS8 we clearly need to ask the user to allow us to get their location. See here .

I added the NSLocationWhenInUseUsageDescription key to my info.plist. My problem is how can I get permission on the CLLocationManager since I use the Google Maps SDK. I put the code below for verification purposes only, but it did not request a permission dialog.

 let locationManager = CLLocationManager() locationManager.requestWhenInUseAuthorization() 

Is there a workaround? Or do we need to wait until Google updates its SDK?

Thanks.

+5
source share
1 answer

You need your CLLocationManager to be saved, otherwise it will be released before it gets the opportunity to present an authorization dialog.

 class MapViewController: UIViewController { @IBOutlet var mapView: GMSMapView! var firstLocationUpdate: Bool? let locationManager=CLLocationManager() override func viewDidLoad() { super.viewDidLoad() self.locationManager.requestWhenInUseAuthorization() let camera = GMSCameraPosition.cameraWithLatitude(-33.86, longitude: 151.20, zoom: 12) mapView = GMSMapView.mapWithFrame(CGRectZero, camera: camera) mapView.settings.compassButton = true mapView.settings.myLocationButton = true mapView.addObserver(self, forKeyPath: "myLocation", options: .New, context: nil) dispatch_async(dispatch_get_main_queue(), { () -> Void in self.mapView.myLocationEnabled = true }) println(mapView.myLocation) view = mapView let marker = GMSMarker() marker.position = CLLocationCoordinate2DMake(-33.86, 151.20) marker.title = "Sydney" marker.snippet = "Australia" marker.map = mapView } 
+7
source

Source: https://habr.com/ru/post/1203999/


All Articles