当我使用连接到我的Mac的手机运行下面的代码时,我有一个GPX文件,在调试器上我选择“Locations”一切正常,我在GPX文件上的每个区域都被触发,通知出现没有问题。
问题是当我拿着手机开车而且我在GPX文件上传递了我所拥有的确切区域时,通知不会被触发,只是第一个。
关于方法
func locationManager(_ manager: CLLocationManager, didStartMonitoringFor region: CLRegion) {
print("Hi \(region.identifier)")
}
我可以看到所有地区都已启动进行监控
我已经被困在这几天了......你能不能请别人帮助我。
代码如下。
func GetAllILocations(){
if let url = URL(string: "http://www.goemobile.com/mobile/liquidnitro/getlocations.php"){
var request = URLRequest(url:url)
request.httpMethod = "POST";// Compose a query string
let postString = ""
request.httpBody = postString.data(using: String.Encoding.utf8)
let task = URLSession.shared.dataTask(with:request) { data, response, error in
if error != nil{
return
}
do {
if let convertedJson = try JSONSerialization.jsonObject(with: data!, options: []) as? [[String:Any]] {
for location in convertedJson {
if ((location["locationid"] as? Int)! > 0) {
let latitude = location["latitude"] as! Double
let longitude = location["longitude"] as! Double
let title = location["locationtitle"] as! String
let subtitle = location["locationsubtitle"] as? String
let annotation = MKPointAnnotation()
annotation.title = title
annotation.subtitle = subtitle
annotation.coordinate = CLLocationCoordinate2D(latitude: latitude, longitude: longitude)
self.mapView.addAnnotation(annotation)
let center = CLLocationCoordinate2DMake(latitude, longitude)
let region = CLCircularRegion.init(center: center, radius: 0.5, identifier: title)
region.notifyOnEntry = true;
region.notifyOnExit = false;
self.locationManger.startMonitoring(for: region)
}
}
}
}
catch let error as NSError {
print(error.localizedDescription)
}
}
task.resume()
}
}
func locationManager(_ manager: CLLocationManager, didStartMonitoringFor region: CLRegion) {
print("Hi \(region.identifier)")
}
func locationManager(_ manager: CLLocationManager, didEnterRegion region: CLRegion) {
lblRegion.text = region.identifier
if region is CLCircularRegion {
scheduleNotification(inSeconds: 0.5, storeName:region.identifier, completion: {success in
if !success{
let alert = UIAlertController(title: "Error Alert", message: region.identifier, preferredStyle: .alert)
let action = UIAlertAction(title: "OK", style: .cancel, handler: nil)
alert.addAction(action)
self.present(alert, animated: true, completion: nil)
}
})
}
}