Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert PFGeoPoint to CLLocation

Is it possible to convert the PFGeoPoint with Parse to a CLLocation?

Reason being I'm trying to get the names of places from PFGeoPoint like so:

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

                                }
                            }];

Error I get obviously is:

Incompatiable pointer types sending 'PFGeoPoint *' to parameter of type 'CLLocation *'
like image 622
Kimberly Avatar asked Dec 23 '14 19:12

Kimberly


3 Answers

As Wain said there is no convenient method to convert from PFGeoPoint to a CLLocation however you can make your own and extend PFGeoPoint using the following

import Parse

extension PFGeoPoint {

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

now you can easily return a CLLocation object by doing

let aLocation: CLLocation = yourGeoPointObject.location()
like image 139
Justin Oroz Avatar answered Nov 11 '22 03:11

Justin Oroz


You need to explicitly create the CLLocation instance using initWithLatitude:longitude: with the latitude and longitude from the PFGeoPoint. There is only a convenience method for creating a PFGeoPoint from a CLLocation, not the other way round.

like image 43
Wain Avatar answered Nov 11 '22 03:11

Wain


Based on Justin Oroz's answer (see the answer below): Also a great idea could be to create the extension in the CLLocation side so it can be created using a convenience init which receives the PFGeoPoint:

extension CLLocation {
    convenience init(geoPoint: PFGeoPoint) {
        self.init(latitude: geoPoint.latitude, longitude: geoPoint.longitude)
    }
}
like image 2
crisisGriega Avatar answered Nov 11 '22 04:11

crisisGriega