In Swift, can I detect if a CLLocation is valid or not?

Cody Lucas

I am passing a location to another UIViewController which has a MapView using prepareForSegue. I call this receivedLocation. The map centers on the location that is passed. If the user clicks a button to take them to the map without first sending a location, won't this fail?

How can I test to see if receivedLocation actually contains data, so that I can set the map to center on something else if it's empty?

Would creating a Boolean variable, and initially setting it false but turning it to true when passing a location and testing that work as I desire?

I see something called CLLocationCoordinateIsValid is available, but its parameter is a 2DCoordinate, where I run into the same problem of testing whether or not receivedLocation.

I'm fairly new at this and tried looking for an answer already but couldn't find an appropriate one. Thank you!

import UIKit
import MapKit
import CoreLocation

class mapViewController: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate {

    @IBOutlet var map: MKMapView!
    @IBOutlet var notes: UITextView!

    var receivedLocation = CLLocation()
    var annotation = MKPointAnnotation()
    var currentManager:CLLocationManager!

    var latitute:CLLocationDegrees = CLLocationDegrees()
    var longitude:CLLocationDegrees = CLLocationDegrees()

    override func viewDidLoad() {
        super.viewDidLoad()

        map.layer.cornerRadius = 12.0
        notes.layer.cornerRadius = 12.0

        currentManager = CLLocationManager()
        currentManager.delegate = self
        currentManager.desiredAccuracy = kCLLocationAccuracyBest
        currentManager.startUpdatingLocation()

        if CLLocationCoordinate2DIsValid(receivedLocation.coordinate) {

            latitute = receivedLocation.coordinate.latitude
            longitude = receivedLocation.coordinate.longitude
            annotation.coordinate.latitude = receivedLocation.coordinate.latitude
            annotation.coordinate.longitude = receivedLocation.coordinate.longitude
            map.addAnnotation(annotation)

        } else {

            latitute = currentManager.location.coordinate.latitude
            longitude = currentManager.location.coordinate.longitude
        }

        var latDelta: CLLocationDegrees = 0.01
        var lonDelta: CLLocationDegrees = 0.01
        var span:MKCoordinateSpan = MKCoordinateSpanMake(latDelta, lonDelta)
        var location : CLLocationCoordinate2D = CLLocationCoordinate2DMake(latitute, longitude)
        var region: MKCoordinateRegion = MKCoordinateRegionMake(location, span)       
        map.setRegion(region, animated: false)

    }

So far this is not working for me. CLLocationCoordinate2DIsValid always returns true and centers around an island, as well as adds an annotation to that weird spot.

matt

First of all, do not needlessly put a useless CLLocation() into receivedLocation. Declare receivedLocation as an implicitly unwrapped Optional:

 var receivedLocation : CLLocation! = nil

That way, either someone has set it or they have not. If they have not, it is nil, and you can test that:

if receivedLocation != nil {
    // okay, it's a real location!
}

Second, your else is never going to work, because it takes time for the sensors to warm up and get a location - in fact, they may never get one. So I can pretty well guarantee that in the case where receivedLocation is nil, currentManager.location will not be any use either. (See my answer here for an explanation, and pointer to sample code, of how to use a location manager to get a single location value.)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How can I get the CLLocation of a photo selected from UIImagePickerController in Swift?

From Dev

How do I convert an NSNumber to a CLLocation in Swift

From Dev

Swift: How can I detect elements inside of a UIView?

From Dev

How can I detect the new view during touchedMoved in Swift?

From Dev

Saving Swift CLLocation in CoreData

From Dev

CLLocation distanceFromLocation (in Swift?)

From Dev

Covert CLLocation to PFGeoPoint with Swift

From Dev

Calculate travel time CLLocation Swift

From Dev

Calculate travel time CLLocation Swift

From Dev

How can I detect if I'm in a subshell?

From Dev

How can I verify in Postgresql that JSON is valid?

From Dev

How can i render valid html with JQuery?

From Dev

How can I prove a type is valid in Agda?

From Dev

How can I prove a type is valid in Agda?

From Dev

Can I subtract dates to see if token valid?

From Dev

In CUDA, I can't get valid value

From Dev

Calculating bearing between two CLLocation points in Swift

From Dev

put CLLocationCoordinate2D into CLLocation for Swift

From Dev

CLLocation Manager in Swift to get Location of User

From Dev

Initialize a CLLocation object in swift with latitude and longitude

From Dev

Encoding an array of CLLocation in Swift crashes the compiler?

From Dev

Displaying CLLocation in label Xcode 7 Swift 2

From Dev

Trying to update CLLocation if NSUserDefault Exists in Swift

From Java

How can I detect flipping in a time series?

From Dev

How can I detect a html class with javascript?

From Dev

How can I detect text selected in a div?

From Dev

Can I detect if iPhone has blur enabled?

From Dev

Marionette.js - can I detect onAppend?

From Dev

How can I detect a letter character in JavaScript?

Related Related

HotTag

Archive