ParseGeoPoint and CLLocationCoordinate2D

I find myself converting ParseGeoPoint lat and long data to CLLocationCoordinate2D often for use with Mapkit.

Is there an extension to ParseGeoPoint I can use to easily convert it? If not, I’m planning to make one.

Something like
let coordinate = geopoint.toCLLocationCoordinate2D()

Ideas?

And is something like this good to be part of ParseSwift? @cbaker6

thanks,
Jayson

There currently isn’t and this would be a useful addition. You will probably want to add an init for it as well like these ParseGeoPoint Structure Reference

Feel free to open up an issue on the Github repo along with a PR. You should look through the test cases for guidance. Your tests will most likely need to be added within this block:

Perfect. I’ve been meaning to contribute and this tiny piece of code would be the perfect way for me to learn the process.

Will open a PR once I get this done.

Thanks,

1 Like

I normally add:

extension CLLocation {
    func toGeoPoint() -> ParseGeoPoint {
        var geopoint = ParseGeoPoint()
        geopoint.latitude = coordinate.latitude
        geopoint.longitude = coordinate.longitude
        return geopoint
    }
}
extension ParseGeoPoint {
    func toCLLocation() -> CLLocation {
        return CLLocation(latitude: latitude, longitude: longitude)
    }
}

that’s basically what I added to the PR I’m adding. but in CLLocationCoordinate2D. Maybe I might as well add toCLLocation like you did in my PR.

I like the toGeoPoint() as well. but how would do that if I’m working on the ParseGeoPoint side? is that possible?

// MARK: Convenience
extension ParseGeoPoint {
    /**
     Creates a new `CLLocation` instance for the given `ParseGeoPoint`, set to the location's coordinates.
     - parameter geopoint: Instance of `ParseGeoPoint`, with set latitude and longitude.
     - returns: Returns a `CLLocation`
     */
    public func toCLLocation() -> CLLocation {
        return CLLocation(latitude: latitude, longitude: longitude)
    }
    
    /**
     Creates a new `CLLocationCoordinate2D` instance for the given `ParseGeoPoint`, set to the location's coordinates.
     - parameter geopoint: Instance of `ParseGeoPoint`, with set latitude and longitude.
     - returns: Returns a `CLLocationCoordinate2D`
     */
    public func toCLLocationCoordinate2D() -> CLLocationCoordinate2D {
        return CLLocationCoordinate2D(latitude: latitude, longitude: longitude)
    }
}

This is basically what I’m adding to the PR which I’ll submit soon. It’s my first PR so if you guys see any mistakes I need to fix, let me know.

 func testToCLLocation() throws {
        let point = try ParseGeoPoint(latitude: 10, longitude: 20)
        let location = point.toCLLocation()
        XCTAssertEqual(point.latitude, location.coordinate.latitude)
        XCTAssertEqual(point.longitude, location.coordinate.longitude)
    }

    func testToCLLocationCoordinate2D() throws {
        let point = try ParseGeoPoint(latitude: 10, longitude: 20)
        let location = point.toCLLocationCoordinate2D()
        XCTAssertEqual(point.latitude, location.latitude)
        XCTAssertEqual(point.longitude, location.longitude)
    }

And my Tests that pass.

changes were approved and should be in the next release. Though still no toGeoPoint() as it’s a CLLocation extension.

added that toCLLocation() :slight_smile:

The initializer that was already there gives you the same functionality as toGeoPoint: