How to round the doubles values returned by Interface Location

1

I have a ArrayList that stores GPS coordinate objects and their elements are always compared to the double values returned by the getLatitude() and getLongitude() methods of Interface .

Problem

From the tests I've done, I realized that it's very difficult for the user's Smartphone to capture a GPS coordinate exactly like any of my ArrayList elements. So the solution I encounter for this problem is to round out the double values of the objects of my ArrayList and also round the double values returned by the getLatitude() and getLongitude() methods of Interface Location to be able to detect with more success when the user is near the places that I intend to control.

Does anyone know how to best round the doubles values returned by the getLatitude() and getLongitude() methods or indicate another solution to this problem?

Below is my ArrayList :

ArrayList<Coordenada> coordenadasPKs = new ArrayList<>();
    coordenadasPKs.add(new Coordenada(41.148185, -8.584755, 336.1));
    coordenadasPKs.add(new Coordenada(40.82614, -8.59654, 296));
    coordenadasPKs.add(new Coordenada(40.168235, -8.62813666666667, 199));
    coordenadasPKs.add(new Coordenada(39.69546, -8.50684666666667, 135));
    coordenadasPKs.add(new Coordenada(39.6148033333333, -8.49502833333333, 125));
    coordenadasPKs.add(new Coordenada(39.411415, -8.52799166666667, 99));
    coordenadasPKs.add(new Coordenada(39.1335366666667, -8.761665, 59));
    
asked by anonymous 16.10.2016 / 18:31

2 answers

2

Rounding the values may not be the best option.

What is usually used in this situation is the GeofencingClient API . It allows circular zones to be defined from a location and a radius.

These zones are then monitored, and events are posted when the user enters or leaves the circle.

Giving a full explanation of how to use the API is something that would be too long for an answer. You can find a tutorial here or browse Creating and Monitoring Geofences in the documentation.

Another option is to create LatLngBounds and check the user's location is within it.

A LatLngBounds is a rectangle defined by the coordinates of its northeast and southwest corners.

You can create one from a coordinate and distance to the corners as follows:

//Calcula o ponto à distância 'raio' do 'centro*' na direção 45º(canto superior direito)
LatLng norhtEast = SphericalUtil.computeOffset(centro, raio, 45);

//Calcula o ponto à distância 'raio' do 'centro' na direção 225º(canto inferior esquerdo)
LatLng southWest = SphericalUtil.computeOffset(centro, raio, 225);

LatLngBounds bounds = new LatLngBounds(southWest,norhtEast);

Where

  • center - LatLng from the center of the rectangle.
  • radius - Distance, in meters, from the center to the upper right (northeast) and lower left (southwest) corners of Bounds

To check if a LatLng is within LatLngBounds use:

boolean contains = bounds.contains(point); 

Edit, after your comment.

If you are not using the google maps api you do not have access to the LatLngBounds class.

Basically what you want is to know if the user is near or not at a particular location. To determine this, you only need to define what is "close by" and how to calculate the distance between two coordinates.

To calculate the distance spherical trigonometry should be used, usually the "haversine formula" is used.

As Bacco said, there are several implementations on the net, SphericalUtil class lib com.google.maps.android:android-maps-utils and this is mine helper class are examples of this.

Add a method to your Coordinate class that indicates, by the criterion (x meters), whether the coordinates passed as argument are close to no.

private boolean isNear(int criterio, double lat, double long){

    return GeoMath.getDistance(this.lat, this.long, lat, long) <= criterio;
}
    
16.10.2016 / 19:40
-1

As I suggested in the comment:

int indice = 0;

// lat - latitude do cliente
// lon - longitude do cliente

double menorDistanciaAoQuadrado = Math.Pow(coordenadasPKs[i].getLatitude() - lat, 2) + Math.Pow(coordenadasPKs[i].getLongitude() - lon, 2);

double distanciaAoQuadrado;
for (int i = 1; i < coordenadasPKs.length; i++) {
    distanciaAoQuadrado = Math.Pow(coordenadasPKs[i].getLatitude() - lat, 2) + Math.Pow(coordenadasPKs[i].getLongitude() - lon, 2);
    if (distanciaAoQuadrado < menorDistanciaAoQuadrado) {
        menorDistanciaAoQuadrado = distanciaAoQuadrado;
        indice = i;
    }
}

The closest point is coordenadasPKs[indice] . So I would solve this problem.

    
16.10.2016 / 19:51