从另一个视图中侦听用户位置更改 - Swift

时间:2015-08-31 19:30:24

标签: swift cllocationmanager

我已经在appDelegate中实现了CLLocation,以允许整个应用程序中的单个实例。我正在启动并停止位置更新并将位置存储在一个数组中但是我正在尝试在视图控制器中监听位置更改,以便我可以在地图上绘制路线,但不知道如何执行此操作。

我的代码如下。

viewcontroller.swift

var a = new Person();
a.setName('John');

var b = new Person();
b.getName(); // the result will be John

appDelegate.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
   }
}

1 个答案:

答案 0 :(得分:1)

解决。我添加了一个观察员来监听位置更改然后发布了通知。