【问题标题】:Swift 3 Geofencing issuesSwift 3 地理围栏问题
【发布时间】:2016-10-06 23:13:06
【问题描述】:

在我的一些代码下方,我有 4 到 5 个地址,我想使用地理围栏对其进行监控。当它开始监控区域时,我可以在控制台上看到正确的位置名称。

问题是当模拟到达一个位置时,它会提醒我到错误的位置,它总是给我 Chaus 餐厅,它应该是 Schreiner Home 之一。

我将断点设置在是否输入了区域并且所有区域的代码都停在那里...为什么它只是在我输入正确的位置时触发?现在我要进入所有地区。

这个json是我得到的

[
{
locationid: 1,
latitude: 37.8037522,
longitude: -121.9871216,
islocationactive: 1,
locationtitle: "Schreiner's Home",
locationsubtitle: "Danville"
},
{
locationid: 2,
latitude: 37.8191921,
longitude: -122.0071005,
islocationactive: 1,
locationtitle: "Montair",
locationsubtitle: "Elementary School"
},
{
locationid: 3,
latitude: 37.8186077,
longitude: -121.999046,
islocationactive: 1,
locationtitle: "Chaus Restaurant",
locationsubtitle: "Americas Eats"
},
{
locationid: 4,
latitude: 37.7789669,
longitude: -121.9829908,
islocationactive: 1,
locationtitle: "Valley",
locationsubtitle: "Cheer & Dance"
}
]

谢谢 罗德里戈

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 region = CLCircularRegion(center: (self.locationManger.location?.coordinate)!, radius: 0.5, identifier: title)
                                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) {
        if region is CLCircularRegion {
            let alert = UIAlertController(title: "Alert", message: region.identifier, preferredStyle: .alert)
            let action = UIAlertAction(title: "OK", style: .cancel, handler: nil)
            alert.addAction(action)
            present(alert, animated: true, completion: nil)
        }
    }

【问题讨论】:

  • 半径以米为单位。 0.5m太小了。半径应该更像 100

标签: ios swift3 geofencing


【解决方案1】:

我发现了。

问题出在这一行

let region = CLCircularRegion(center: (self.locationManger.location?.coordinate)!, radius: 0.5, identifier: title)

我一直使用用户位置信息,而不是来自 json 的位置信息。

我改变了这个

let center = CLLocationCoordinate2DMake(latitude, longitude)
let region = CLCircularRegion.init(center: center, radius: 0.025, identifier: title)

【讨论】:

    猜你喜欢
    • 2013-06-25
    • 1970-01-01
    • 2019-12-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-08
    相关资源
    最近更新 更多