当GPS进入一个确定的区域时,我的所有地理围栏都会触发,起初我认为这是因为半径,但是即使在减半之后我也遇到了同样的问题.
import UIKit
import CoreLocation
class itemDesc {
var title: String
var coordinate: CLLocationCoordinate2D
var regionRadius: CLLocationDistance
var location: String
var type: String
init(title: String, coordinate: CLLocationCoordinate2D, regionRadius: CLLocationDistance, location: String, type: String) {
self.title = title
self.coordinate = coordinate
self.regionRadius = regionRadius
self.location = location
self.type = type
}
}
class ViewController: UIViewController, CLLocationManagerDelegate {
let locationManager = CLLocationManager()
override func viewDidLoad() {
super.viewDidLoad()
locationManager.delegate = self
locationManager.requestAlwaysAuthorization()
locationManager.startUpdatingLocation()
locationManager.desiredAccuracy = kCLLocationAccuracyBest
setupData()
}
func locationManager(manager: CLLocationManager, didChangeAuthorizationStatus status: …Run Code Online (Sandbox Code Playgroud) 我一直在iOS上开发区域监控大约2个月.最近我们发现了一个小故障,同时在一定半径范围内(约4.7KM或4700米)触发了所有区域.设备的当前位置甚至不靠近任何区域.我不确定是什么原因引发了这一事件.我搜索了StackOverFlow,Apple Developer论坛等,我没有发现任何与我面临的类似问题.
在我正在开发的应用程序中,我们正在监控城市内的8个区域(吉隆坡).有一次,我的同事发现他的手机同时触发了4个地区通知.下面是显示其位置,所有受监控区域,触发4区域通知的潜在半径的地图.

触发通知的屏幕截图:

这是我的位置管理器代码: -
CLLocationManager *locationManager = [LocationTracker sharedLocationManager];
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation;
locationManager.distanceFilter = kCLDistanceFilterNone;
Run Code Online (Sandbox Code Playgroud)
这是我的didEnterRegion代码: -
-(void)locationManager:(CLLocationManager *)manager
didEnterRegion:(CLRegion *)region{
NSString* message = [NSString stringWithFormat:@"Message"];
UIApplicationState state = [[UIApplication sharedApplication] applicationState];
if (state == UIApplicationStateBackground || state == UIApplicationStateInactive)
{
UILocalNotification *notification = [[UILocalNotification alloc] init];
notification.fireDate = [NSDate date];
NSTimeZone* timezone = [NSTimeZone defaultTimeZone];
notification.timeZone = timezone;
notification.alertBody = message;
notification.alertAction = @"Show";
notification.soundName = UILocalNotificationDefaultSoundName; …Run Code Online (Sandbox Code Playgroud) 我在启动区域监视时正在构建与区域监视有关的功能,我在请求状态,如下代码所示。在某些设备上,我一直都在获取区域状态“未知”。如果我打开或关闭Wifi或将充电器插入其中。它开始正常工作。如何使它在蜂窝网络上更可靠?请注意,在进行任何区域监视或状态请求调用之前,我已获得用户的所有位置权限。
private func initiateLocationManager() {
locationManager = CLLocationManager()
locationManager.delegate = self
locationManager.distanceFilter = kCLLocationAccuracyBest
locationManager.requestAlwaysAuthorization()
}
func startMonitoring(alarm: StationAlarm) {
if LocationManager.sharedInstance.isRegionMonitoringAvailable() {
let coordinate = CLLocationCoordinate2D(latitude: stationLatitude, longitude: stationLongitude)
// 1
let region = CLCircularRegion(center: coordinate, radius: CLLocationDistance(radius * 1000), identifier: alarm.alarmId)
// 2
region.notifyOnEntry = true
region.notifyOnExit = false
// 4
locationManager.startMonitoring(for: region)
Utility.delay(0.1) { [weak self] in
self?.locationManager.requestState(for: region)
}
}
}
func locationManager(_: CLLocationManager, didDetermineState state: CLRegionState, for region: CLRegion) {
Log.event("Region State is \(state.rawValue)")
}
Run Code Online (Sandbox Code Playgroud)