1

I trying to match hardcoded latitude an longitude with dynamic latitude and longitude, but its not showing correct output, can anyone help me to sort out this error

My code is

            String Log = "-122.084095";
            String Lat = "37.422005";
            try {
                if ((Lat.equals(latitude)) && (Log.equals(longitude))) {
                    AudioManager audiM = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
                    audiM.setRingerMode(AudioManager.RINGER_MODE_SILENT);
                    Toast.makeText(getApplicationContext(),
                            "You are at home",
                            Toast.LENGTH_LONG).show();
                } else {
                    AudioManager auMa = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
                    auMa.setRingerMode(AudioManager.RINGER_MODE_NORMAL);

                    Toast.makeText(getApplicationContext(),
                            "You are at office ", Toast.LENGTH_LONG)
                            .show();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }

it always goes for else part...

Reena
  • 152
  • 8
  • Are you sure that the strings are actually equal? – Egor Feb 22 '13 at 10:01
  • Your measured coordinates are very likely not *exactly* the same as the ones you statically provide. You need to calculate the distance between the two and check if it is within a certain (accepted) range. – Veger Feb 22 '13 at 10:01
  • With GPS, there's always a chance for error due to the precision. You should probably convert the locations to numbers and compare the position fits with a small error amount. – vipw Feb 22 '13 at 10:03
  • could you print those latitude and longitude in LogCat and compare whether those strings are actually equal – VIGNESH Feb 22 '13 at 10:03
  • make sure check your hardcoded Lat and Log is equal to dynamic latitude and longitude. How you can get exact lat and long value – MuraliGanesan Feb 22 '13 at 10:05

6 Answers6

3

You don't want to use a String comparison here as you can't guarantee the level of accuracy with the real-time location.

The best way to handle this would be to determine the distance between the points and then determine if it's close enough for you to consider, approx, the same.

For this, we use distanceBetween or distanceTo

Docs are here and here

Examples can be found here. Here's one of those examples:

Location locationA = new Location("point A");

locationA.setLatitude(pointA.getLatitudeE6() / 1E6);
locationA.setLongitude(pointA.getLongitudeE6() / 1E6);

Location locationB = new Location("point B");

locationB.setLatitude(pointB.getLatitudeE6() / 1E6);
locationB.setLongitude(pointB.getLongitudeE6() / 1E6);

double distance = locationA.distanceTo(locationB);
Community
  • 1
  • 1
Rawkode
  • 21,990
  • 5
  • 38
  • 45
0

your matching is okay but you probably should not check for a gps location like this.

You should convert the location to something where you can check that you are in 10m radius of the location.

plaisthos
  • 6,255
  • 6
  • 35
  • 63
0

A nicer way would be to leave the long/lat as doubles and compare the numbers.

if(lat > HOME_LAT - 0.1 && lat < HOME_LAT + 0.1 && ...same for lon... ){}
alex
  • 6,359
  • 1
  • 23
  • 21
0

You can print dynamice Latitute and Longitute to Logcat and check with hardcoded Latitute and Longitute

Kirit Vaghela
  • 12,572
  • 4
  • 76
  • 80
0

The latitude and longitude are variables which vary from point to point, matter of fact they keep on changing while standing on the same spot, because it is not precise.

Instead of comparing the Strings, take a rounded value of the lat and long (in long or float ) and check those values within a certain range. That will help you out with the "Home" and "Office " thing.

For e.g :

  String Log = "22.084095";
    String Lat = "37.422005";

    double lng=Double.parseDouble(Log);
    double lat=Double.parseDouble(Lat);

    double upprLogHome=22.1;
    double lwrLogHome=21.9;
    double upprLatHome=37.5;
    double lwrLatHome=37.3;

//  double upprLogOfc=;
//  double lwrLogOfc=;
//  double upprLatOfc=;
//  double lwrLatOfc=;

    if(lng<upprLogHome && lng>lwrLogHome && lat<upprLatHome &&lat>lwrLatHome )
    {
        System.out.println("You are Home");
    }
  /*  else if(lng<upprLogOfc && lng>lwrLogOfc && lat<upprLatOfc &&lat>lwrLatOfc )
    {
        System.out.println("You are Home");
    }*/
    else
        System.out.println("You are neither Home nor Ofc");

But for the negative lat long you have to reverse the process of checking.

Shail Adi
  • 1,502
  • 4
  • 14
  • 35
0

Try this,

Use google map api to pass lat and long value you will get formatted address. And also pass dynamic lat and lng value same google api you will get formatted address. And then match two formatted address you will get result. i suggest this way you can try this

Use this google api. http://maps.googleapis.com/maps/api/geocode/json?latlng=11.029494,76.954422&sensor=true

MuraliGanesan
  • 3,233
  • 2
  • 16
  • 22