Using ParseGeoPoint on Android With sample simple app

In this doc, you’ll be able to build this system:

gm_20160907_204919

Which is able to upload GeoPoint locations to Back4App’s Server and query GeoPoints. Check out the next steps:

Step 1 – Upload GeoPoint to Back4App

In this step, you can upload the GeoPoint by creating a ParseObject and add a ParseGeoPoint to it as a field:

// Creating the ParseObject
ParseObject placeObject = new ParseObject("Restaurants");
// Creating the GeoPoint with latitude and longitude defined earlier in code
ParseGeoPoint point = new ParseGeoPoint(latitude, longitude);
// Then, it is necessary to add it to the object as some field
placeObject.put("Location", point);
// After that, just saveInBackground
placeObject.saveInBackground();

And that’s it! Now, we have to be able to query GeoPoints.

Step 2 – Query GeoPoint from Back4App near location

When querying points from Back4App, you can do so by querying the nearest GeoPoints to a defined location, or use a box to gather every location inside it. Let’s start with the nearest GeoPoints to a location.

To do so, you have to create a GeoPoint and then create a ParseQuery so that you can search in the Database:

// Creating a GeoPoint with the desired point
// latitude and longitude have to be defined before in the code
ParseGeoPoint location = new ParseGeoPoint(latitude,longitude);

// Creating a Query so that it can search in the DB
ParseQuery<ParseObject> query = ParseQuery.getQuery("Restaurants");

// Query locations near that point within the field named FavoritePlaces
query.whereNear("Location", location);

// Setting a query limit, to avoid an excess of results
query.setLimit(8);

// Then start the query with a Callback
query.findInBackground(new FindCallback<ParseObject>() {
    @Override public void done(List<ParseObject> objects, ParseException e) {

        // Iterating over the results
        for(int i= 0; i < objects.size(); i++){
            // Now get Latitude and Longitude of the object
            double queryLatitude = objects.get(i).getParseGeoPoint("Location").getLatitude();
            double queryLongitude = objects.get(i).getParseGeoPoint("Location").getLongitude();
        }

    }
});

And that’s it for querying locations near to a point! Now, let’s query locations inside a box, in San Francisco.

Step 3 – Query GeoPoint from Back4App inside box

To do so, you have to specify two vertices of the box: the southwestern and northeastern ones – or the point that is at the bottom and to the left, and the one that is in the top on the right:

// Bottom-left or southwestern point of San Francisco
ParseGeoPoint southwestOfSF = new ParseGeoPoint(69.244773,30.498611);

// Top-right or northeastern point of San Francisco
ParseGeoPoint northeastOfSF = new ParseGeoPoint(4.971406,148.273765);

// Create the query on the Class we will search
ParseQuery<ParseObject> query = ParseQuery.getQuery("Restaurants");

// Set the box we will search into
query.whereWithinGeoBox("Location", southwestOfSF, northeastOfSF);

// Start the query and define the callback
query.findInBackground(new FindCallback<ParseObject>() {
    @Override
    public void done(List<ParseObject> objects, ParseException e) {

        // Iterating over the results
        for(int i= 0; i < objects.size(); i++) {
            // Now get Latitude and Longitude of the object
            double queryLatitude = objects.get(i).getParseGeoPoint("Location").getLatitude();
            double queryLongitude = objects.get(i).getParseGeoPoint("Location").getLongitude();
        }
     }
});

That’s it for querying locations! Check out the full source code:

https://github.com/dominwong4/back4app-parse-android-GeoPointSample