2015-03-25 70 views
1

也許我找不到這個問題的答案,因爲它非常簡單,我應該能夠弄清楚,但我很難過。哦,如果我的某些術語關閉,我很抱歉 - 我還在學習。從核心數據創建一個CLLocation座標陣列

我正在使用Swift,並有一個從核心數據派生的數組。到現在爲止還挺好。這個數組中的兩個元素是存儲經度和緯度的Doubles/NSNumbers。我將使用這兩個元素來繪製地圖,但我無法弄清楚如何將這兩個元素放入他們自己的CLLocations數組中。

所以,我有我的所有數據的陣列中的核心數據:

var locationsList: [Locations] = [] 

var context = (UIApplication.sharedApplication().delegate as AppDelegate).managedObjectContext! 
    var request = NSFetchRequest(entityName: "Locations") 
    let pred = NSPredicate(format: "game = %d", passedGameNumber) 
    request.predicate = pred 
    request.sortDescriptors = [NSSortDescriptor(key:"time", ascending: false)] 
    self.locationsList = context.executeFetchRequest(requestMap, error: nil)! as [Locations] 

但它在覈心數據的一切(由遊戲過濾)保存數據:

class Locations: NSManagedObject { 

@NSManaged var game: NSNumber 
@NSManaged var time: NSDate 
@NSManaged var latitude: NSNumber 
@NSManaged var longitude: NSNumber 

} 

而且我只需要一個包含經度和緯度的數組,我需要將它轉換爲CLLocation放置在地圖上。我應該知道地圖的一部分 - 這只是我的腦袋撓了撓!謝謝。

+0

目前尚不清楚爲什麼你「需要轉換爲CLLocation把地圖上」。如果地圖是MKMapView,它需要實現MKAnnotation協議的對象。從技術上講,CLLocation不符合MKAnnotation,但碰巧有一個座標屬性,它對地圖視圖「起作用」。 – Anna 2015-03-25 11:19:53

+0

謝謝安娜。所以我不一定需要做任何數據轉換?我想這會讓事情變得更簡單,但我仍然不確定如何獲取Core Data中的座標並將它們放入MapView中。據我所知,我需要一組正確的座標信息,而我目前從核心數據中獲取的數據轉儲也包含遊戲和時間信息。正如我所說的,我確信這個問題有一個非常簡單的答案,但我無法將頭圍繞在它周圍。 – 2015-03-25 11:23:39

+0

而不是CLLocation,使用類似於內置的MKPointAnnotation正式實現MKAnnotation的類。更好的是(我認爲)只需讓你的Locations類自己實現MKAnnotation協議,這樣就不需要新的數組了(然後你可以給地圖視圖添加一個Locations對象)。聲明Locations實現MKAnnotation(以及NSManagedObject),然後爲'coordinate'實現一個getter方法。 – Anna 2015-03-25 11:27:29

回答

3

想通了。在循環遍歷Core Data數組之前,我還必須將我的緯度和經度轉換爲雙精度,然後再將它們作爲CLLocationCoordinate2D附加到我的新數組中。

var coordinates: [CLLocationCoordinate2D] = [] 

for index in 0..<self.locationsList.count{ 
     var lat = Double(self.locationsList[index].latitude) 
     var long = Double(self.locationsList[index].longitude) 
     var coordinatesToAppend = CLLocationCoordinate2D(latitude: lat, longitude: long) 
     coordinates.append(coordinatesToAppend) 
    } 
1

很好的解決亞當......我能夠做類似這樣的東西..

聲明這些全局變量

var longitudeCollection: [String] = [String]() 
var latitudeCollection: [String] = [String]() 

提出延期輕鬆訪問CLLoationDegrees類型作爲雙

extension String { 
var coordinateValue: CLLocationDegrees { 
    return (self as NSString).doubleValue 
    } 
} 

然後這就是你將如何追加座標

for var index = 0; index<=longitudeCollection.count-1; index++ { 
    var lat = latitudeCollection[index].coordinateValue 
    var long = longitudeCollection[index].coordinateValue 

    var coordinatesToAppend = CLLocationCoordinate2D(latitude: lat, longitude: long) 
    coordinates.append(coordinatesToAppend) 
} 
0

您不需要使用NSNumber來存儲經度和緯度點。您可以直接將CLLocation存儲爲核心數據。

爲每個CLLocation建立一個實體,無論使用哪個實體的位置點,它都會是一個太多的關係。讓我們把這種的LocationPoint:

class LocationPoint: NSManagedObject { 

@NSManaged var game: NSNumber 
@NSManaged var time: NSDate 
@NSManaged var location: AnyObject 

} 

你再設置位置屬性變形在Xcode的數據模型。而已!

在Objective-C,可實際上卻宣佈此的LocationPoint財產CLLocation沒有任何錯誤:

@property (nonatomic, strong) CLLocation *location