Dror
Dror

Reputation: 5505

parse server - how to extract longitude+latitude from ParseGeoPoint in cloud code

In cloud code, I want to get the longitude and latitude from a geopoint that was stored in the DB.

This is some of what I see in the logs - for afterSave function

2019-03-24T13:41:01.130Z - afterSave triggered for abc for user XYZ:
_ Input: {“geo”:{"__type":“GeoPoint”,“latitude”:32.7843083,“longitude”:34.6441017},“image”:_

In an afterSave cloud function - I’m reading the geoPint itself by calling:

const geo = request.object.get(“geo”);

But how do I get the longitude & latitude from the geo constant?

Upvotes: 1

Views: 246

Answers (2)

nataliec
nataliec

Reputation: 502

You can add in your cloud code environment the afterSave below:

Parse.Cloud.afterSave("Your_class_name", (request) => {

    const geopoint = request.object.get("geo");

    console.log("latitude: " + geopoint.latitude + "\nlongitude: " + geopoint.longitude);
});

Upvotes: 1

Dror
Dror

Reputation: 5505

In your afterSave function add the following const for your geoPoint:

const geo = request.object.get("geo");

and then, simply refer to:

geo.latitude

and

geo.longitude

The same works in any environment running the Parse JavaScript SDK (but your variable declaration would be different).

Upvotes: 2

Related Questions