CLCircularRegion didEnterRegion不起作用

时间:2015-06-30 06:02:47

标签: ios swift geofencing clcircularregion

我有一个GPX模拟到坐标的移动。我无法让CLCircularRegion无所事事。我试过把代码放到各处。这是我正在使用的:

var center: CLLocationCoordinate2D = CLLocationCoordinate2DMake(37.33171100, -122.03018400)
var radius: CLLocationDistance = CLLocationDistance(300)
var identifier: String = "Destination"
let region = CLCircularRegion(center: center, radius: radius, identifier: identifier)
manager!.startMonitoringForRegion(region)

我把它放在各处。我把它放进去了:

viewDidLoad 
viewWillAppear
viewDidAppear
mapViewDidFinishLoadingMap(mapView: MKMapView!
mapViewDidFinishRenderingMap(mapView: MKMapView!, fullyRendered: Bool)
locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!)

这是我的GPX文件:

<?xml version="1.0"?>
   <gpx version="1.1" creator="Xcode">
      <wpt lat="37.331711" lon="-122.030184">
               <name>Apple</name>
      </wpt>
      <wpt lat="37.422455" lon="-122.084306">
              <name>Google</name>
      </wpt>
</gpx>

Println适用于didStartMonitoringRegion,它会将区域信息打印到日志中,但不会触发。似乎什么都没有用,它让我疯狂。这是我的完整计划。

import UIKit
import CoreLocation
import MapKit

class ViewController: UIViewController, CLLocationManagerDelegate, MKMapViewDelegate {

var mapView: MKMapView?
var manager: CLLocationManager?

override func viewDidLoad() {
    super.viewDidLoad()
    mapView = MKMapView(frame: CGRectMake(0.0, 0.0, view.frame.size.width, view.frame.size.height))
    self.mapView!.delegate = self
    mapView!.showsUserLocation = true
    view.addSubview(mapView!)   
   }

func locationManager(manager: CLLocationManager!, didEnterRegion region: CLRegion!) {
    println("Entered")
   }

func locationManager(manager: CLLocationManager!, didStartMonitoringForRegion region: CLRegion!) {
    println("Monitoring")
    println(region)
}

func mapViewDidFinishLoadingMap(mapView: MKMapView!) {
    manager = CLLocationManager()
    manager!.delegate = self
    manager!.desiredAccuracy = kCLLocationAccuracyBest
    manager!.startUpdatingLocation()

    var center: CLLocationCoordinate2D = CLLocationCoordinate2DMake(37.33171100, -122.03018400)
    var radius: CLLocationDistance = CLLocationDistance(300)
    var identifier: String = "Destination"
    let region = CLCircularRegion(center: center, radius: radius, identifier: identifier)
    manager!.startMonitoringForRegion(region)
   }
}

1 个答案:

答案 0 :(得分:2)

你必须开玩笑吧。这是因为我在if ($(window).height() > 800){ $('html, body').css('transform', 'scale(2)'); }

中使用了NSLocationWhenInUseUsageDescription而不是NSLocationAlwaysUsageDescription