How to get location from GPS when offline in Swift? - ios

I want to make app can get location (latitude & longitude) with no internet, and someone tell me use CCLocationManager, here.
And I testing with my code:
var locationManager:CLLocationManager!
#IBOutlet weak var locationTextField: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
locationManager = CLLocationManager()
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.requestAlwaysAuthorization()
locationManager.startUpdatingLocation()
let authstate = CLLocationManager.authorizationStatus()
if(authstate == CLAuthorizationStatus.NotDetermined){
print("Not Authorised")
locationManager.requestWhenInUseAuthorization()
}
print("first")
locationTextField.text = "11"
// 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("success \(locations.count)")
let location:CLLocation = locations[locations.count - 1] as CLLocation
print("aa:\(location.coordinate.latitude) \(location.coordinate.longitude)aa")
locationTextField.text = location.description
}
func locationManager(manager: CLLocationManager, didFailWithError error: NSError) {
print("Couldn't get your location with \(error.description)")
}
But it just work with internet and when I turn off wifi, my app log: Couldn't get your location with Error Domain=kCLErrorDomain Code=0 "The operation couldn’t be completed. (kCLErrorDomain error 0.)"
Maybe I'm wrong somewhere, any helps, thanks.

Related

get user location and updates

I am trying to get user location in my iPhone app. It has specified in info.plist needed permission (use location even if app is not offline). But still I do not see that my delegate is triggered at least once meaning I do not get even current location. Here is the code:
import UIKit
import CoreLocation
class ViewController: UIViewController, CLLocationManagerDelegate {
var locationManager = CLLocationManager();
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
locationManager.delegate = self
if (CLLocationManager.authorizationStatus() == CLAuthorizationStatus.notDetermined) {
self.locationManager.requestAlwaysAuthorization();
}
locationManager.distanceFilter = kCLDistanceFilterNone
locationManager.desiredAccuracy = kCLLocationAccuracyBest
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 long = userLocation.coordinate.longitude;
let lat = userLocation.coordinate.latitude;
print(lat)
}
func locationManager(manager: CLLocationManager!, didFailWithError error: NSError!)
{
locationManager.stopUpdatingLocation()
if ((error) != nil)
{
print(error)
}
}
Could somebody point me out possible problems?
Try to remove locationManager.stopUpdatingLocation() in locationManager(manager: CLLocationManager!, didFailWithError error: NSError!). It should work.

CLLocationManager delegate methods are not getting called(google maps is integrated)

The delegate Methods of CLLocationManager
didChangeAuthorizationStatus
and
didUpdateToLocation
are not getting called.
Location Always Usage Description key is already added in info.plist and I am getting notification also when i launch app for the first time.
I am able to see the google map, but i am not able to see current location, When i change location, its not getting updated. Basicaaly delegate methods are not getting called.
//code
import UIKit
import GoogleMaps
class ViewController: UIViewController,GMSMapViewDelegate {
#IBOutlet weak var mapViewTest: GMSMapView!
let locationManager = CLLocationManager()
var currentLocation :CLLocation = CLLocation(latitude: 0.0, longitude: 0.0)
var currentLatitude : Double = 0.0
var currentLongitude : Double = 0.0
override func viewDidLoad()
{
super.viewDidLoad()``
locationManager.delegate = self
if (CLLocationManager.locationServicesEnabled())
{
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.allowsBackgroundLocationUpdates = true
locationManager.requestAlwaysAuthorization()
locationManager.startUpdatingLocation()
}
// Do any additional setup after loading the view, typically from a nib.
}
}
extension ViewController : CLLocationManagerDelegate
{
func locationManager(manager: CLLocationManager, didChangeAuthorizationStatus status: CLAuthorizationStatus)
{
if status == .authorizedAlways
{
if(CLLocationManager .locationServicesEnabled())
{
locationManager.startUpdatingLocation()
mapViewTest.isMyLocationEnabled = true
mapViewTest.settings.myLocationButton = true
}
}
}
func locationManager(manager: CLLocationManager, didUpdateToLocation newLocation: CLLocation, fromLocation oldLocation: CLLocation)
{
mapViewTest.camera = GMSCameraPosition(target: (newLocation.coordinate), zoom: 15, bearing: 0, viewingAngle: 0)
currentLocation = newLocation
currentLatitude = newLocation.coordinate.latitude
currentLongitude = newLocation.coordinate.longitude
}
func locationManager(manager: CLLocationManager, didFailWithError error: NSError)
{
print("Errors: " + error.localizedDescription)
}
}
From your code you are working with Swift 3, and in Swift 3 CLLocationManagerDelegate method's signature is changed like this.
func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
}
//func locationManager(_ manager: CLLocationManager, didUpdateTo newLocation: CLLocation,
from oldLocation: CLLocation) is deprecated with below one
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
}
func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
}
Check Apple Documentation on CLLocationManagerDelegate for more details.
After checking your code i found some changes you needs to do as follow,
note : I have only added the code having issue here that of location manager
import UIKit
import CoreLocation
class ViewController: UIViewController {
let locationManager = CLLocationManager()
var currentLocation :CLLocation = CLLocation(latitude: 0.0, longitude: 0.0)
var currentLatitude : Double = 0.0
var currentLongitude : Double = 0.0
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
locationManager.delegate = self
if (CLLocationManager.locationServicesEnabled())
{
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.allowsBackgroundLocationUpdates = true
locationManager.requestAlwaysAuthorization()
locationManager.startUpdatingLocation()
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
extension ViewController : CLLocationManagerDelegate {
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
}
func locationManager(_ manager: CLLocationManager, didFinishDeferredUpdatesWithError error: Error?) {
print("Errors: " + (error?.localizedDescription)!)
}
}
Also add below lines in .plist file if not added,
<key>NSLocationWhenInUseUsageDescription</key>
<string>$(PRODUCT_NAME) location use</string>
<key>NSLocationAlwaysUsageDescription</key>
<string>$(PRODUCT_NAME) always uses location </string>
Add these two properties in your info.plist
NSLocationWhenInUseUsageDescription
NSLocationAlwaysUsageDescription
you need to put the mapview delegate to self.
try this:
override func viewDidLoad() {
super.viewDidLoad()
// User Location Settings
locationManager.delegate = self
locationManager.requestWhenInUseAuthorization()
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.startUpdatingLocation()
// Google Maps Delegate
mapView.delegate = self
}
// View will appear
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
// Google maps settings
mapView.isMyLocationEnabled = true
mapView.settings.myLocationButton = true
// Get location if autorized
if (CLLocationManager.authorizationStatus() == CLAuthorizationStatus.authorizedWhenInUse) {
let (latitude, longitude) = self.getLocation()
mapView.camera = GMSCameraPosition.camera(
withLatitude: latitude,
longitude: longitude,
zoom: 14)
}
}
//Get the user location
func getLocation() -> (latitude: CLLocationDegrees, longitude: CLLocationDegrees) {
let latitude = (locationManager.location?.coordinate.latitude)!
let longitude = (locationManager.location?.coordinate.longitude)!
return (latitude, longitude)
}

locationManager:didUpdateLocations never called

I have this code in my ViewController:
#IBAction func testButton(sender: UIButton) {
let locationManager = CLLocationManager()
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
print("\(CLLocationManager.locationServicesEnabled())")
locationManager.requestWhenInUseAuthorization()
locationManager.startUpdatingLocation()
print("Started updating location")
}
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let userLocation: CLLocation = locations[0]
let long = userLocation.coordinate.longitude
let lat = userLocation.coordinate.latitude
print("\(long), \(lat)")
}
func locationManager(manager: CLLocationManager, didFailWithError error: NSError) {
print("Location update failed: \(error)")
}
NSLocationWhenInUseUsageDescription in my plist. It should just work according to all information I could find, but it just outputs
true
Started updating location
Is edit 2 of this question true? If so, what would be the best way to move this out of the viewController?
Before startUpdatingLocation(), have to ask for permission using requestWhenInUseAuthorization() first.
So your codes in ViewController will be similar like the following or you can download a sample project here.
class ViewController: UIViewController, CLLocationManagerDelegate {
let locationManager: CLLocationManager = CLLocationManager()
override func viewDidLoad() {
super.viewDidLoad()
self.locationManager.delegate = self
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest
self.testButton()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func testButton() {
if CLLocationManager.authorizationStatus() != .AuthorizedWhenInUse {
self.locationManager.requestWhenInUseAuthorization()
return
}
self.locationManager.startUpdatingLocation()
}
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let userLocation: CLLocation = locations[0]
let long = userLocation.coordinate.longitude
let lat = userLocation.coordinate.latitude
print("\(long), \(lat)")
}
func locationManager(manager: CLLocationManager, didFailWithError error: NSError) {
print("Location update failed: \(error)")
}
func locationManager(manager: CLLocationManager, didChangeAuthorizationStatus status: CLAuthorizationStatus) {
if status == .AuthorizedWhenInUse {
self.testButton()
}
}
}

Error finding location when using CoreLocation

I'm trying to grab a user's location using XCode 7.1 & for iOS 9.1.
I don't see any errors in the code, but I keep getting this same error every time I try getting the location. Error finding location: The operation couldn’t be completed. (kCLErrorDomain error 0.)
Here's my code:
import UIKit
import AVFoundation
import CoreLocation
class secondview: UIViewController, CLLocationManagerDelegate {
var player = AVPlayer() // Audio Player
let locationManager = CLLocationManager() // Location Manager
override func viewDidLoad() {
super.viewDidLoad()
//PlayEntryAudio() // Start playing entry audio
locationManager.requestWhenInUseAuthorization()
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters
locationManager.requestLocation()
}
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
if let location = locations.first {
print("Current location: \(location)")
} else {
// ...
}
}
func locationManager(manager: CLLocationManager, didFailWithError error: NSError) {
print("Error finding location: \(error.localizedDescription)")
}
func PlayEntryAudio(){
print("Got here")
let url = NSURL(string: "https://9147abb4.ngrok.io/track.mp4")!
let playerItem = AVPlayerItem( URL:url)
player = AVPlayer(playerItem:playerItem)
player.play()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
Edit 01: Noticed that I wasn't getting an alert asking me to get location, so I tried adding locationManager.requestWhenInUseAuthorization() to no success.

Swift CoreLocation to get latitude and longitude

I am trying to use CoreLocation to get the latitude and longitude coordinates in iOS 8. After looking through a lot of posts, I am still unsure how to go about doing this. Also, I would appreciate a brief explanation of the locationManager() methods as I don't understand what role they play in obtaining the location. Do I ever need to call these methods? Because I never saw them called in any examples.
class ViewController: UIViewController, CLLocationManagerDelegate {
var locationMan = CLLocationManager()
var ourLocation: CLLocation!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.locationMan.delegate = self
self.locationMan.desiredAccuracy = kCLLocationAccuracyBest
self.locationMan.requestWhenInUseAuthorization()
self.locationMan.startUpdatingLocation()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func locationManager(manager: CLLocationManager!, didUpdateLocations locations : [AnyObject]!){
locationMan.stopUpdatingLocation()
ourLocation = locations[0] as! CLLocation
}
func locationManager(manager: CLLocationManager!, didFailWithError error: NSError!) {
println("Error: " + error.localizedDescription)
}
#IBAction func button(sender: AnyObject) {
var testClass:item = item()
After the next line, the app crashes and I get the error: fatal error: unexpectedly found nil while unwrapping an Optional value. I think this means that ourLocation never actually gets set. (Note that I am casting to float because I need to store the location as a float on my server)
testClass.lat = Float(ourLocation.coordinate.latitude)
println(testClass.lat)
testClass.long = Float(ourLocation.coordinate.longitude)
println(testClass.long)
testClass.name = "Nice"
testClass.description = "Wow this is pretty cool"
testClass.postItemToServer()
}
}
Note that I have updated my plist file appropriately! Location is turned on in the simulator.
Check if the values you are passing to variable are nil or not
if ourLocation.coordinate.latitude != nil
{
testClass.lat = Float(ourLocation.coordinate.latitude)
println(testClass.lat)
}
Also check for longitude.
import UIKit
import CoreLocation
class ViewController: UIViewController, CLLocationManagerDelegate{
var manager:CLLocationManager!
var location : CLLocation!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.manager = CLLocationManager()
self.manager.delegate = self;
self.manager.desiredAccuracy = kCLLocationAccuracyBest
self.manager.requestWhenInUseAuthorization()
self.manager.startUpdatingLocation()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func locationManager(manager: CLLocationManager!, didUpdateLocations locations : [AnyObject]!){
manager?.stopUpdatingLocation()
self.location = locations[0] as CLLocation
}
func locationManager(manager: CLLocationManager!, didFailWithError error: NSError!) {
println("Error: " + error.localizedDescription)
}
#IBAction func button(sender: AnyObject) {
//check if location is not nil
if self.location != nil {
//assign your loaction values here
let lat = Float(location.coordinate.latitude)
let long = Float(location.coordinate.longitude)
} else {
println("locationManager.location is nil")
}
}
}
The location property is an implicitly unwrapped CLLocation optional, and it is likely nil. You should never access members of implicitly unwrapped optional unless you know it is not nil.

Resources