I got stack with such issue, trying to make a simple app, using CoreLocation.
I wrote all necessary keys into info.plist
import UIKit
import CoreLocation
class ViewController: UIViewController, CLLocationManagerDelegate {
var locationManager = CLLocationManager()
override func viewDidLoad() {
super.viewDidLoad()
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.requestAlwaysAuthorization()
locationManager.startUpdatingLocation()
// 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.
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
print (locations)
}
}
However, application doesn't write anything to console. Are there any special moments, working with location is ios 10?
Related
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.
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 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.
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
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. :)