2016-06-11 5 views
0

CloudKitデータベース内のすべてのGeofencedリージョンのオーバーレイを表示する新しいマップビューをアプリケーションに追加しようとしました。複数のCloudKitレコードからMapKit Circleオーバーレイを作成

現時点では、次のコードでそれぞれの場所からピンを作成できます。

func fetchData() { 

    let predicate = NSPredicate(format: "TRUEPREDICATE", argumentArray: nil) 

    let query = CKQuery(recordType: "Collection", predicate: predicate) 

    let operation = CKQueryOperation(query: query) 
    operation.desiredKeys = ["Location"] 

    operation.recordFetchedBlock = { (record : CKRecord) in 

     self.collectionLocation = record.objectForKey("Location") as? CLLocation 

     print(self.collectionLocation?.coordinate.latitude) 

     self.buildBubbles() 

    } 

    publicDB!.addOperation(operation) 

    operation.queryCompletionBlock = {(cursor, error) in 

     dispatch_async(dispatch_get_main_queue()) { 

      if error == nil { 

      } else { 

       print("error description = \(error?.description)") 
      } 
     } 

    } 

} 

func buildBubbles() { 

    if CLLocationManager.isMonitoringAvailableForClass(CLCircularRegion.self) { 

     let intrepidLat: CLLocationDegrees = (self.collectionLocation?.coordinate.latitude)! 

     let intrepidLong: CLLocationDegrees = (self.collectionLocation?.coordinate.longitude)! 

     let title = "Item" 

     let coordinate = CLLocationCoordinate2DMake(intrepidLat, intrepidLong) 

     let regionRadius = 300.0 

     let region = CLCircularRegion(center: CLLocationCoordinate2D(latitude: coordinate.latitude, 
    longitude: coordinate.longitude), radius: regionRadius, identifier: title) 

     self.locationManager.startMonitoringForRegion(region) 

     let restaurantAnnotation = MKPointAnnotation() 

     restaurantAnnotation.coordinate = coordinate; 

     restaurantAnnotation.title = "\(title)" 

     self.mapView.addAnnotation(restaurantAnnotation) 

     // Overlay code goes here 

    } 

    else { 

     print("System can't track regions") 

    } 

} 

しかし、私はオーバーレイを追加するために行く:

let circle = MKCircle(centerCoordinate: coordinate, radius: regionRadius) 

self.mapView.addOverlay(circle) 

アプリがエラーで失敗します。

"This application is modifying the autolayout engine from a background thread, which can lead to engine corruption and weird crashes. This will cause an exception in a future release."

私の推測では、私は内側にあまりにも多くをやっているということですバックグラウンドスレッドですが、 "buildBubbles"関数をメインキューに移動するとサークルオーバーレイが追加されますが、マップの場所の1つだけが追加されます。

私は本当に助けていただきありがとうございました。

答えて

0

バブル関数へのインターフェイスは、1つの場所を保持するだけです。配列などのインターフェイスを変更してから、何が得られるかを確認してください。また、実際にどのように同期させるか心配する必要があります

0

FeldurがCloudKit Dataから配列を作成して作成し、MapKitの設定をバックグラウンドスレッドから移動したときに行ったことです。

func fetchBubble() { 

    let query = CKQuery(recordType: "Collection", predicate: NSPredicate(format: "TRUEPREDICATE", argumentArray: nil)) 

    publicDB!.performQuery(query, inZoneWithID: nil) { results, error in 

     if error == nil { 

      for collection in results! { 

       let collectionLocation = collection.valueForKey("Location") as? CLLocation 

       let collectionName = collection.valueForKey("Name") as! String 

       dispatch_async(dispatch_get_main_queue(), {() -> Void in 

        if CLLocationManager.isMonitoringAvailableForClass(CLCircularRegion.self) { 

         let intrepidLat: CLLocationDegrees = (collectionLocation?.coordinate.latitude)! 

         let intrepidLong: CLLocationDegrees = (collectionLocation?.coordinate.longitude)! 

         let title = collectionName 

         let coordinate = CLLocationCoordinate2DMake(intrepidLat, intrepidLong) 

         let regionRadius = 50.0 

         let region = CLCircularRegion(center: CLLocationCoordinate2D(latitude: coordinate.latitude, 
          longitude: coordinate.longitude), radius: regionRadius, identifier: title) 

         self.locationManager.startMonitoringForRegion(region) 

         let restaurantAnnotation = MKPointAnnotation() 

         self.mapView.addAnnotation(restaurantAnnotation) 

         restaurantAnnotation.coordinate = coordinate 

         let circle = MKCircle(centerCoordinate: coordinate, radius: regionRadius) 

         self.mapView.addOverlay(circle) 

         self.numberOfObjectsInMyArray() 

        } 

        else { 

         print("System can't track regions") 

        } 


       }) 

      } 

     } 

     else { 

      print(error) 

     } 

    } 

} 
関連する問題