2014-12-23 33 views
5

是否可以將PFGeoPoint與Parse轉換爲CLLocation?如何將PFGeoPoint轉換爲CLLocation

原因是我想從PFGeoPoint獲得的地名,像這樣:

CLGeocoder *geocoder; 
    CLPlacemark *placemark; 
    PFGeoPoint *point = [object objectForKey:@"location"]; 
          [geocoder reverseGeocodeLocation:point completionHandler:^(NSArray *placemarks, NSError *error) { 
           NSLog(@"Found placemarks: %@, error: %@", placemarks, error); 
           if (error == nil && [placemarks count] > 0) 
           { 
            placemark = [placemarks lastObject]; 
            NSLog(@"%@", placemarks); 

           } 
          }]; 

錯誤,我得到明顯的是:

Incompatiable pointer types sending 'PFGeoPoint *' to parameter of type 'CLLocation *' 

回答

5

您需要使用顯式地創建CLLocation實例initWithLatitude:longitude:與來自PFGeoPointlatitudelongitude。只有從一個CLLocation創建PFGeoPoint,而不是其他方式輪的便捷方法。

6

正如韋恩說沒有從PFGeoPoint轉換爲CLLocation但是你可以使自己的使用擴展PFGeoPoint以下

import Parse 

extension PFGeoPoint { 

    func location() -> CLLocation { 
     return CLLocation(latitude: self.latitude, longitude: self.longitude) 
    } 
} 

現在你可以很容易地通過做

返回CLLocation對象方便的方法
let aLocation: CLLocation = yourGeoPointObject.location() 
0

你總是可以得到PFGeoPoint,然後將其添加到CLLocationCoordinate2D這樣的:

//get all PFObjects 

let parseLocation = ParseObject["location"] 

let location:CLLocationCoordinate2D = CLLocationCoordinate2D(latitude: parseLocation.latitude, longitude: parseLocation.longitude) 
2

基於賈斯汀Oroz的answer(見下面的答案): 也是一個偉大的想法可能是建立在CLLocation側進行擴展,以便它可以使用其接收PFGeoPoint便利的init創建:

extension CLLocation { 
    convenience init(geoPoint: PFGeoPoint) { 
     self.init(latitude: geoPoint.latitude, longitude: geoPoint.longitude) 
    } 
} 
相關問題