我已经在我的 appDelegate 中实现了 CLLocation,以允许在我的应用程序中使用单个实例。我正在启动和停止位置更新并将位置存储在数组中,但是我正在尝试监听 View Controller 中的位置更改,以便我可以在 map 上绘制路线,但不确定如何执行此操作。
我的代码如下。
viewcontroller.swift
if let loc = appDelegate.location {
var currentLat = loc.latitude
var currentLng = loc.longitude
appDelegate.locationManager.startUpdatingLocation()
appDelegate.trackLocation = true
// SETUP OUR MAP
mapView.delegate = self
mapView.mapType = MKMapType.Standard
mapView.showsUserLocation = true
let spanX = 0.01
let spanY = 0.01
var newRegion = MKCoordinateRegion(center: loc, span: MKCoordinateSpanMake(spanX, spanY))
mapView.setRegion(newRegion, animated: false)
if(!markStartLocation)
{
locationStart.coordinate = CLLocationCoordinate2DMake(loc.latitude, loc.longitude)
mapView.addAnnotation(locationStart)
markStartLocation = true
}
}
appDelegate.swift
func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {
var locationArray = locations as NSArray
var locationObj = locationArray.lastObject as! CLLocation
if (foundLocation == false) {
self.locationManager.stopUpdatingLocation()
foundLocation = true
}
self.location = locationObj.coordinate
if(self.trackLocation)
{
var lat = Double(locationObj.coordinate.latitude)
var long = Double(locationObj.coordinate.longitude)
var coordinatesToAppend = CLLocationCoordinate2D(latitude: lat, longitude: long)
trackedCoordinates.append(coordinatesToAppend)
}
}
最佳答案
已解决。我添加了一个观察者来监听位置变化,然后发布了一条通知。
关于swift - 从另一个 View 监听用户位置变化 - Swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32318588/