Plywood
Plywood

Reputation: 899

How to copy latitude and longitude fields into a GeoPoint on Parse.com

I have imported a 16,000 row csv into a Parse.com class. Now I need to convert the latitude and longitude fields into a Parse.com GeoPoint data type field.

I have added the GeoPoint field and now must run an update to copy the lat/lng data from each row into their respective GeoPoint field.

Where in the API or Parse.com UI does one accomplish this? I can't seem to find it. I know it's possible because what sane team of developers would omit such a feature.

Upvotes: 3

Views: 1770

Answers (1)

Aaron Peterson
Aaron Peterson

Reputation: 56

The solution is indeed to create a Job using CloudCode. Here is an example in Javascript:

Parse.Cloud.job("airportMigration", function(request, status) {
    // Set up to modify user data
    Parse.Cloud.useMasterKey();

    var recordsUpdated = 0;

    // Query for all airports with GeoPoint location null
    var query = new Parse.Query("Airports");
    query.doesNotExist("location");
    query.each(function(airport) {
        var location = {
            latitude: airport.get("latitude_deg"),
            longitude: airport.get("longitude_deg")
        };
        if (!location.latitude || !location.longitude) {
            return Parse.Promise.error("There was an error.");
            // return Parse.Promise.resolve("I skipped a record and don't care.");
        }

        recordsUpdated += 1;
        if (recordsUpdated % 100 === 0) {
            // Set the job's progress status
            status.message(recordsUpdated + " records updated.");
        }

        // Update to GeoPoint
        airport.set("location", new Parse.GeoPoint(location));
        return airport.save();
    }).then(function() {
        // Set the job's success status
        status.success("Migration completed successfully.");
    }, function(error) {
        // Set the job's error status
        console.log(error);
        status.error("Uh oh, something went wrong.");
    });
});

Upvotes: 4

Related Questions