If Logic Problem with GPS on Android

1
  • How It Works :
  

The user's GPS latitude should be between -22.899529 and   -22.899922 to create a perimeter . The application will only work some functionality if it is within this perimeter

.

  • Note :
  

1) I did not insert the longitude because I'm trying to make it   enter this If, but I am very   possible to show examples with longitude together.

  • Shopping.java
package com.vuforia.samples.Books.app.Neoris;

import android.app.Activity;

import com.vuforia.Vuforia;
import com.vuforia.samples.Books.ui.ActivityList.AboutScreen;

/**
 * Created by th on 14/06/17.
 */

public class Shopping {

    private String nomeShopping;
    private String licenseKey;
    private String accessKey;
    private String secretKey;

    private Activity mActivity;
    private int mVuforiaFlags = 0;

    public Shopping(AboutScreen aboutScreen) {
    }

    public Shopping() {

    }

    public String getNome() { return nomeShopping; }

    public void setNome(String nome) {
        this.nomeShopping = nome;
    }

    public String getLicenseKey() {
        return licenseKey;
    }

    public void setLicenseKey(String licenseKey) {
        this.licenseKey = licenseKey;
    }

    public String getAccessKey() {
        return accessKey;
    }

    public void setAccessKey(String accessKey) {
        this.accessKey = accessKey;
    }

    public String getSecretKey() {
        return secretKey;
    }

    public void setSecretKey(String secretKey) {
        this.secretKey = secretKey;
    }

    public void setDados(double latitude, double longitude){
//   -22.899529    -22.899922
        // O menor é o MAIOR
        // o maior é o MENOR
        if((latitude >= -22.899529 && latitude <= -22.899922)){
            setNome("Neoris");
            setLicenseKey("Ad84Z0z/////AAAAGSgcOhPVvkoniWypHW2Dfsw+iX69sih5qx5JH1PEs92sM2xIhbsKnb2eTNkfBeQymsfgyRswpCDi2Ocu78RH+5+7/fXED7hJPLf3T7k4xKKLl4z8OKfCmr8jE0L5wQdJVV9L3tiHEoUx67M4b3ZSlO3/AzDLYi2/Zt3z8fuPo62osy469O+bKBsKKZtnyX9K7RYwJ2colMQ1bIhQERjg1w5cEZLHUacXAI1ndYhzS2Xl5iwAz98VZ65sptn+PrA5Xno55VGddt7rSBmvwuhZzeyShWnOqiYFhjWg80F3PFv2H/hYfIt4ML37yerJxS/n0z8yFv1H5gPi+Abd5nfaboz/xx1WgXf0yDvQKslFV+rr");
            Vuforia.setInitParameters(mActivity, mVuforiaFlags, "" + getLicenseKey()); // Importante para o setLicenseKey.
            setAccessKey("f854427b00cf89cb4909cf3780a22f1fe6dd1daa");
            setSecretKey("16208b50224e2ff8d3c93f069d2c4adf739cea7a");

        } else if((latitude >= -22.899682) && (latitude <= -22.900006) && (longitude >= -43.178666) && (longitude <= -43.178197)){
            setNome("Outros");
            setLicenseKey("AULCxLD/////AAAAGa6JoRhAAk70lshljOUpGeN7XUgbJ/jA8ZGpdHb4EVUBTsJ5Z6C8FYvrRBtMsbePU6wI2DKgO3U7msQ9bMqY1+qn0SRY8K3raNYxd+cgBkbmiJDonnuvPr9Hd3RHo7ArwO1x8wGsA1sWw/Bo+q7HpjLbdKqM/ceI2IlnWJQTD+H47zlFuV63utnK/soPttLP+HmL1Lx/ko6uLLKe9yhuuAwpbkNR0UsNGYXlueTCOU/CfIot0VCBg2Kxpz4/cnlmCetedW0+bZypzh6gWfV9MS1Sh9n1LEYr8EXjpOEyHhaBPAgE3lX5khkVc3FRetD81WC7fRAQB5ozp6X1H4u04yWLbzWf8S8XSPK5a542gbNg");
            Vuforia.setInitParameters(mActivity, mVuforiaFlags, "" + getLicenseKey()); // Importante para o setLicenseKey.
            setAccessKey("6ad29c13cb8af364c7c7f893ca7651974a887f96");
            setSecretKey("370db33ce57acc73a84df1f6d19339da69e45235");

        }

    }
}
  • AboutScreen.java [The code is not complete because you do not need it]:
            latitude = gps.getLatitude();
            longitude = gps.getLongitude();

            /*
            *  Melhora a precisão do Resultado encontrado pelo GPS
            *  para (6) casas decimais com arredondamento.
            * */
            BigDecimal precisaoLatitude = new BigDecimal(latitude);
            BigDecimal precisaoLongitude = new BigDecimal(longitude);
            precisaoLatitude = precisaoLatitude.setScale(6, RoundingMode.UP);
            precisaoLongitude = precisaoLongitude.setScale(6, RoundingMode.UP);

            latitude = Double.parseDouble(String.valueOf(precisaoLatitude));
            longitude = Double.parseDouble(String.valueOf(precisaoLongitude));
    
asked by anonymous 20.06.2017 / 21:24

2 answers

3

I was looking at your if code and I noticed the following.

latitude >= -22.899529 && latitude <= -22.89992

This will never go in, because for latitude to be greater than -22.899529, you would expect a latitude of -22.89952 8 and for it to be less than -22.89992 you would need a latitude of -22.8999 3 , then no latitude would enter your if.

The correct in my opinion would be you to reverse the signs, so it would be:

latitude <= -22.899529 && latitude >= -22.89992

Because it would stay in the range of "529" through "992", did you understand?

    
20.06.2017 / 22:58
7

So, guys, I've read the discussion and I believe you're having a misunderstanding around here.

Latitude / Longitude should not be compared with higher or lower, because they are not numbers, but coordinates.

To delimit a perimeter of coordinates, you have to not compare between the two, but rather determine the bounds of the place and then compare.

This bound has to take into account not only a part of the coordinate, but the whole coordinate (lat / long)

One example, I had a project where I had the starting point of the city, and then there was the area of the city, and I had to know if a lat / long was inside the city area. This is more or less the idea the friend's request, but more narrowly from a smaller distance.

It would even look to see if the latitude / longitude is between one and the other, reversing the query order as lat / long is measured from right to left, from the meridians ( link ).

Then my solution would be as follows:

  • Determine the perimeter using bounds, for lat / long. If it is somewhere, I would refer the google maps query to know the bounds of the establishment as a reference link

  • Create the bounds, reference: link

  • Then check if lat / long is within LatLngBounds created in 3, using the contains (LatLng) method, reference: link ;

  • 21.06.2017 / 15:33