简体   繁体   中英

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?

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);
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM