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.
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.
Comments