Como comparar Distancias do GPS?

Tenho o seguinte código:

location.setBlurRadius(10000);
location.calculateDistance(lat_1, long_1, lat_2, long_2);

Onde setBlurRadius Reduz a precisão em 10 mil metros
E calculateDistance calcula a distancia entre as coordenadas.

Eu gostaria de saber como eu faço um IF,
para dizer o seguinte:

Se as coordenadas do usuário estiver dentro do perímetro do lat_1, long_1 e lat_2, long_2 faça isso.

Já tentou algo como o código abaixo:

if((lat_1 >= lat_2 && lat_1 <= lat_2) && (long_1 >= long_2 && long_1 <= long_2)){
    //faça isso
}

@Jonathan_Medeiros eu utilizava assim,
porém falaram que não é o certo.

O certo é criar um “perímetro” como eu citei a cima, aqui abaixo vai o código das funções citadas:

/**
     * Sets the blur radius (in meters) to use for privacy reasons
     *
     * @param blurRadius the blur radius (in meters)
     */
    public void setBlurRadius(final int blurRadius) {
        mBlurRadius = blurRadius;
    } 

/**
     * Calculates the difference from the start position to the end position (in meters)
     *
     * @param start the start position
     * @param end the end position
     * @return the distance in meters
     */
    public static double calculateDistance(Point start, Point end) {
        return calculateDistance(start.latitude, start.longitude, end.latitude, end.longitude);
    }

    /**
     * Calculates the difference from the start position to the end position (in meters)
     *
     * @param startLatitude the latitude of the start position
     * @param startLongitude the longitude of the start position
     * @param endLatitude the latitude of the end position
     * @param endLongitude the longitude of the end position
     * @return the distance in meters
     */
    public static double calculateDistance(double startLatitude, double startLongitude, double endLatitude, double endLongitude) {
        float[] results = new float[3];
        Location.distanceBetween(startLatitude, startLongitude, endLatitude, endLongitude, results);
        return results[0];
    }

Se a distância calculada for menor que o raio, significa que está dentro do perímetro.
:slight_smile:

Então irá ficar, assim ?

if(location.calculateDistance(lat_1, long_1, lat_2, long_2) > location.setBlurRadius(10000)){
// TODO
}

Acho que compreendi sua ideia, se de repente você utilizar o addProximityAlert() localizado dentro de LocationManager, acho que você consegue chegar no resultado esperado.

Dá uma olhada nestes Links, talvez te ajude:
Documentação.
Exemplo de uso.

2 curtidas