Este artigo vai detalhar um exemplo de como se criar um aplicativo a partir da utilização do Parse GeoPoint. Ao final deste artigo, você conseguirá criar um sistema como o abaixo.
Passo 1. Faça o upload do GeoPoint para o Back4app
Na presente etapa, você pode carregar o GeoPoint a partir da criação de um ParseObject e adicionar um ParseGeoPoint ao campo:
// 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();
Passo 2. Consulte o Geopoint de localização próxima
Você pode efetuar a consulta de ponto de duas forma distintas:
- Consultando os GeoPoints mais próximos a uma localização definida;
- Criando um perímetro e coletando localizações dentro do mesmo.
Vamos iniciar agora da forma mais simples, que é a consulta dos GeoPoints mais próximos. Para tal, você necessita criar o GeoPoint e uma ParseQuery de forma que possa consultar a base de dados.
// 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();
}
}
});
Passo 3. Consulte o Geopoint de um perímetro
Neste caso, você necessita especificar os dois vértices do perímetro, ou seja, o Sudoeste e o Nordeste.
// 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();
}
}
});
O acesso ao código fonte pode ser baixado neste link.






