UILabel will not update to show fetched location coordinates - swift

So I'm just trying to do a simple test in Xcode, where the app will fetch the user's current location and display the coordinates on screen. This can then be updated by pressing a 'Fetch Location' button.
The app doesn't seem to be fetching any coordinates (the UILabel only ever displays default text).
It's just a single-page app. And yes, the #IBOutlet and #IBAction are correctly linked.
import UIKit
import CoreLocation
class ViewController: UIViewController, CLLocationManagerDelegate {
#IBOutlet weak var labelLocation: UILabel!
var locationManager = CLLocationManager()
var myPosition = CLLocationCoordinate2D()
#IBAction func fetchLocation(_ sender: Any) {
locationManager.startUpdatingLocation()
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
locationManager.delegate = self
locationManager.requestWhenInUseAuthorization()
locationManager.startUpdatingLocation()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func locationManager(manager: CLLocationManager!, didUpdateToLocation newLocation: CLLocation!, fromLocation oldLocation: CLLocation!) {
print("Got Location \(newLocation.coordinate.latitude), \(newLocation.coordinate.longitude)")
myPosition = newLocation.coordinate
locationManager.stopUpdatingLocation()
labelLocation.text = "Got Location \(newLocation.coordinate.latitude), \(newLocation.coordinate.longitude)"
}
}

Step 1:
Enable the Privacy - Location When In Use Usage Description in your info.plist file
Go to your info.plist file
Click on + on Information property list and type Privacy - Location When In Use Usage Description
In the value type why you want to request the users location (this will be shown to the user)
Step 2:
Get the location
Use this code:
guard let latitude = manager.location?.coordinate.latitude, let longitude = manager.location?.coordinate.longitude else { return }
locationManager.stopUpdatingLocation()
labelLocation.text = "Got Location \(latitude), \(longitude)"

Ok, I think that you can resolve it adding the "Privacy - Location When In Use Usage Description" key into Info.plist, this is an important step in Location topic.
Regards!

Related

The app's Info.plist must contain an NSLocationWhenInUseUsageDescription key with a string value explaining to the user how the app uses this data

I am new to IOS. I am trying to use Maps to get user's current location. The tutorial i am following is for IOS 10.
I went through this post and did everything it said but still it doesn't work
Location Services not working in iOS 11
Following is my code
class ViewController: UIViewController,CLLocationManagerDelegate,MKMapViewDelegate {
var locationManager=CLLocationManager()
// #IBOutlet weak var mapView: MKMapView!
#IBOutlet weak var mapView: MKMapView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
locationManager.delegate=self
locationManager.desiredAccuracy=kCLLocationAccuracyBest
locationManager.requestWhenInUseAuthorization()
locationManager.startUpdatingLocation()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let userLocation:CLLocation = locations[0]
let latitude=userLocation.coordinate.latitude
let longitude=userLocation.coordinate.longitude
let latDelta:CLLocationDegrees=0.05
let lonDelta:CLLocationDegrees=0.05
let span=MKCoordinateSpan(latitudeDelta:latDelta, longitudeDelta:lonDelta)
let location=CLLocationCoordinate2D(latitude:latitude,longitude:longitude)
let region=MKCoordinateRegion(center:location,span:span)
self.mapView.setRegion(region,animated:true)
}
}
I have added following in my info.plist file
<key>NSLocationAlwaysUsageDescription</key>
<string>Program requires GPS to track cars and job orders</string>
<key>NSLocationAlwaysAndWhenInUseUsageDescription</key>
<string>Program requires GPS to track cars and job orders</string>
<key>NSLocationWhenInUseUsageDescription</key>
<string>Program requires GPS to track cars and job orders</string>
Any help would be greatly appreciated.
If you are using the simulator, make sure you have a location chosen.
Select the simulator, in the menu bar you need to go to Debug -> Location -> And then either do a custom or predefined.
If you've done that, try the below.
Rather than manually entering that text in the info.plist, I would just open it with the default editor.
Click the plus button that is shown in the blue highlighted area ( you can really select any of them) This will add a new row.
Second, Type in Privacy - Location When ... Select the one that applies to you.
After you have the left side filled out, double tap the right side and enter the text you want shown to the user.
There should be three descriptions in info.plst.

the did Update To location is not being called

I've just started working with maps and locations . I've added the required code to the info.plist as (right after <dict>) :
<key>NSLocationAlwaysUsageDescription</key>
<string>Can we use your location?</string>
<key>NSLocationWhenInUseUsageDescription</key>
<string>Can we use your location?</string>
However I cant get the function called and as a result I dont have any output
import UIKit
import CoreLocation
class ViewController: UIViewController , CLLocationManagerDelegate {
var locationManeger = CLLocationManager()
override func viewDidLoad() {
super.viewDidLoad()
locationManeger.delegate = self
locationManeger.requestWhenInUseAuthorization()
locationManeger.startUpdatingLocation()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func locationManager(manager: CLLocationManager, didUpdateToLocation newLocation: CLLocation, fromLocation oldLocation: CLLocation) {
print("got the location ")
}
}
My dear Friend if You are testing in simulator. run your app and select simulator and select Debug tab from upside and in hardware select Location and in location select Apple instead of None
Debug->Location->Apple
its will call your method . but it will show apple as a location but if you want proper location you have to check in iphone
Add only below in plist
<key>NSLocationWhenInUseUsageDescription</key>
<string>Can we use your location?</string>

How do I save annotations?

I have my code so it puts an annotation on the map each time the user clicks a button, but when the user closes out of the app, the annotation disappears. How do I make it so the annotations stays on the map even when the user closes the app? Below is my code:
import UIKit
import CoreLocation
import MapKit
class UpdateCar: UIViewController, CLLocationManagerDelegate {
#IBOutlet weak var mapView: MKMapView!
#IBOutlet weak var lblLocation: UILabel!
var locationManager = CLLocationManager()
var myPosition = CLLocationCoordinate2D()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
locationManager.delegate = self
locationManager.requestWhenInUseAuthorization()
locationManager.startUpdatingLocation()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func locationManager(manager: CLLocationManager!, didUpdateToLocation newLocation: CLLocation!, fromLocation oldLocation: CLLocation!) {
println("Updating Car Location \(newLocation.coordinate.latitude) , \(newLocation.coordinate.longitude) ")
myPosition = newLocation.coordinate
locationManager.stopUpdatingLocation()
lblLocation.text = "\(newLocation.coordinate.latitude) , \(newLocation.coordinate.longitude)"
}
#IBAction func findUserLocationAndDropPin(sender: UIButton) {
var userLocationCoordinates = CLLocationCoordinate2DMake(locationManager.location.coordinate.latitude, locationManager.location.coordinate.longitude)
var pinForUserLocation = MKPointAnnotation()
pinForUserLocation.coordinate = userLocationCoordinates
mapView.addAnnotation(pinForUserLocation)
mapView.showAnnotations([pinForUserLocation], animated: true)
}
}
You have to save it in a persistent store.
Few options:
CoreData, the native way of saving data, recommended, not too easy
NSUserDefaults, usually thought for small stuff, also native, not recommended, very easy though
Another API for managing a persistent store like Realm (similar to CoreData, a little easier but not native)
//when I need to save for example, the last date on which the user login my app will use the setObject function, this will save a value ("10/05/2015") in the "lastlogin" key
var lastLogin = "10/05/2015"
NSUserDefaults.standarUserDefaults().setObject(lastLogin, forkey: "lastLogin")
//And when I need to retrieve the stored value in the "lastlogin" key which I use is "objectForKey" function
NSUserDefaults.standarUserDefaults().objectForKey("lastLogin")
see following link:
https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSUserDefaults_Class/index.html#//apple_ref/occ/instm/NSUserDefaults/setObject:forKey:

How do I save a users location and display it on a map using a pin (or some other marker)?

What I want to do is have a button that the user can click that would save their current location. Then, on a map, a pin would appear where the save location is. How would I go about doing this? I've searched for some sample code but I can't find any that work or are in Swift.
I have it so the user can also see where they are at at all times. Bellow is what I currently have for code.
import UIKit
import CoreLocation
import MapKit
class ViewController: UIViewController, CLLocationManagerDelegate {
var locationManager = CLLocationManager()
#IBOutlet var Map: MKMapView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
locationManager.delegate = self
locationManager.requestWhenInUseAuthorization()
locationManager.startUpdatingLocation()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func locationManager(manager: CLLocationManager!, didUpdateToLocation newLocation: CLLocation!, fromLocation oldLocation: CLLocation!) {
println("Updating Location \(newLocation.coordinate.latitude) , \(newLocation.coordinate.longitude) ")
let span = MKCoordinateSpanMake(0.0009, 0.0009)
let region = MKCoordinateRegion(center: newLocation.coordinate, span: span)
Map.setRegion(region, animated: false)
}
}
What would I add to this code to accomplish my goal that I described in my first paragraph?
Try like this:
Create your button action to findUserLocationAndDropPin():
Capture the userLocationCoordinates using
CLLocationCoordinate2DMake();
Create your pinForUserLocation using MKPointAnnotation();
Assign your pinForUserLocation.coordinate to be equal to
userLocationCoordinates;
Get your mapView and addAnnotation;
Finally ask mapView to showAnnotations.
This code is what I mean and should do that, at least until iOS 8.4 :
#IBAction func findUserLocationAndDropPin(sender: UIButton) {
var userLocationCoordinates = CLLocationCoordinate2DMake(locationManager.location.coordinate.latitude, locationManager.location.coordinate.longitude)
var pinForUserLocation = MKPointAnnotation()
pinForUserLocation.coordinate = userLocationCoordinates
mapView.addAnnotation(pinForUserLocation)
mapView.showAnnotations([pinForUserLocation], animated: true)
}
Good question!
Try adding an array that stores newLocation.coordinate when an IBAction takes place. Then, you can set a pin by using this sample code as an example:
override func viewDidLoad() {
super.viewDidLoad()
// Set map view delegate with controller
self.mapView.delegate = self
let newYorkLocation = CLLocationCoordinate2DMake(40.730872, -74.003066)
// Drop a pin
let dropPin = MKPointAnnotation()
dropPin.coordinate = newYorkLocation
dropPin.title = "New York City"
mapView.addAnnotation(dropPin)
}
But then just set dropPin.coordinate to be the value stored in the array.
Ok, so firstly you want to get permission from the user:
Add the CoreLocation framework
Update info.plist by adding NSLocationWhenInUseUsageDescription
Your code in viewDidLoad is correct
You can use the simpler method:
func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) { }
Inside that, set:
var userLocation : CLLocation = locations[0] as! CLLocation
Now create a CLLocationCoordinate2D by using userLocation.latitude and .longitude
create an annotation with MKPointAnnotation(), and set annotation.coordinate to be the CLLocationCoordinate2D above.
set the .title if required
map.addAnnotation(annotation)
This will in effect add a pin every time the user's location is updated - so you might want to think about removing the previous pins. Also, it's not very effective if y you're getting updates every half second or so, so consider simply setting the mapView to show the region every time it is updated, rather than a pin.
Hope this helps!

Core Location in Swift

This is sample code. NSLocationAlwaysUsageDescription key in Info.plist installed. All work fine - program receives the coordinates.... displays them.....
And it continues to update them permanently! In the case of the iOS simulator - it is not critical, but in the case of a real application it is very quickly drain the battery. How to make an application launched Core Location, received location, and got shut down Core Location?
import UIKit
import CoreLocation
class ViewController: UIViewController, CLLocationManagerDelegate {
#IBOutlet var locLabel: UILabel
#IBOutlet var latLabel: UILabel
#IBOutlet var lonLabel: UILabel
var locationManager = CLLocationManager()
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
#IBAction func locButton(sender: AnyObject) {
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.requestAlwaysAuthorization()
locationManager.startUpdatingLocation()
}
func locationManager(manager:CLLocationManager, didUpdateLocations locations:AnyObject[]) {
println("locations = \(locationManager)")
var latValue = locationManager.location.coordinate.latitude
var lonValue = locationManager.location.coordinate.longitude
latLabel.text = String(latValue)
lonLabel.text = String(lonValue)
locLabel.text = "success"
}
}
If you are not much concern about high level of accuracy then you should consider startMonitoringSignificantLocationChanges instead of startUpdatingLocation. It will really make a big difference in battery draining.
This method is more appropriate for the majority of applications that just need an initial user location fix and need updates only when the user moves a significant distance. This interface delivers new events only when it detects changes to the device’s associated cell towers, resulting in less frequent updates and significantly lower power usage.
For more detail you can take a look over CLLocationManager Guide