Problem with If Logic with GPS on Android

Asked

Viewed 123 times

1

  • How It Works:

User’s GPS latitude must be between -22.899529 and -22.899922 so that if brood one perimeter. The app will only work some features if you are within this perimeter

.

  • Observing:

1) I didn’t insert the longitude because I’m trying to make it enter this If, but I’m having a hard time, in case possible please show examples with longitude together.

  • Shopping mall.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 it does not need]:
            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));

2 answers

7

So, guys, I read the argument and I think you’re having a misconception around here.

Latitude/Longitude should not be compared with greater or lesser, as they are not numbers, but rather 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 consideration not only a part of the coordinate, but the entire coordinate (lat/long)

For example, I had a project where I had the starting point of the city, and then I had the city area, and I needed to know if a lat/long was within the city area. This is more or less the idea the request of the friend, but more restricted to a shorter distance.

You could even check if the latitude/longitude is between one and the other, reversing the order of the query because lat/long is measured from right to left, from the meridians (https://stackoverflow.com/questions/21190923/is-a-lat-lon-within-a-bounding-box).

Then my solution would be this:

  1. Determine the perimeter using Bounds, by lat/long. If it is somewhere, I would use the google maps query to know the establishment Bounds, as reference https://stackoverflow.com/questions/27990985/how-to-check-if-latitude-and-longitude-of-a-specific-address-belongs-to-lat-and

  2. Create the Ounds, reference: https://stackoverflow.com/questions/15319431/how-to-convert-a-latlng-and-a-radius-to-a-latlngbounds-in-android-google-maps-ap

  3. Then check if the lat/long is inside the Latlngbounds created in 3, using the contains(Latlng) method, reference: https://developers.google.com/android/reference/com/google/android/gms/maps/model/LatLngBounds;

3


was watching your if code and noticed the following.

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

This will never enter, as for the latitude to be greater than -22.899529, you would be expecting a latitude like -22.899528 and for it to be less than -22.89992 you would need a latitude like -22.89993, then no latitude would enter your if.

The correct thing in my opinion would be for you to reverse the signals:

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

'Cause that would be on the track from "529" to "992," you know?

  • But look at my reasoning. -22.899529 is a number greater than -22.89992, as this first number is closer to being positive than the other, correct? So it can’t be <= -22.899529 and not >= -22.89992. Or I’m wrong?

  • It is wrong. You want the position to be between 2 values: -xx.xxx920 and -xx.xxx529, so it must be greater than -xx.xxx920 and less than -xx.xxx529.

  • But then @Thiagosaad, that’s what I answered, latitude <= -22.899529 && latitude >= -22.89992. Its latitude >= -22.899529 && latitude <= -22.89992.

  • @Victorpettengillfernandes, he does not enter the if.. I read the API and says to use the Location.distanceBetween , but it only calculates (Latitude of the User, Longitude of the User, latitude origin, latitude destination). I need him to calculate 2 LAT and 2 LONG+ the user’s LAT and LONG comparison.

  • @Márciooliveira Victor’s answer is correct. As it is negative number, for numbers before the -22.89992 are even bigger.

  • I said "It’s wrong" to the above comment of mine.

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.