getting user location and creating map not in sync - ios

The below code is getting the user current location and creating a mapbox map.
problem is that the mapbox map is being created before the users location is obtain.
How may I slow or sync this process?
Thank you in advance,
import UIKit
import CoreLocation
import MapboxGL
class AViewController: UIViewController, CLLocationManagerDelegate {
var manager:CLLocationManager!
var userLocation:CLLocation = CLLocation(latitude: 25.776243, longitude: -80.136509)
override func viewDidLoad() {
super.viewDidLoad()
println("inside viewdidload")
self.getUserLocation()
}//eom
override func viewDidAppear(animated: Bool) {
println("inside viewdidappear")
self.createMapBoxMap()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
/*** MapBox Functions ************************************************************************/
/*Create preliminary map */
func createMapBoxMap(){
// set your access token
let mapView = MGLMapView(frame: view.bounds, accessToken: "pk.eyJ1IjoiZGFya2ZhZGVyIiwiYSI6IlplVDhfR3MifQ.pPEz732qS8g0WEScdItakg")
mapView.autoresizingMask = .FlexibleWidth | .FlexibleHeight
// set the map's center coordinate
mapView.setCenterCoordinate(CLLocationCoordinate2D(latitude: self.userLocation.coordinate.latitude, longitude: self.userLocation.coordinate.longitude),
zoomLevel: 13, animated: false)
view.addSubview(mapView)
//showing the user location on map - blue dot
mapView.showsUserLocation = true
}//eom
/*** location Functions ************************************************************************/
/*getting user current location*/
func getUserLocation(){
self.manager = CLLocationManager()
self.manager.delegate = self
self.manager.desiredAccuracy = kCLLocationAccuracyBest
self.manager.requestWhenInUseAuthorization()
self.manager.startUpdatingLocation()
}//eom
/*location manager 'didUpdateLocations' function */
func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {
self.manager.stopUpdatingLocation() //stop getting user location
println(locations)
self.userLocation = locations[0] as! CLLocation
}//eom
/* errors occurred */
func locationManager(manager: CLLocationManager!, didFailWithError error: NSError) {
println("Error:" + error.localizedDescription)
}//eom
}//eoc

Location manager is running asynchronously (expected behavior), which means that it returns immediately and finishes creating the map as soon as the create map box method is called. However, it doesn't mean that the map has gotten a location yet. I'm thinking that the best way to implement this would be to move self.createMapBoxMap() to inside didUpdateLocations and try that. From my experience, you want the creation of your view to happen in a callback from an asynchronous method like this. You might want to have some type of loading view though, because the user will be confused at first.

Related

App crashes when run more than once on GMSMAPVIEW

I have an app that finds places near the user’s location, however, the app crashes the second time it runs with the exception:
fatal error: unexpectedly found nil while unwrapping an Optional value.
On line:
self.googleMapView.animate(toLocation: coordinates)
I checked and the googleMapView is nil but I don’t understand how it is nil or how it ran the first time. It only starts crashing on subsequent tries if I delete and reinstall the app it works fine on the first try but after that it keeps crashing even if I restart the app. Full code below
import UIKit
import GoogleMaps
import GooglePlacePicker
import MapKit
class MapViewController: UIViewController, CLLocationManagerDelegate {
var currentLongitude: CLLocationDegrees = 0
var currentLatitude: CLLocationDegrees = 0
var locationManager: CLLocationManager!
var placePicker: GMSPlacePickerViewController!
var googleMapView: GMSMapView!
#IBOutlet weak var mapViewContainer: MKMapView!
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
self.googleMapView = GMSMapView(frame: self.mapViewContainer.frame)
self.googleMapView.animate(toZoom: 18.0)
self.view.addSubview(googleMapView)
}
override func viewDidLoad() {
super.viewDidLoad()
self.locationManager = CLLocationManager()
self.locationManager.delegate = self
self.locationManager.requestAlwaysAuthorization()
self.locationManager.requestWhenInUseAuthorization()
self.locationManager.startUpdatingLocation()
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
if let location:CLLocation = locations.last {
self.currentLatitude = location.coordinate.latitude
self.currentLongitude = location.coordinate.longitude
}
else {
print("Location Error")
}
let coordinates = CLLocationCoordinate2DMake(self.currentLatitude, self.currentLongitude)
let marker = GMSMarker(position: coordinates)
marker.title = "I am here"
marker.map = self.googleMapView
self.googleMapView.animate(toLocation: coordinates)
}
private func locationManager(manager: CLLocationManager,
didFailWithError error: Error){
print("An error occurred while tracking location changes : \(error.localizedDescription)")
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
Crash is pretty self-explanatory:
You are setting your location's delegate in the viewDidLoad but creating the map in the viewDidAppear.
If the location was known by iOS, you will receive the message before viewDidAppear call so in the line : self.googleMapView.animate(toLocation: coordinates), your map is still nil
You can either define your map as optional : var googleMapView: GMSMapView?, or you can wait for your map to be defined to create your location Manager.

How to add current timestamp in camera in ios app

Something like this I want to add current timestamp when recording is done in the video and save into camera roll.
**Note **: I have successfully added current location and timestamp in AVVideoPreviewLayer but it is static after exporting video is show static time does not show running timestamp
Try creating a layer of previewLayer: UIView with the components you want to have on camera.
Add this layer to currently running session of AVCaptureVideoPreviewLayer.
This link might help you to achieve your problem
Adding objects over camera view in app (Swift 3) not under camera view?
I tried something out. I guess you want to get latitude, longitude and the timestamp.
You find an example for latitude and longitude below.
import UIKit
import CoreLocation
class ViewController: UIViewController, CLLocationManagerDelegate {
var locationManager: CLLocationManager!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
#IBAction func showLocation(_ sender: Any) {
locationManager = CLLocationManager()
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.requestAlwaysAuthorization()
if(CLLocationManager.locationServicesEnabled()){
locationManager.startUpdatingLocation()
}
locationManager.stopUpdatingLocation()
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let userLocation:CLLocation = locations[0] as CLLocation
print("user latitude = \(userLocation.coordinate.latitude)")
print("user longitude = \(userLocation.coordinate.longitude)")
}
func locationManager(_ manager: CLLocationManager, didFailWithError error: Error)
{
print("Error \(error)")
}
}
To get the current time you can use something like this:
let date = NSDate()
print(date)
The result for both in console should look something like this:
2017-12-17 21:45:39 +0000
user latitude = 37.3322499
user longitude = -122.056264
(The time looks like this in my example because im from EU but you can convert it to what you want)
You can add this to a separate view and bring it to front with
view.bringSubview(toFront: YourView)
I hope this is helpful!

Swift - User Location Issues

I've just started with swift and I'm having an issue. I've read the various threads about user location and map kits and can't solve my issue. I had the code running and could create regions as I wanted and I could zoom into the user location.
I've paired the code back to try and locate the issue and the code left is below. The issue is that the userlocation is coming back as a nil value when you try and run the simulator which crashes the app. What am I doing wrong as I've completed authorising user location so surely it shouldn't be coming back nil. At one point I had code to zoom on the user location AFTER initially setting a region elsewhere and calling a function to do the zoom, but if you initially try and call the user location its always nil so you can't initialise the map zooming into where the user is which is what I want.
import UIKit
import MapKit
class MapController: UIViewController, CLLocationManagerDelegate {
#IBOutlet weak var mapView: MKMapView!
// MARK: - location manager to authorize user location for Maps app
var locationManager = CLLocationManager()
func checkLocationAuthorizationStatus() {
if CLLocationManager.authorizationStatus() == .AuthorizedWhenInUse {
mapView.showsUserLocation = true
} else {
locationManager.requestWhenInUseAuthorization()
}
}
override func viewDidLoad() {
super.viewDidLoad()
checkLocationAuthorizationStatus()
var userLocation = locationManager.location
println("\(userLocation.coordinate.latitude)")
println("\(userLocation.coordinate.longitude)")
// Do any additional setup after loading the view.
}
}
Firstly, CLLocationManager updates user location asynchronously. That means that even after you call startUpdatingLocation() your location will be nil until location manager returns with the new location.
Secondly, in your code you are not actually calling this method. If you DO need to be able to store the user location then you should change your code to:
import UIKit
import MapKit
class MapController: UIViewController, CLLocationManagerDelegate {
#IBOutlet weak var mapView: MKMapView!
// MARK: - location manager to authorize user location for Maps app
lazy var locationManager: CLLocationManager = {
var manager = CLLocationManager()
manager.delegate = self
return manager
}()
func checkLocationAuthorizationStatus() {
if CLLocationManager.authorizationStatus() == .AuthorizedWhenInUse {
mapView.showsUserLocation = true
locationManager.startUpdatingLocation()
} else {
locationManager.requestWhenInUseAuthorization()
}
}
override func viewDidLoad() {
super.viewDidLoad()
checkLocationAuthorizationStatus()
//location is nil at this point because location update is
//an asynchronous operation!
//var userLocation = locationManager.location
//println("\(userLocation.coordinate.latitude)")
//println("\(userLocation.coordinate.longitude)")
// Do any additional setup after loading the view.
}
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
if let location = locations.last {
//this is the place where you get the new location
println("\(location.coordinate.latitude)")
println("\(location.coordinate.longitude)")
}
}
There is only one minor thing to note. In the last function I am using an argument locations: [CLLocation]. This is definitely correct in Swift 2.0, but in Swift 1.2 it might be locations: [AnyObject] in which case you have to do a conditional downcast yourself.
Let me know if this works for you

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.

Google Map SDK, iOS , cannot get myLocation

I'd like to show my location on iOS app by using Google Maps SDK. However, it cannot get my location. I referred the following documents, document1, document2
This is my code. It only shows the map of United Kingdom.
Please help me to solve the problem.
import UIKit
class SearchVC: UIViewController,CLLocationManagerDelegate{
///Google Map
#IBOutlet weak var mapView:GMSMapView!
let locationManager = CLLocationManager()
override func viewDidLoad() {
super.viewDidLoad()
locationManager.delegate = self
locationManager.requestWhenInUseAuthorization()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func locationManager(manager:CLLocationManager!, didChangeAuthorizationStatus status: CLAuthorizationStatus){
if status == .AuthorizedWhenInUse{
locationManager.startUpdatingLocation()
mapView.myLocationEnabled = true
mapView.settings.myLocationButton = true
}
}
func locationManager(manager:CLLocationManager!, didUpdateLocations locations:[AnyObject]!){
if let location = locations.first as? CLLocation{
mapView.camera = GMSCameraPosition(target:location.coordinate, zoom:15,bearing:0, viewingAngle:0)
locationManager.stopUpdatingLocation()
}
}
}
Heres some code to parse your location... I think your just having an issue extracting the location info for the map view to load
func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {
CLGeocoder().reverseGeocodeLocation(manager.location, completionHandler: { (placemarks, error) -> Void in
if (error != nil) {
println("Error:" + error.localizedDescription)
return
}
if placemarks.count > 0 {
let pm = placemarks[0] as CLPlacemark
pm.location.coordinate;
mapView.camera = GMSCameraPosition(target:pm.location.coordinate, zoom:15,bearing:0, viewingAngle:0)
locationManager.stopUpdatingLocation()
}else {
println("Error with data")
}
}
I haven't compiled this code and I'm not swift savvy but hopefully this helps
To change your location go to Edit Scheme...
Then select whatever location you want to simulate
The problem:
func locationManager(manager:CLLocationManager!, didChangeAuthorizationStatus status: CLAuthorizationStatus)
This function is only truly called when the authorization status changes. For instance when you first run the app, it will ask to enable location services. Once you hit yes, this function will run. Next time you run the app, the authorization status won't change, because it was previously enabled.
The fix:
override func viewDidLoad() {
super.viewDidLoad()
locationManager.requestWhenInUseAuthorization()
if CLLocationManager.locationServicesEnabled() {
locationManager.delegate = self
locationManager.startUpdatingLocation()
mapView.myLocationEnabled = true
mapView.settings.myLocationButton = true
}
}
Now everytime the view loads, it will start updating location as opposed to when the authorization status changes. Make sure that you also add the key: value pair into info.plist (NSLocationWhenInUseUsageDescription: {authorizatinon message string}).

Resources