mapView.showsUserLocation = true affects the correctness of gps in my swift app. Why? - ios

In my app I want to know user's real location. I want to show it in couple places as a plain text (as a string of longitude/latitude) and also show it on the map. I have several ui view controllers and one of them contains - now, for testing - a print to the console with current longitude and latitude, it also contains a map view.
I'm fetching the current gps position from my app delegate and put it on the screen.
In my AppDelegate.swift class I have:
var locationManager: CLLocationManager! = nil
var location: CLLocation! = nil
var longitude : Double = 0
var latitude : Double = 0
var location_fixed = false
func application(application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
initLocationManager()
}
func initLocationManager() {
locationManager = CLLocationManager()
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
if CLLocationManager.authorizationStatus() == .AuthorizedAlways {
locationManager!.startUpdatingLocation()
} else {
locationManager!.requestAlwaysAuthorization()
}
}
func locationManager(manager: CLLocationManager, didFailWithError error: NSError) {
print("Failed to initialize GPS: ", error.description)
}
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
location = locations.first!
print("Managed to get a fix on location")
let coord = location.coordinate
longitude = coord.longitude
latitude = coord.latitude
location_fixed = true;
locationManager.stopUpdatingLocation()
}
func locationManager(manager: CLLocationManager, didChangeAuthorizationStatus status: CLAuthorizationStatus) {
switch status {
case .NotDetermined:
print("NotDetermined")
case .Restricted:
print("Restricted")
case .Denied:
print("Denied")
case .AuthorizedAlways:
print("AuthorizedAlways")
locationManager!.startUpdatingLocation()
case .AuthorizedWhenInUse:
print("AuthorizedWhenInUse")
locationManager!.startUpdatingLocation()
}
}
func stopGPS() -> Void
{
print("STOP GPS")
location_fixed = false;
locationManager.stopUpdatingLocation()
}
func startGPS() -> Void
{
locationManager.startUpdatingLocation()
}
func isLocationFixed() -> Bool
{
return self.location_fixed
}
func getLongitude() -> Double
{
return self.longitude
}
func getLatitude() -> Double
{
return self.latitude
}
func getLocation() -> CLLocation
{
return self.location
}
Now in my UIViewController in viewWillAppear method I added asynchronous call to the app delegate methods for checking current gps position:
let priority = DISPATCH_QUEUE_PRIORITY_DEFAULT
dispatch_async(dispatch_get_global_queue(priority, 0)) {
let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
appDelegate.startGPS()
while (!appDelegate.isLocationFixed())
{
sleep(1)
}
dispatch_async(dispatch_get_main_queue()) {
self.fetchRequests(self.radius, lat: appDelegate.getLatitude(), lon: appDelegate.getLongitude())
print("gps here: \(appDelegate.getLatitude()), \(appDelegate.getLongitude())") }
}
and now when I enter this panel and waits for fetching the gps - I see the correct data. So far so good. Now, when I change the gps position in a simulator, then go to some other panel and come back to this one - I see the new data, which is fine.
And now comes the real weird problem. In the same panel I have a map view.
It's embedded in a UIViewController that I put in a container, so I cannot access it from my async call. It has a viewDidLoad() method and when I put there:
mapView.showsUserLocation = true
and run the app, I see the blue dot exactly when the simulator's gps points out, but now my data in the previous async call is not updating correctly.
When I comment out that one particular line of code, I'm getting the correct data shown as gps here:. When I leave it as a part of the code, then this line gps here: shows the same data all the time, even though I'm changing the gps position in a simulator, leaving and entering the panel several times. What's up with that?

Related

Getting location on real device not working

I'm trying to get the user location, running on the simulator, I get the default address, but atleast I know it is working.
I tried to run it on my device but it didn't work.
I try to look for a solution before writing this question but couldn't find something that work for me.
This is my code:
LocationManager:
class LocationManager: NSObject, CLLocationManagerDelegate {
static let shared = LocationManager()
var locationManager: CLLocationManager!
var geoCoder = CLGeocoder()
var callBack:((String)->())?
override init() {
super.init()
locationManager = CLLocationManager()
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.activityType = .other
locationManager.requestWhenInUseAuthorization()
}
func checkIfLocationIsEnabled() -> Bool{
return CLLocationManager.locationServicesEnabled()
}
func getUserLocation(){
locationManager.requestLocation()
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]){
let userLocation: CLLocation = locations[0] as CLLocation
geoCoder.reverseGeocodeLocation(userLocation) { (placemarks, err) in
if let place = placemarks?.last{
self.callBack?(place.name!)
}
}
}
func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
print(error)
}
}
This is my getLocation (just calling the getUserLocation and setting the address I get from the callback):
func getLocation(_ label: UILabel) -> String{
guard let comment = self.mView.addCommentTextField.text else { return ""}
LocationManager.shared.getUserLocation()
var addressString = ""
LocationManager.shared.callBack = { address in
DispatchQueue.main.async {
label.text = "\(address), \(comment)"
addressString = address
}
}
return addressString
}
This is how I call getLocation:
self.mView.inLabel.isHidden = false
self.getLocation(self.mView.inLabel)
Actually looking closer at your code, I see that you are asking permissions like this:
locationManager.requestWhenInUseAuthorization()
But requestWhenInUseAuthorization() is asynchronous call, you need to wait for user response before you can use any location services:
When the current authorization status is CLAuthorizationStatus.notDetermined, this method runs asynchronously and prompts the user to grant permission to the app to use location services.
(source)
Also notice that it will only work if status is notDetermined. Any other status would not trigger it. So firstly:
if CLLocationManager.authorizationStatus() == .authorizedWhenInUse {
// already authorized, can use location services right away
}
if CLLocationManager.authorizationStatus() == .notDetermined {
locationManager.requestWhenInUseAuthorization()
// wait, don't call any location-related functions until you get a response
}
If location permissions are set to anything else, no point to ask for them.
And then your class is already CLLocationManagerDelegate, so:
func locationManager(_ manager: CLLocationManager,
didChangeAuthorization status: CLAuthorizationStatus) {
// do something with new status, e.g.
if status == .authorizedWhenInUse {
// good, now you can start accessing location data
}
// otherwise, you can't

Can't update label after getting location

I have a simple button, when I press the button, I'm making a call to another class, my Location class to get the user's current location.
After getting the location, I want to update a label text I have to show the location.
This is my location class:
class LocationManager: NSObject, CLLocationManagerDelegate {
var locationManager: CLLocationManager!
var geoCoder = CLGeocoder()
var userAddress: String?
override init() {
super.init()
locationManager = CLLocationManager()
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.activityType = .other
locationManager.requestWhenInUseAuthorization()
}
func getUserLocation(completion: #escaping(_ result: String) -> ()){
if CLLocationManager.locationServicesEnabled(){
locationManager.requestLocation()
}
guard let myResult = self.userAddress else { return }
completion(myResult)
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]){
let userLocation: CLLocation = locations[0] as CLLocation
geoCoder.reverseGeocodeLocation(userLocation) { (placemarks, err) in
if let place = placemarks?.last{
self.userAddress = place.name!
}
}
}
func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
print(error)
}
}
and this is where I call the method and updating the label:
func handleEnter() {
mView.inLabel.isHidden = false
location.getUserLocation { (theAddress) in
print(theAddress)
self.mView.inLabel.text = "\(theAddress)"
}
}
My problem is that when I click my button (and firing handleEnter()), nothing happens, like it won't register the tap. only after tapping it the second time, I get the address and the labels update's.
I tried to add printing and to use breakpoint to see if the first tap registers, and it does.
I know the location may take a few seconds to return an answer with the address and I waited, but still, nothing, only after the second tap it shows.
It seems like in the first tap, It just didn't get the address yet. How can I "notify" when I got the address and just then try to update the label?
Since didUpdateLocations & reverseGeocodeLocation methods are called asynchronously, this guard may return as of nil address
guard let myResult = self.userAddress else { return }
completion(myResult)
Which won't trigger the completion needed to update the label , instead you need
var callBack:((String)->())?
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]){
let userLocation: CLLocation = locations[0] as CLLocation
geoCoder.reverseGeocodeLocation(userLocation) { (placemarks, err) in
if let place = placemarks?.last{
callBack?(place.name!)
}
}
}
Then use
location.callBack = { [weak self] str in
print(str)
DispatchQueue.main.async { // reverseGeocodeLocation callback is in a background thread
// any ui
}
}

Retrieving the current location of the user/ retrieving the values of longitude and latitude watchkit/ios swift

I want to obtain the current location (longitude and latitude) of the user, but I just get 0 for both of the variables. I do not know where the problem lies in. I am following this tutorial and have updated the infop.list of the app folder and the watch extension folder (NSLocationWhenInUseUsageDescription and NSLocationAlwaysUsageDescription), inserted the needed Watchconnector to both of the folders with cocoapods, and I also have added the Corelocation framework via link binary with libraries.
I have little knowledge about swift and watch os, I hope anyone can help.
Here is the project file: https://ufile.io/l5elkpsw
Thank you very much.
And here are my Appdelegate and Interfacecontroller file:
import UIKit
import CoreLocation
import MapKit
#UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, CLLocationManagerDelegate {
let locationManager:CLLocationManager = CLLocationManager()
var currentLocation = CLLocation()
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
WatchConnector.shared.activateSession()
self.locationManager.delegate = self
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest
self.locationManager.requestLocation()
self.locationManager.requestWhenInUseAuthorization()
self.locationManager.startUpdatingLocation()
return true
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
if locations.count == 0
{
return
}
self.currentLocation = locations.first!
let message = ["lat":self.currentLocation.coordinate.latitude,"long":self.currentLocation.coordinate.longitude]
WatchConnector.shared.sendMessage(message, withIdentifier: "sendCurrentLocation") { (error) in
print("error in send message to watch\(error.localizedDescription)")
}
}
func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
print("Fail to load location")
print(error.localizedDescription)
}
func applicationWillResignActive(_ application: UIApplication) {
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
}
func applicationDidEnterBackground(_ application: UIApplication) {
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}
func applicationWillEnterForeground(_ application: UIApplication) {
// Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
}
func applicationDidBecomeActive(_ application: UIApplication) {
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}
func applicationWillTerminate(_ application: UIApplication) {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
}
import WatchKit
import Foundation
import CoreLocation
class InterfaceController: WKInterfaceController{
private let locationAccessUnauthorizedMessage = "Locations Disabled\n\nEnable locations for this app via the Settings in your iPhone to see meetups near you!"
private let pendingAccessMessage = "Grant location access to GPS dummy"
#IBOutlet weak var map: WKInterfaceMap!
#IBOutlet weak var button: WKInterfaceButton!
#IBOutlet weak var latitudeL: WKInterfaceLabel!
#IBOutlet weak var longitudeL: WKInterfaceLabel!
#IBOutlet weak var authorizeL: WKInterfaceLabel!
var currentLocation = CLLocation()
let locationManager = CLLocationManager()
var lat: Double = 0.0
var long: Double = 0.0
override func awake(withContext context: Any?) {
super.awake(withContext: context)
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
// Configure interface objects here.
}
override func willActivate() {
// This method is called when watch view controller is about to be visible to user
super.willActivate()
WatchConnector.shared.listenToMessageBlock({ (message) in
self.lat = message["lat"] as! Double
self.long = message["long"] as! Double
print(self.lat)
print(self.long)
self.currentLocation = CLLocation(latitude: self.lat as! CLLocationDegrees, longitude: self.long as! CLLocationDegrees)
let mylocation : CLLocationCoordinate2D = CLLocationCoordinate2DMake(self.currentLocation.coordinate.latitude, self.currentLocation.coordinate.longitude)
let span = MKCoordinateSpan(latitudeDelta: 0.1, longitudeDelta: 0.1)
let region = MKCoordinateRegion(center: mylocation, span: span)
self.map.setRegion(region)
self.map.addAnnotation(mylocation, with: .red)
}, withIdentifier: "sendCurrentLocation")
let authorizationStatus = CLLocationManager.authorizationStatus()
handleLocationServicesAuthorizationStatus(authorizationStatus)
}
func handleLocationServicesAuthorizationStatus(_ status: CLAuthorizationStatus) {
switch status {
case .notDetermined:
print("handleLocationServicesAuthorizationStatus: .undetermined")
handleLocationServicesStateNotDetermined()
case .restricted, .denied:
print("handleLocationServicesAuthorizationStatus: .restricted, .denied")
handleLocationServicesStateUnavailable()
case .authorizedAlways, .authorizedWhenInUse:
print("handleLocationServicesAuthorizationStatus: .authorizedAlways, .authorizedWhenInUse")
handleLocationServicesStateAvailable(status)
}
}
func handleLocationServicesStateNotDetermined() {
authorizeL.setText(pendingAccessMessage)
locationManager.requestWhenInUseAuthorization()
}
func handleLocationServicesStateUnavailable() {
authorizeL.setText(locationAccessUnauthorizedMessage)
}
func handleLocationServicesStateAvailable(_ status: CLAuthorizationStatus) {
switch status {
case .authorizedAlways:
locationManager.startUpdatingLocation()
case .authorizedWhenInUse:
locationManager.requestLocation()
default:
break
}
}
override func didDeactivate() {
// This method is called when watch view controller is no longer visible
super.didDeactivate()
}
#IBAction func btnPressed() {
self.latitudeL.setText("\(self.lat)")
self.longitudeL.setText("\(self.long)")
print("\(locationManager.requestLocation())")
print("\(self.lat)")
print("\(self.long)")
}
}
extension InterfaceController: CLLocationManagerDelegate {
func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
print("error:: \(error.localizedDescription)")
}
func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
if status == .authorizedWhenInUse {
locationManager.requestLocation()
}
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
if locations.first != nil {
print("location:: (location)")
}
}
}
Are you testing on a simulator? If so, you might need to set the location to something in the Debug/Location menu:

Retrieve current location when application is in background

I've built an application where you can press a start button. Once the button is pressed the application will get user location every 10 second all the way till the stop button is pressed. When I leave the application or if the screen gets black, it will NOT get any more locations till I re-enter the application.
So, I'm currently trying to update the locations when the application is minimized. (I guess it's called in the background?), and also when the screen turns black. But my questions is:
Should I write this code in the AppDelegate?, if so. How can I know
if the button was pressed or not?
Where exactly in the AppDelegate should I add the code? And how can
I pass the locations back to the correct ViewController? (Since I
cannot make any prepare for segue from AppDelegate)
If you know the answers of this questions, please do not hesitate to answer them. :) I would really appreciate it!
The best way to get user's location in background is to use the Significant-Change Location Service according to apple's documentation put this func in your class:
func startReceivingSignificantLocationChanges() {
let authorizationStatus = CLLocationManager.authorizationStatus()
if authorizationStatus != .authorizedAlways {
// User has not authorized access to location information.
return
}
if !CLLocationManager.significantLocationChangeMonitoringAvailable() {
// The service is not available.
return
}
locationManager.delegate = self
locationManager.startMonitoringSignificantLocationChanges()
}
and also this func:
func locationManager(_ manager: CLLocationManager, didUpdateLocations
locations: [CLLocation]) {
let lastLocation = locations.last!
// Do something with the location.
}
so you just need to call startReceivingSignificantLocationChanges() inside your button and it will call locationManager(_ manager: CLLocationManager,didUpdateLocations locations: [CLLocation]), so do what you want with the location there.
Remember to ask permission to use location and to stop tracking with locationManager.stopMonitoringSignificantLocationChanges()
Take location permission for Always Allow
Set location manager for allowsBackgroundLocationUpdates true
from the above way you can get location in every location changes store this information and it send to server. Below is the sample code
typealias LocateMeCallback = (_ location: CLLocation?) -> Void
/*
LocationTracker to track the user in while navigating from one place to other and store new locations in locations array.
**/
class LocationTracker: NSObject {
static let shared = LocationTracker()
var lastLocation: CLLocation?
var locations: [CLLocation] = []
var previousLocation: CLLocation?
var isPreviousIsSameAsCurrent: Bool {
if let previous = previousLocation, let last = lastLocation {
return previous == last
}
return false
}
var isAggressiveModeOn = false
var locationManager: CLLocationManager = {
let locationManager = CLLocationManager()
locationManager.allowsBackgroundLocationUpdates = true
locationManager.pausesLocationUpdatesAutomatically = true
locationManager.activityType = .automotiveNavigation
return locationManager
}()
var locateMeCallback: LocateMeCallback?
var isCurrentLocationAvailable: Bool {
if lastLocation != nil {
return true
}
return false
}
func enableLocationServices() {
locationManager.delegate = self
switch CLLocationManager.authorizationStatus() {
case .notDetermined:
// Request when-in-use authorization initially
locationManager.requestWhenInUseAuthorization()
case .restricted, .denied:
// Disable location features
print("Fail permission to get current location of user")
case .authorizedWhenInUse:
// Enable basic location features
enableMyWhenInUseFeatures()
case .authorizedAlways:
// Enable any of your app's location features
enableMyAlwaysFeatures()
}
}
func enableMyWhenInUseFeatures() {
locationManager.startUpdatingLocation()
locationManager.delegate = self
escalateLocationServiceAuthorization()
}
func escalateLocationServiceAuthorization() {
// Escalate only when the authorization is set to when-in-use
if CLLocationManager.authorizationStatus() == .authorizedWhenInUse {
locationManager.requestAlwaysAuthorization()
}
}
func enableMyAlwaysFeatures() {
enableCoarseLocationFetch()
locationManager.startUpdatingLocation()
locationManager.delegate = self
}
// Enable Rough Location Fetch
func enableCoarseLocationFetch() {
isAggressiveModeOn = false
locationManager.desiredAccuracy = kCLLocationAccuracyKilometer
locationManager.distanceFilter = 100
}
// Enable Aggressive Location Fetch
func enableAggressiveLocationFetch() {
isAggressiveModeOn = true
locationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation
locationManager.distanceFilter = 10
}
func locateMe(callback: #escaping LocateMeCallback) {
self.locateMeCallback = callback
if lastLocation == nil {
enableLocationServices()
} else {
callback(lastLocation)
}
}
func startTracking() {
enableLocationServices()
}
func stopTracking() {
locationManager.stopUpdatingLocation()
}
func resetPreviousLocation() {
previousLocation = nil
}
private override init() {}
}
extension LocationTracker: CLLocationManagerDelegate {
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
print(locations)
guard let location = locations.first else { return }
guard -location.timestamp.timeIntervalSinceNow < 120, // Validate only location fetched recently
location.horizontalAccuracy > 0, // Validate Horizontal Accuracy - Ve means Invalid
location.horizontalAccuracy < 200 // Validate Horizontal Accuracy > 100 M
else {
print("invalid location received OR ignore old (cached) updates")
return
}
self.locations.append(location)
lastLocation = location
if let activeRide = RideManager.shared.activeRide,
let _ = AccessTokenHelper.shared.accessToken,
let activeRideId = activeRide.ride_id,
let type = activeRide.rideStatusTypeOptional,
type == .started {
//Store Location For A particular Ride after Start
LocationUpdater.shared.saveInDataBase(rideId: activeRideId, locations: [location])
}
}
func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
print(error.localizedDescription)
}
func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
enableLocationServices()
}
}
/*
This class having responsibility of Updating the location on server after n second and update path after n second.
**/
class LocationTimer {
static let time: Double = 30
}
/*
class to update locations to server after nth second
**/
class LocationUpdater: NSObject {
static let shared = LocationUpdater(n: Double(LocationTimer.time), tracker: LocationTracker.shared)
let n: Double
private let tracker: LocationTracker
var timer: Timer! = nil
init(n: Double, tracker: LocationTracker) {
self.n = n
self.tracker = tracker
super.init()
}
func startUpdater() {
self.timer?.invalidate()
self.timer = nil
self.timer = Timer.scheduledTimer(timeInterval: n, target: self, selector: #selector(updateLocationsToServer), userInfo: nil, repeats: true)
self.timer.fire()
}
func stopUpdater() {
self.timer?.invalidate()
self.timer = nil
}
#objc func updateLocationsToServer() {
// update to server
}
}
// usage
LocationTracker.shared.startTracking()
LocationUpdater.shared.startUpdater()

Swift: Google Map code throwing nil exception

I have the following code and it should basically detect location (if location services are on), then goes back to previous page and shows the coordinates (when button clicked).
#IBOutlet var mapView:GMSMapView!// Adding Google Map check on Storyboard
var locationManager: CLLocationManager!
var viewController : ViewController! //to save the object of previous controller to send the location back
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
locationManager = CLLocationManager() // Intialize the location manager
locationManager.delegate = self
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
//Methos to change the MApType
#IBAction func changeMaptoEarthView()
{
self.mapView.mapType = kGMSTypeSatellite
}
#IBAction func changeMaptoMapView()
{
self.mapView.mapType = kGMSTypeNormal
}
//Method to get UserLocation
#IBAction func sendLocationtoPreviousView()
{
viewController.locationRecieved(locationManager.location)
self.performSegueWithIdentifier("backSegue", sender: nil)
}
#IBAction func addUserLocation() //Detect location click event
{
if(CLLocationManager.authorizationStatus() == .AuthorizedWhenInUse) // If already have location access
{
locationManager.startUpdatingLocation()
}
else if (CLLocationManager.authorizationStatus() == .Denied)// If location access is denied in setting
{
UIAlertView(title: "Permission", message: "Please allow location services in setting", delegate: nil, cancelButtonTitle: "ok").show()
}
else
{
locationManager.requestWhenInUseAuthorization() // Ask user permission if permission not given
}
}
//CLLocation Manager Delegate methoda
func locationManager(manager: CLLocationManager!, didChangeAuthorizationStatus status: CLAuthorizationStatus) {
if status == .AuthorizedWhenInUse {
locationManager.startUpdatingLocation()
}
}
func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {
if let location = locations.first as? CLLocation {
self.mapView.clear() // clear all overlay on Map
self.mapView.camera = GMSCameraPosition(target: location.coordinate, zoom: 15, bearing: 0, viewingAngle: 0)
// Adding pin on Current location
var marker = GMSMarker()
marker.position = location.coordinate
marker.snippet = "My Location"
marker.appearAnimation = kGMSMarkerAnimationPop
marker.map = mapView
locationManager.stopUpdatingLocation()
}
}
The app works in simulator until I say go back, then it throws nil error message on this line:
viewController.locationRecieved(locationManager.location)
What could be the reason? I can see the pin on the map although the map is not showing any graphics except google logo below
If you don't see the map graphic in your view (subclass of GMSMapView), this is because of the Google map API key , maybe you have not set it yet in your app delegate or its not set correctly !
make sure you have set the correct google API key with you app bundle id and your account in google . you have to set below code in your app delete , did didFinishLaunchingWithOptions function :
import GoogleMaps
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
GMSServices.provideAPIKey("your API Key")
}
The location property on CLLocationManager is an Optional - meaning that it can be nil. According to the docs, it will be nil if no location data has ever been retrieved.
You don't show how viewController.locationRecieved is defined, but my guess is that you've configured the CLLocation argument to an implicitly unwrapped optional (i.e., with a "!"). Passing nil to such an argument will cause a crash.

Resources