Location Data from CoreLocation swift - ios

I'm trying to get the users coordinates, but I cant work out how to retrieve them? Here is my code for finding their location on a map, what should I add to get numerical values for longitude/latitude? It works fine, and I've added the requestAuthorization key into info.plist.I've tried using the CLLocation object, but I cant seem to get it to work.
import UIKit
import MapKit
class ViewController: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate {
#IBOutlet weak var Map: MKMapView!
var locationMgr = CLLocationManager()
override func viewDidLoad() {
super.viewDidLoad()
locationMgr.delegate = self
locationMgr = CLLocationManager()
locationMgr.requestAlwaysAuthorization()
locationMgr.startUpdatingLocation()
Map.showsUserLocation = true
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}

Implement the CLLocationManager delegate protocol methods, specifically:
func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!)

You've initialised the location manager twice. so the delegate you set after first time the manager initialised is nil. so delete this locationMgr = CLLocationManager() in your viewDidLoad method. it should work

Related

How do I have Mapbox annotations show up with current location information

I was wondering if anyone may know how to make Mapbox annotations be able show the current location information once dropped on a certain spot. Kind of like it does in Apple's maps when you tap on a pin or drop one and it then shows the locations name or address. I have the code for the pin being dropped but after that I don't know how to make it show information on that current location. Thank you for your feedback in advance.
import UIKit
import CoreLocation
import Mapbox
class SecondViewController: UIViewController, CLLocationManagerDelegate, MGLMapViewDelegate {
#IBOutlet var mapView: MGLMapView!
let manager = CLLocationManager()
#IBOutlet weak var tableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
manager.delegate = self
manager.desiredAccuracy = kCLLocationAccuracyBest
manager.requestWhenInUseAuthorization()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
#IBAction func markStuff(_ sender: Any) {
manager.startUpdatingLocation()
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let location = locations[0]
let center = CLLocationCoordinate2D(latitude: location.coordinate.latitude, longitude: location.coordinate.longitude)
mapView.setCenter(center, zoomLevel: 15, animated: true)
let annotation = MGLPointAnnotation()
annotation.coordinate = location.coordinate
self.mapView.addAnnotation(annotation)
manager.stopUpdatingLocation()
}
}
You’ll want to reverse geocode the coordinate of the annotation. The Mapbox iOS SDK does not offer this capability out of the box, but we do provide a library called MapboxGeocoder.swift.

locationManager Delegate not working

I have included NSLocationWhenInUsageDescription and NSLocationAlwaysUsageDescription in my info.plst file and my code is like this.
import UIKit
import MapKit
class LocationSelectorViewController: UIViewController,
UISearchBarDelegate, MKMapViewDelegate {
#IBOutlet weak var locationSearchBar: UISearchBar!
#IBOutlet weak var mapView: MKMapView!
var selectedLoc: String?
let locationManager = CLLocationManager()
override func viewDidLoad() {
super.viewDidLoad()
locationManager.requestAlwaysAuthorization()
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
locationManager.startUpdatingLocation()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func locationManager(manager: CLLocationManager, didupdateLocations locations: [CLLocation]) {
}
func locationManager(manager: CLLocationManager, didFailWithError error: NSError) {
}
func locationManager(manager: CLLocationManager, didChangeAuthorizationStatus status: CLAuthorizationStatus) {
}
}
I think I have nothing wrong but it is keep showing this error
Cannot Assign value of type "LocationSelectorViewController" to type "CLLocationManagerDelegate?"
Can you tell me where is the Problem?
Cannot Assign value of type "LocationSelectorViewController" to type "CLLocationManagerDelegate?"
Ans :
Your LocationSelectorViewController class must adopt and conform toCLLocationManagerDelegate protocol.
Add the CLLocationManagerDelegate in your class as shown below.
class LocationSelectorViewController: UIViewController,
UISearchBarDelegate, MKMapViewDelegate, CLLocationManagerDelegate {
instead of
class LocationSelectorViewController: UIViewController,
UISearchBarDelegate, MKMapViewDelegate {

I want to access a var in a second view controller in swift code

Hi here is my code so far
import UIKit
import MapKit
import CoreLocation
class ViewController: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate {
#IBOutlet var map: MKMapView!
var locationManager = CLLocationManager()
override func viewDidLoad() {
super.viewDidLoad()
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.requestWhenInUseAuthorization()
locationManager.startUpdatingLocation()
}
func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {
var userLocation:CLLocation = locations[0] as! CLLocation
var latitude = userLocation.coordinate.latitude
var longitude = userLocation.coordinate.longitude
var latDelta:CLLocationDegrees = 0.05
var lonDelta:CLLocationDegrees = 0.05
var span:MKCoordinateSpan = MKCoordinateSpanMake(latDelta, lonDelta)
var location:CLLocationCoordinate2D = CLLocationCoordinate2DMake(latitude, longitude)
var region:MKCoordinateRegion = MKCoordinateRegionMake(location, span)
self.map.setRegion(region, animated: false)
println(latitude)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
I would like to access the info in the locationManager function in a second view controller showing the latitude, longitude, course and speed. Am a newbie so any help would be great. cheers
Move your variables you want to pass to the second VC from local to class-scope:
import UIKit
import MapKit
import CoreLocation
class ViewController: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate {
#IBOutlet var map: MKMapView!
var locationManager = CLLocationManager()
var longitude:CLLocationDegrees!
var latitude:CLLocationDegrees!
override func viewDidLoad() {
super.viewDidLoad()
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.requestWhenInUseAuthorization()
locationManager.startUpdatingLocation()
}
Create your Target UIViewController with variables to hold the passed values:
import UIKit
import MapKit
import CoreLocation
class MyDestinationViewController: UIViewController {
var longitude:CLLocationDegrees!
var latitude:CLLocationDegrees!
override func viewDidLoad() {
super.viewDidLoad()
}
}
Add the prepareForSegue method to your viewcontroller from which you want to send the data:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
let destinationVC = segue.destinationViewController as! MyDestinationViewController
destinationVC.latitude = latitude
destinationVC.longitude = longitude
}
If you do not want to use a segue, you can just instanciate the targetViewController and pass the variables, before presenting the VC.

Cannot get MapView to Zoom in on current location - SWIFT

I have tried all day on this - seems it should be simple - but I cannot get the MapView to zoom in on the current location.
Using "Apple" in the simulator for the location just for testing - I don't get any errors and it runs - but the view just stays at a full picture of the USA with a blue dot - in California representing the Apple location. Any help is appreciated in advance!
Code as follows:
import MapKit
import UIKit
import CoreLocation
class MapViewController: UIViewController, CLLocationManagerDelegate, MKMapViewDelegate {
#IBOutlet weak var mapView: MKMapView!
var locationManager: CLLocationManager!
func createLocationManager () {
locationManager = CLLocationManager()
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.distanceFilter = kCLDistanceFilterNone
}
func locationManager (manager:CLLocationManager!, didUpdateLocations locations:[AnyObject]!) {
if let firstlocation = locations.first as? CLLocation {
mapView.setCenterCoordinate(firstlocation.coordinate, animated: true)
let region = MKCoordinateSpanMake (1000,1000)
}
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
locationManager = CLLocationManager()
locationManager?.requestWhenInUseAuthorization()
self.mapView.showsUserLocation = true
}
Try Debug->Location->City Bicycle Ride in the simulator. This will give you continual updates.
Also make sure you are setting the locationManager's delegate.

CLLocationManager swift does´t work

Im trying to do a simple app. Just a map with the users location. I search in here the solution, but I think that I have everything and still does´t work.
Here is the code:
import UIKit
import MapKit
import CoreLocation
class ViewController: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate {
#IBOutlet var MapView: MKMapView = nil
var location : CLLocationManager! = nil
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
location = CLLocationManager()
location.delegate = self
location.requestWhenInUseAuthorization()
location.startUpdatingLocation()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
I added NSLoscationWhenInUseUsageDescription and NSLocationAlwaysUsageDescription in info.plist. I activated Debug -> Location -> Custom Location in the iOS simulator.
I am not sure if I have to put something in AppDelegate.swift (It's my first app).
I will be very grateful if you can help me!!
Thanks. :)

Resources