2016-10-06 8 views
1

ниже некоторых из моего кода, где у меня есть от 4 до 5 адресов, и я хочу контролировать его с помощью геообстановки. Когда он начинает отслеживать регионы, я могу видеть на моей консоли имя правильных мест.Swift 3 Проблемы с Geofencing

Проблема заключается в том, чтобы моделировать, чтобы добраться до одного места, оно дает мне предупреждение о неправильном расположении, оно всегда дает мне ресторан 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" 
} 
] 

Thanks Родриго

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

в метрах. 0,5 м слишком мал. Радиус мне больше понравится 100 – Paulw11

ответ

0

я узнал.

Проблема была на этой линии

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)