Skip to content
Advertisement

Android Studio – how to find nearest array location of my current location

the schema is I have 3 locations and I need to find where is the nearest location depending on my current location.

how to achieve that ?

and is it possible to use FusedLocation instead of LocationManager ?

Advertisement

Answer

you can use

double distance = SphericalUtil.computeDistanceBetween(place1.getPosition(), place2.getPosition());
        distance = (int)Math.round(distance);

place1 its current location

LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
                     
               place1 = new MarkerOptions().position(latLng);

place2 its one of our location

place2 = new MarkerOptions().position(new LatLng(latitude, longitude)).title("Position 1");

I use this function to switch to another activity if the distance is less than 5 meters

if (distance <= 5) {
            Intent intent = new Intent();
            intent.setClass(activity1.this, activity2.class);
            startActivity(intent);
        }
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement