gpt4 book ai didi

ios - 核心位置 didEnterRegion 方法无法处理多个区域

转载 作者:行者123 更新时间:2023-11-30 12:31:02 24 4
gpt4 key购买 nike

我正在开发一个关于基于位置的提醒的项目。我使用了 CoreLocation 的 didEnterRegion 方法。因此,当我设置输入区域的位置时,我可以收到通知,但每当我想使用 didEnterRegion 方法设置另一个输入位置时,它只会看到第一个位置。无法为第二个位置调用该方法。你能帮我吗?

这是我的代码:

import UIKit
import CoreLocation
import MapKit


class ViewController: UIViewController,CLLocationManagerDelegate {

var locationManager = CLLocationManager()


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

let latitude1: CLLocationDegrees = 78.98
let longitude1: CLLocationDegrees = 90.09
let center1: CLLocationCoordinate2D = CLLocationCoordinate2DMake(latitude1, longitude1)
let radius1: CLLocationDistance = CLLocationDistance(100.0)
let identifier1: String = "Notre Dame"

let currRegion1 = CLCircularRegion(center: center1, radius: radius1, identifier: identifier1)

let latitude: CLLocationDegrees = 20.2020
let longitude: CLLocationDegrees = 2.2945
let center: CLLocationCoordinate2D = CLLocationCoordinate2DMake(latitude, longitude)
let radius: CLLocationDistance = CLLocationDistance(100.0)
let identifier: String = "Notre Dame"

let currRegion = CLCircularRegion(center: center, radius: radius, identifier: identifier)

locationManager.distanceFilter = 10
locationManager.desiredAccuracy = kCLLocationAccuracyBest
currRegion1.notifyOnEntry = true
currRegion.notifyOnEntry = true

locationManager.delegate=self
locationManager.requestAlwaysAuthorization()
locationManager.startMonitoring(for: currRegion1)
locationManager.startMonitoring(for: currRegion)
locationManager.startUpdatingLocation()
}

func locationManager(_ manager: CLLocationManager, didEnterRegion region: CLRegion) {
if region is CLCircularRegion {
handleEvent(forRegion: region)
}
}

func handleEvent(forRegion region: CLRegion!) {
print("Geofence triggered!")
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}

最佳答案

官方文档:

You must call this method once for each region you want to monitor. If an existing region with the same identifier is already being monitored by the app, the old region is replaced by the new one.

因此请使用不同的标识符:

//...
let identifier1: String = "Notre Dame1"
//...
let identifier: String = "Notre Dame2"
//...

关于ios - 核心位置 didEnterRegion 方法无法处理多个区域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43557346/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com