菜鸟教程小白 发表于 2022-12-12 16:16:00

iOS 区域监控不起作用


                                            <p><p>我已经做了几个月的 iOS 开发,最近我正在开发一个公交车应用程序。
我目前正在模仿公交车的运动并在公交车站上设置多个注释。出于测试目的,我只设置了一个公交车站,并试图监控公交车何时进入该区域并退出。</p>

<p>奇怪的是,我的 <code>didStartMonitoringForRegion</code> 方法被完美调用,但 <code>didEnterRegion</code> 和 <code>didExitRegion</code> 方法都没有被调用。每次我运行程序时,公共(public)汽车都会在没有提示我的情况下通过车站。</p>

<p>有人可以向我解释为什么会发生这种情况以及如何解决它吗?</p>

<pre><code>let locationManager = CLLocationManager()
var allBusAnnotations = ()
var summitEastBusStations = ()
var busStopNames = [&#34;Dix Stadium&#34;, &#34;Risman Plaza&#34;, &#34;Terrace Drive&#34;, &#34;Terrace Drive 2&#34;,&#34;C-Midway&#34;,&#34;Theatre Dr.&#34;,&#34;East Main Street&#34;,&#34;South Lincoln&#34;]
var radius = 500 as CLLocationDistance

// 0.02 is the best zoom in factor
var mapZoomInFactor : Double = 0.02

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    self.getBusStop()
    self.locationManager.delegate = self
    // gets the exact location of the user
    self.locationManager.desiredAccuracy = kCLLocationAccuracyBest
    // gets the user&#39;s location only when the app is in use and not background
    self.locationManager.requestWhenInUseAuthorization()
    self.locationManager.startUpdatingLocation()

    self.mapView.showsUserLocation = true
    self.setBusStopAnnotations(summitEastBusStations)
    // self.mapView.mapType = MKMapType.Satellite
}
    func locationManager(manager: CLLocationManager!, didUpdateLocations locations: !) {

    // sends the latitude and longitude to the Apple Servers then returns the address
    CLGeocoder().reverseGeocodeLocation(manager.location, completionHandler: { (placeMarks: !, error: NSError!) -&gt; Void in

      if error != nil
      {
            println(&#34;Reverse Geocode Failed: &#34; + error.localizedDescription)
            return
      }

      if placeMarks.count &gt; 0
      {
            // gets the most updated location
            let pm = placeMarks.last as! CLPlacemark

            let centre = CLLocationCoordinate2D(latitude: manager.location.coordinate.latitude, longitude: manager.location.coordinate.longitude)

            // draws a circle in which the map will zoom to
            let region = MKCoordinateRegion(center: centre, span: MKCoordinateSpan(latitudeDelta: self.mapZoomInFactor, longitudeDelta: self.mapZoomInFactor))

            self.mapView.setRegion(region, animated: true)

            self.displayLocationInfo(pm)

            // self.distanceToClosestAnnotation(pm)

            self.geoFencing()

            // YOU CAN IGNORE THIS WHOLE PART. IT&#39;S IRRELEVANT FOR THIS QUESTION
            var repeatTimes = 0
            var count = 0
            while(count &lt;= 7)
            {
                if count == (self.summitEastBusStations.count - 1)
                {
                  count = 1
                  ++repeatTimes
                }
                else if repeatTimes == 1
                {
                  count = 0
                  ++repeatTimes
                }
                else if repeatTimes == 2
                {
                  break
                }

                self.distanceToBusStop(pm, count: count)
                ++count
            }
      }
    })
}
    func locationManager(manager: CLLocationManager!, didFailWithError error: NSError!) {
    println(&#34;Location Manager Failed: &#34; + error.localizedDescription)
}

func locationManager(manager: CLLocationManager!, didStartMonitoringForRegion region: CLRegion!) {
    println(&#34;The region is monitored&#34;)
    println(&#34;The monitored region is \(region.description)&#34;)
}

func locationManager(manager: CLLocationManager!, monitoringDidFailForRegion region: CLRegion!, withError error: NSError!) {
    println(&#34;Failed to monitor the stated region&#34;)
}

func locationManager(manager: CLLocationManager!, didEnterRegion region: CLRegion!) {
    println(&#34;The bus has entered the region&#34;)
}

func locationManager(manager: CLLocationManager!, didExitRegion region: CLRegion!) {
    println(&#34;The bus has left the region&#34;)
}
func geoFencing()
{

    let rismanPlaza = CLLocationCoordinate2D(latitude: 41.1469492, longitude: -81.344068)

    var currentBusStop = CLLocation(latitude: rismanPlaza.latitude, longitude: rismanPlaza.longitude)
    addRadiusCircle(currentBusStop)

    let busStopRegion = CLCircularRegion(center: CLLocationCoordinate2D(latitude: rismanPlaza.latitude, longitude: rismanPlaza.longitude), radius: radius, identifier: busStopNames)

    if radius &gt; self.locationManager.maximumRegionMonitoringDistance
    {
      radius = self.locationManager.maximumRegionMonitoringDistance
    }

    locationManager.startMonitoringForRegion(busStopRegion)

}

// creates the radius around the specified location
func addRadiusCircle(location: CLLocation)
{
    self.mapView.delegate = self
    var circle = MKCircle(centerCoordinate: location.coordinate, radius: radius)
    self.mapView.addOverlay(circle)
}

// performs the actual circle colouring
func mapView(mapView: MKMapView!, rendererForOverlay overlay: MKOverlay!) -&gt; MKOverlayRenderer!
{
    if overlay is MKCircle
    {
      var circle = MKCircleRenderer(overlay: overlay)
      circle.strokeColor = UIColor.redColor()
      circle.fillColor = UIColor(red: 255, green: 0, blue: 0, alpha: 0.1)
      circle.lineWidth = 1
      return circle
    }
    else
    {
      return nil
    }
}
</code></pre></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>我最终改用了 <code>CLRegion.containsCoordinate(location.coordinate)</code> 方法。它的工作方式几乎相同。</p>

<p>一旦对象进入我设置的区域,它就会返回 true,从这里我可以知道它何时进入和退出该区域。</p></p>
                                   
                                                <p style="font-size: 20px;">关于iOS 区域监控不起作用,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/32548000/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/32548000/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: iOS 区域监控不起作用