आर्काइव पॉलीलाइन वस्तु और कोर डाटा को बचाने:
let context = self.fetchedResultsController.managedObjectContext
let entity = self.fetchedResultsController.fetchRequest.entity!
let newManagedObject = NSEntityDescription.insertNewObjectForEntityForName(entity.name!, inManagedObjectContext: context)
let polylineObj = polyline() // For test purpose.
let polylineData = polylineToArchive(polylineObj)
newManagedObject.setValue(polylineData, forKey: "polyline")
context.save()
NSManagedObject से संग्रह से निकालें पॉलीलाइन:
let object = self.fetchedResultsController.objectAtIndexPath(indexPath) as! NSManagedObject
let data = object.valueForKey("polyline") as! NSData
let polyline = polylineUnarchive(data)
log(polyline!)
एमकेपोलीलाइन अरिवर और अनारक फ़ंक्शन। साथ ही कुछ सहायक कार्यों।
func polylineUnarchive(polylineArchive: NSData) -> MKPolyline? {
guard let data = NSKeyedUnarchiver.unarchiveObjectWithData(polylineArchive),
let polyline = data as? [Dictionary<String, AnyObject>] else {
return nil
}
var locations: [CLLocation] = []
for item in polyline {
if let latitude = item["latitude"]?.doubleValue,
let longitude = item["longitude"]?.doubleValue {
let location = CLLocation(latitude: latitude, longitude: longitude)
locations.append(location)
}
}
var coordinates = locations.map({(location: CLLocation) -> CLLocationCoordinate2D in return location.coordinate})
let result = MKPolyline(coordinates: &coordinates, count: locations.count)
return result
}
func polylineToArchive(polyline: MKPolyline) -> NSData {
let coordsPointer = UnsafeMutablePointer<CLLocationCoordinate2D>.alloc(polyline.pointCount)
polyline.getCoordinates(coordsPointer, range: NSMakeRange(0, polyline.pointCount))
var coords: [Dictionary<String, AnyObject>] = []
for i in 0..<polyline.pointCount {
let latitude = NSNumber(double: coordsPointer[i].latitude)
let longitude = NSNumber(double: coordsPointer[i].longitude)
let coord = ["latitude" : latitude, "longitude" : longitude]
coords.append(coord)
}
let polylineData = NSKeyedArchiver.archivedDataWithRootObject(coords)
return polylineData
}
func polyline() -> MKPolyline {
let locations = [CLLocation(latitude: 37.582691, longitude: 127.011186), CLLocation(latitude: 37.586112,longitude: 127.011047), CLLocation(latitude: 37.588212, longitude: 127.010438)]
var coordinates = locations.map({(location: CLLocation) -> CLLocationCoordinate2D in return location.coordinate})
let polyline = MKPolyline(coordinates: &coordinates, count: locations.count)
return polyline
}
func log(polyline: MKPolyline) {
let coordsPointer = UnsafeMutablePointer<CLLocationCoordinate2D>.alloc(polyline.pointCount)
polyline.getCoordinates(coordsPointer, range: NSMakeRange(0, polyline.pointCount))
var coords: [Dictionary<String, AnyObject>] = []
for i in 0..<polyline.pointCount {
let latitude = NSNumber(double: coordsPointer[i].latitude)
let longitude = NSNumber(double: coordsPointer[i].longitude)
let coord = ["latitude" : latitude, "longitude" : longitude]
coords.append(coord)
}
print(coords)
}
वाह। बहुत बहुत धन्यवाद। मुझे एएसएपी परीक्षण करने दें और फिर स्वीकार करें। वैसे, क्या आईओएस अपडेट के बीच दृष्टिकोण बेचा जाना चाहिए? – Greg
@ ग्रेग हां। यह कार्यक्षमता आईओएस अपडेट के बीच ठोस है। मेरा उदाहरण केवल अभिलेखागार और अनारक समन्वय करता है। यदि आप एमकेपोलीलाइन से अधिक सामान संग्रहित करना चाहते हैं तो अतिरिक्त कोड जोड़ने की आवश्यकता है। – Ramis
ठीक है धन्यवाद - मुझे लगता है कि यह सब मुझे चाहिए – Greg