2014-12-23 11 views
5

È possibile convertire PFGeoPoint con Parse su CLLocation?Come convertire PFGeoPoint in CLLocation

Ragione di essere sto cercando di ottenere i nomi dei luoghi da PFGeoPoint in questo modo:

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); 

           } 
          }]; 

errore che ottengo è ovviamente:

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

risposta

5

È necessario creare in modo esplicito l'istanza utilizzando CLLocationinitWithLatitude:longitude: con il latitude e longitude dal PFGeoPoint. C'è solo un metodo comodo per creare un PFGeoPoint da un CLLocation, non viceversa.

6

Come Wain detto non esiste un metodo conveniente per convertire da PFGeoPoint ad un CLLocation tuttavia è possibile rendere il proprio ed estendere PFGeoPoint utilizzando il seguente

import Parse 

extension PFGeoPoint { 

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

ora si può facilmente restituire un oggetto CLLocation facendo

let aLocation: CLLocation = yourGeoPointObject.location() 
0

si può sempre ottenere il PFGeoPoint e poi aggiungerlo a un CLLocationCoordinate2D come questo:

//get all PFObjects 

let parseLocation = ParseObject["location"] 

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

Sulla base di Justin Oroz di answer (vedere la risposta qui sotto): anche una grande idea potrebbe essere quella di creare l'estensione nel lato CLLocation in modo che possa essere creato utilizzando un init convenienza che riceve il PFGeoPoint:

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