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 {
Related
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?
I have many different buttons like UIStepper and DatePicker and a MapView. Can anyone help me get my whole view controller and the button working. I have been getting an error about SIGABRT. It say's "Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Field_Service_Report.AddController hoursStepper:]: unrecognized selector sent to instance 0x7fb5d7c8d010'"
This is my View Controller code :
import UIKit
import MapKit
import CoreLocation
class AddController: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate {
#IBOutlet weak var hoursStepperResult: UILabel!
#IBOutlet weak var hoursStepper: UIStepper!
#IBOutlet weak var minutesStepperResult: UILabel!
#IBOutlet weak var minutesStepper: UIStepper!
#IBOutlet weak var currentLocationLabel: UILabel!
#IBOutlet weak var dateOfReport: UIDatePicker!
#IBOutlet weak var mapView: MKMapView!
let locationManager = CLLocationManager()
override func viewDidLoad() {
super.viewDidLoad()
// Start Map Contents
self.locationManager.delegate = self
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest
self.locationManager.requestWhenInUseAuthorization()
self.locationManager.startUpdatingLocation()
self.mapView.showsUserLocation = true
// End Map Contents
}
// Start Stepper Contents
#IBAction func hoursStepperAction(_ sender: UIStepper) {
hoursStepperResult.text = String(Int(sender.value))
}
#IBAction func minutesStepperAction(_ sender: UIStepper) {
minutesStepperResult.text = String(Int(sender.value))
}
// End Stepper Contents
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// Here is the new code its giving me 3 erroe because im trying to get the text and numbers from another ViewController to print out in this VC.
#IBAction func doneACButton(_ sender: AnyObject) {
let newVC = storyboard?.instantiateViewController(withIdentifier: "ResultController") as! ResultController
newVC.intPassed = hoursIntProvided
newVC.intPassed = minutesIntProvided
newVC.stringPassed = resultedHours.text!
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let location = locations.last
let center = CLLocationCoordinate2D(latitude: location!.coordinate.latitude, longitude: location!.coordinate.longitude)
let region = MKCoordinateRegion(center: center, span: MKCoordinateSpan(latitudeDelta: 1, longitudeDelta: 1))
self.mapView.setRegion(region, animated: true)
self.locationManager.stopUpdatingLocation()
}
func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
print("Errors: " + error.localizedDescription)
}
// MARK: - Navigation
}
Notice the colon right here in the method signature:
-[Field_Service_Report.AddController hoursStepper:]
^
This is the signature of a method that takes a parameter, not the signature of an accessor. Are you sure you didn't misspell a selector name somewhere or leave something accidentally connected in IB that should have been removed? (Perhaps if you renamed a method and forgot to redo the action connection?)
I have followed the steps in tutorials how to get user's location via Google Maps SDK. I used the tutorial provided by Google also by Ray Wenderlich but it's no use.
The location button doesn't even show.
Any help would be great, thanks.
Here's my code:
class NearestDealerViewController: UIViewController, CLLocationManagerDelegate {
#IBOutlet weak var mainMenuLabel: UILabel!
#IBOutlet weak var nearestDealer: UILabel!
#IBOutlet weak var mapView: GMSMapView!
let locationManager = CLLocationManager()
let transitionDelegate = TransitionDelegate()
var delegate: NearestDealerViewControllerDelegate?
override func viewDidLoad() {
super.viewDidLoad()
locationManager.delegate = self
locationManager.requestWhenInUseAuthorization()
println(mapView.myLocationEnabled)
}
func locationManager(manager: CLLocationManager!, didChangeAuthorizationStatus status: CLAuthorizationStatus) {
if status == .AuthorizedWhenInUse {
locationManager.startUpdatingLocation()
mapView.myLocationEnabled = true
mapView.settings.myLocationButton = true
}
println(mapView.myLocationEnabled)
}
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()
}
}
#IBAction func backTapped(sender: AnyObject) {
delegate?.dismissNearestDealerViewController()
}
}
You should put in the viewDidLoad() next line of code:
mapView.isMyLocationEnabled = true
I've tried following simple CoreLocation examples using Swift, but I'm unable to get things working. I've extracted most of the code out, so it's just barebones in order to focus the question, and I'm hoping the answer jumps out quickly to someone.
import UIKit
import CoreLocation
class ViewController: UIViewController,CLLocationManagerDelegate {
#IBOutlet weak var txtLatitude: UITextField!
#IBOutlet weak var txtLongitude: UITextField!
#IBOutlet weak var cmdLocateMe: UIButton!
#IBOutlet weak var slider: UISlider!
#IBOutlet weak var txtSlider: UITextField!
#IBOutlet weak var power: UISwitch!
var locationManager: CLLocationManager!
override func viewDidLoad() {
locationManager = CLLocationManager();
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.startUpdatingLocation();
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 valueChanged(sender: UISlider) {
if power.on{
var val = "\(slider.value)";
txtSlider.text = val;
}
}
#IBAction func cmdLocateMeClicked(sender: UIButton) {
}
//Deprecated
#IBAction func cmdLocateMePressed(sender: AnyObject) {
}
//CLLocationManagerDelegate
func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {
var location:CLLocation = locations[locations.count-1] as CLLocation
println("locations = \(locations)")
txtLatitude.text = "\(location.coordinate.latitude)";
txtLongitude.text = "\(location.coordinate.longitude)";
}
func locationManager(manager: CLLocationManager!, didFailWithError error: NSError!) {
println(error)
txtLatitude.text = "Can't get your location!"
}
}
Figured it out -- I had to give permissions to my app to receive Location Information on the iOS Simulator (Settings -> Privacy -> Location). Once I did that, things worked perfectly.
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