5

我的代码工作正常,完全符合我在模拟器中的要求。但是当我将它放在真实设备上并转到locationManager(..., didEnterRegion region: CLRegion)不会被调用的位置时。我不认为这是代码中的错误,因为当我在模拟器中进入和离开位置时,didExitRegion都会didEnterRegion被调用,但不是在真实设备上。我正在使用Swift 2.0 Xcode 7 beta 5中的 iPhone 5S 进行测试,在大面积开放区域具有良好的小区覆盖率。我也尝试过不同的半径。我的简化代码如下:

import UIKit
import CoreLocation
import MapKit

class GeoLapViewController: UIViewController, CLLocationManagerDelegate, MKMapViewDelegate{

    var manager: CLLocationManager?
    var location: CLLocationCoordinate2D!

    @IBOutlet weak var mapView: MKMapView!

    override func viewDidLoad() {
        super.viewDidLoad()

        manager = CLLocationManager()
        manager?.delegate = self;
        manager?.desiredAccuracy = kCLLocationAccuracyBest

        self.mapView.delegate = self
        mapView.mapType = MKMapType.Satellite

        manager?.startUpdatingLocation()
        let currRegion = CLCircularRegion(center: CLLocationCoordinate2D(latitude: location.latitude, longitude: location.longitude), radius: 20, identifier: "Laplocation")
        manager?.startMonitoringForRegion(currRegion)

        let newAnotation = MKPointAnnotation()
        newAnotation.coordinate = location
        newAnotation.title = "Start/Finish location"
        mapView.addAnnotation(newAnotation)

        let circle = MKCircle(centerCoordinate: location, radius: 150 as CLLocationDistance)
        self.mapView.addOverlay(circle)

        print(location.latitude)
        print(location.longitude)
        print(location)
    }

    func mapView(mapView: MKMapView!, rendererForOverlay overlay: MKOverlay!) -> MKOverlayRenderer! {
        if overlay is MKCircle {
            let circle = MKCircleRenderer(overlay: overlay)
            circle.strokeColor = UIColor.blueColor()
            circle.fillColor = UIColor(red: 0, green: 0, blue: 255, alpha: 0.4)
            circle.lineWidth = 1
            return circle
        } else {
            return nil
        }
    }

    func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        manager.stopUpdatingLocation()
        let location = locations[0]
        let geoCoder = CLGeocoder()
        geoCoder.reverseGeocodeLocation(location, completionHandler: { (data, error) -> Void in
            self.mapView.centerCoordinate = location.coordinate
            let reg = MKCoordinateRegionMakeWithDistance(location.coordinate, 100, 100)
            self.mapView.setRegion(reg, animated: true)
            self.mapView.showsUserLocation = true
        })
    }

    func locationManager(manager: CLLocationManager, didEnterRegion region: CLRegion) {
        print("Entering region")
    }

    func locationManager(manager: CLLocationManager, didExitRegion region: CLRegion) {
        print("Exit region")
    }

    func locationManager(manager: CLLocationManager, didFailWithError error: NSError) {
        print("\(error)")
    }
}

还有其他人遇到这样的麻烦或 Xcode 7 中的语法发生了变化吗?

4

0 回答 0