0

I have some code that first of all creates some LatLng objects, as below (just one as an example):

LatLng turnOne = new LatLng(52.512657, 1.011376);

Within the onLocationChanged method I want to check the LatLng I created with the current location and if they are equal then do something (in my case trigger speech).

I have tried numerous things, such as:

  1. Creating a LatLng from the Location object. I'm sure this might work but will the two compared LatLng objects have to be exact? If so trying to match them on the move with no threshold seems unlikely.

    LatLng a = new LatLng(location.getLatitude(), location.getLongitude());
    if(turnOne.equals(a))
    {
        ttobj.speak("stuff", 
                TextToSpeech.QUEUE_FLUSH, null);
    }
    
  2. Creating a Location object from the LatLng. I'm not sure why this didn't work. I am comparing two Location objects and allowing for a threshold of 10 meters or less (on a road) but it didn't seem to work (I heard no speech).

     Location loc1 = new Location("");
     loc1.setLatitude(turnOne.latitude);
     loc1.setLongitude(turnOne.longitude);
     if(location.distanceTo(loc1) <= 10)
     {
         ttobj.speak("stuff", 
                TextToSpeech.QUEUE_FLUSH, null);
     }
    

My question therefore is how can I either compare two LatLngs and allow for a slight threshold in tracking (5 meters say) or compare two locations.

The only thing I can find close to this question is Comparing two LatLng objects in google map v2 android but it hasn't helped me completely.

Community
  • 1
  • 1
AndrewNR
  • 134
  • 1
  • 2
  • 13

2 Answers2

4

Just to provide an answer to this.

To compare a global LatLng object such as:

LatLng aLatLng = new LatLng(51.116492, -0.541767);

To the location argument provided in:

public void onLocationChanged(Location location)

Works fine using:

Location locationOne = new Location("");
locationOne.setLatitude(aLatLng.latitude);
locationOne.setLongitudeaLatLng.longitude);
float distanceInMetersOne = locationOne.distanceTo(location);
AndrewNR
  • 134
  • 1
  • 2
  • 13
1

Using .equals is a bad idea because its unlikely the lat and long will ever be exactly equal. Directly comparing floating point numbers is never a good idea. If you don't already have 2 location objects, I'd use:

Location.distanceBetween(double startLatitude, double startLongitude, double endLatitude, double endLongitude, float[] results)

That's probably your best bet. By the way, if you want to trigger when a device gets close to a certain location, consider either Geofencing or LocationManager proximity alarms.

Gabe Sechan
  • 90,003
  • 9
  • 87
  • 127
  • Thanks for the response. For the moment I have decided to use my LatLng objects to create Location objects within the onLocationChanged method so I can compare them with the current location. I have done this using: Location locationOne = new Location(""); locationOne.setLatitude(turnOne.latitude); locationOne.setLongitude(turnOne.longitude); float distanceInMetersOne = locationOne.distanceTo(location); – AndrewNR Jun 25 '14 at 17:17
  • Then test them using if(distanceInMetersOne <= 50) { //do stuff ttobj.speak("You are at the end of Fairfield Drive. " + "Turn left to Sainsburys.", TextToSpeech.QUEUE_FLUSH, null); } – AndrewNR Jun 25 '14 at 17:18
  • If this fails I think LocationManager proximity alarms seem a good alternative. – AndrewNR Jun 25 '14 at 17:18