the did Update To location is not being called - swift

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>

Related

Coordinates not printing when the simulator is run, but they are when I run the program on my iPhone

I have the following code
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate, CLLocationManagerDelegate {
var locationManager = CLLocationManager()
var coordinates: String?
override func viewDidLoad() {
super.viewDidLoad()
self.locationManager.requestAlwaysAuthorization()
if CLLocationManager.locationServicesEnabled() {
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.startUpdatingLocation()
}
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
//updates the user's location as the user moves
let location: CLLocation = locations[0] as CLLocation
coordinates = "\(location.coordinate.latitude),\(location.coordinate.longitude)"
print(coordinates!)
}
When I run this on the simulator, the program works fine but it doesn't print the coordinates. When I run it on my iPhone, it prints the coordinates. Is there something in the Xcode settings I need to change to fix this? Also, when I try to use the "coordinates" variable in another function, I get an error saying the compiler is finding nil even though I assigned it a value in the locationManager function, so I'm guessing the two problems are connected. Thanks in advance
You can simulate location on the simulator using "Simulate location" button when running your app:
You can select one of the existing locations or you can create a GPX file (for example not a static single point but a route that will simulate constant changing of location). I would recommend you using https://mapstogpx.com/ to create GPX files.

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.

Custom locations on Xcode 9+ only causes didUpdateLocations to only fire once

I have an app where I leverage CoreLocation. For some reason when I use Xcode 9 or even the 9.1 beta 2, setting custom location coordinates under Debug > Locations > Custom Location in the iPhone simulator doesn't work as expected.
When I apply the custom coordinates, didUpdateLocations is only called once, or three times, then the location services turn off, and the location arrow turns into an outline. I know most of you guys will say that I shouldn't be testing location services on the simulator, but I do not have a choice, as I do not have access to a device at the moment.
I have provided some simple code bellow for accessing the users location and update it continuously. If you could, please test this out on your iPhone simulator in Xcode 9+ (ios 11+) and set a custom latitude and longitude to Debug > Locations > Custom Location and see if you get the same issue.
import UIKit
import CoreLocation
class ViewController: UIViewController, CLLocationManagerDelegate {
let locationManager = CLLocationManager()
override func viewDidLoad() {
super.viewDidLoad()
locationManager.requestWhenInUseAuthorization()
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.startUpdatingLocation()
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
if let location = locations.first {
print(location.coordinate)
}
}
}

UILabel will not update to show fetched location coordinates

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!

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!