我正在使用prepareForSegue
方法将位置信息传递给另一个带有MapView
的UIViewController
,并将其称为receivedLocation
。地图以传递的位置为中心显示。如果用户在先前未发送位置信息的情况下单击按钮以使其转到地图页面,会出现什么问题?
如何测试receivedLocation
是否包含数据,以便在其为空时将地图设置为中心点的其他位置?
创建一个布尔变量,最初将其设置为false,但在传递位置并测试该变量后将其设置为true,这样是否可以达到预期效果?
我发现CLLocationCoordinateIsValid
可用,但其参数是2D坐标,在测试receivedLocation
是否存在时会遇到同样的问题。
我刚接触这个领域,并尝试寻找答案,但没有找到合适的答案。谢谢!
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)
}
目前这对我不起作用。CLLocationCoordinate2DIsValid总是返回true,并围绕一个岛屿居中,以及在那个奇怪的地方添加注释。
receivedLocation
是什么?请展示它的声明。 - matt