Reputation: 1403
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
Upvotes: 1
Views: 702
Reputation: 625
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")
}
Upvotes: 2
Reputation: 6508
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.
Upvotes: 0