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

I am passing the location to someone else UIViewController

who has MapView

using prepareForSegue

. I call it receivedLocation

. The map focuses on the location that is being transmitted. If the user clicks a button to move them to the map without first submitting the location, wouldn't that fail?

How can I check if it actually receivedLocation

contains data so that I can set the map to something else if it is empty?

Would create a boolean variable and initially set it to false, but return it true when passing the location and testing that works as I wish?

I see that something called is available CLLocationCoordinateIsValid

, but its parameter is 2DCoordinate, where I am facing the same problem of checking if there is receivedLocation

.

I'm new to this and tried to find the answer already but couldn't find a suitable one. Thank!

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 doesn't work for me. CLLocationCoordinate2DIsValid always returns true and centers around the island and also adds annotation to this strange place.

+3


source to share


1 answer


First of all, it's not useless to put useless CLLocation()

in receivedLocation

. Declare receivedLocation

as implicitly expanded Optional:

 var receivedLocation : CLLocation! = nil

      

So either someone installed it or not. If they haven't, it's null and you can check it out:



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

      

Second, yours else

will never work because it takes the sensors to warm up and get a seat - in fact, they can never get it. Therefore, I can quite guarantee that in the case where it receivedLocation

is zero, currentManager.location

it will also not be used. (See my answer here for an explanation and a code example pointer on how to use a location manager to get a single location value.)

+5


source







All Articles