Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot get my location to show up in Google Maps in iOS 8

I'm trying to use the Google Maps for iOS in a Swift project for iOS 8. I added the Objective-C bridging header and added the Google Maps SDK as instructed in their documentation. And I tried this example and it works all fine.

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

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 doesn't point to the location I assigned. When I println() out the location println(mapView.myLocation), it returns nil.

I assume this is because in iOS8, we explicitly have 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 ask for the permission on CLLocationManager since I'm using Google Maps SDK. I put the below code just to check but it didn't prompt the permission dialog.

let locationManager = CLLocationManager()
locationManager.requestWhenInUseAuthorization()

Is there a workaround this? Or do we have to wait until Google update their SDK?

Thank you.

like image 938
Isuru Avatar asked Oct 04 '14 11:10

Isuru


1 Answers

You need your CLLocationManager to be retained, otherwise it is released before it gets a chance to present the authorisation 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
}
like image 157
Paulw11 Avatar answered Nov 11 '22 21:11

Paulw11