0

I'm trying to follow this Google Maps tutorial: http://www.raywenderlich.com/81103/introduction-google-maps-ios-sdk-swift

Like many others I have hit a roadblock where CLLocationManager does not seem to fire startUpdatingLocation().

I have updated the pList with NSLocationAlwaysUsageDescription and NSLocationWhenInUseUsageDescription in accordance with Location Services not working in iOS 8, but still no location being fired.

Code below - any help is appreciated!

import UIKit

class MapViewController: UIViewController, TypesTableViewControllerDelegate, CLLocationManagerDelegate {

    @IBOutlet weak var mapView: GMSMapView!
    @IBOutlet weak var mapCenterPinImage: UIImageView!
    @IBOutlet weak var pinImageVerticalConstraint: NSLayoutConstraint!
    var searchedTypes = ["bakery", "bar", "cafe", "grocery_or_supermarket", "restaurant"]
    let locationManager = CLLocationManager()

    override func viewDidLoad() {
        super.viewDidLoad()
        locationManager.delegate = self
        locationManager.requestWhenInUseAuthorization()
    }

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        if segue.identifier == "Types Segue" {
            let navigationController = segue.destinationViewController as UINavigationController
            let controller = segue.destinationViewController.topViewController as TypesTableViewController
            controller.selectedTypes = searchedTypes
            controller.delegate = self
        }
    }

    // MARK: - Types Controller Delegate
    func typesController(controller: TypesTableViewController, didSelectTypes types: [String]) {
        searchedTypes = sorted(controller.selectedTypes)
        dismissViewControllerAnimated(true, completion: nil)
    }

    func locationManager(manager: CLLocationManager!, didChangeAuthorizationStatus status: CLAuthorizationStatus) {

        if status == .AuthorizedWhenInUse {
            println("success") // Works
            locationManager.startUpdatingLocation() // Does not seem to fire

            mapView.myLocationEnabled = true
            mapView.settings.myLocationButton = true
        }
    }

    func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {
        println("test") // Does not work
        println(locations.count) // Does not work

        if let location = locations.first as? CLLocation {

            mapView.camera = GMSCameraPosition(target: location.coordinate, zoom: 15, bearing: 0, viewingAngle: 0)

            locationManager.stopUpdatingLocation()
        }
    }  
}
Community
  • 1
  • 1
Peder Wessel
  • 646
  • 1
  • 9
  • 23
  • Possible duplicate of http://stackoverflow.com/questions/24062509/location-services-not-working-in-ios-8?lq=1 – Hemant Singh Rathore Apr 01 '15 at 12:25
  • Hernant - agreed. This is why I refer to it in the Question. Code is fine, but issue is that you have to change where you simulate your location for `didUpdateLocations` to fire. Which is not mentioned in the referred Question. Others might face same issue - to avoid that - I would keep the question open – Peder Wessel Apr 01 '15 at 12:40

2 Answers2

0

Unbelievable. After a day of testing I found the solution.. Code is fine. Simulation requires that you change the location

Steps:

  1. In Xcode Simulator --> Debug --> Location --> Apple (or whatever..)
  2. Re-run simulation

Google Map will now be centered on where you chose in your simulation settings (Xcode --> Edit Scheme --> Allow Simulation + Default Location = New York)

Hope this helps someone else!

Peder Wessel
  • 646
  • 1
  • 9
  • 23
0

The code seems okay also you have mentioned that you have updated info.plist. Can you check with "requestAlwaysAuthorization" instead of "requestWhenInUseAuthorization". Hope it may solve your problem.

You can also refer this link - http://nevan.net/2014/09/core-location-manager-changes-in-ios-8/

Uttam Sinha
  • 722
  • 6
  • 9
  • This would be more helpful to others with this problem if you would add some explanation of what to do with this code snippet and how it will fix the problem. – Brian Apr 01 '15 at 21:35