Sunday, July 31, 2016

Find Nearest Location from list of Location Android

Simple snippet code what i do to find nearest location from current user location using google map v2 android api. 
Example
class BranchDetail{
    String name;
    double Longitude;
    double Latitude;

    //setter and getter
}

List<BranchDetail> bdList = new ArrayList<>();
// example
for(int x=0;x<100;x++){
    BranchDetail bd = new BrancDetail();

    // bd.setName("position 1");
    // bd.setLongitude(100.1);
    // bd.setLatitude(32.12);

    bdList.add(bd);
}

// example 2 using your webservice
bdList = restAPI.getBranchList();
Sort the list of location from nearest to farthest location from current user location
// im using FusedLocation to find current location
// Read more -> https://developers.google.com/android/reference/com/google/android/gms/location/FusedLocationProviderApi
mLastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);

private void SortLocationList(){
    Collections.sort(bdList, new Comparator<BranchDetail>() {
                public int compare(BranchDetail loc1, BranchDetail loc2) {
                    Location a = new Location("nearest");
                    Location b = new Location("target");
                    a.setLatitude(Double.parseDouble(loc1.getLatitude()));
                    a.setLongitude(Double.parseDouble(loc1.getLongitude()));

                    b.setLatitude(Double.parseDouble(loc2.getLatitude()));
                    b.setLongitude(Double.parseDouble(loc2.getLongitude()));

                    // compare with user location to find nearest location and sort
                    // mLastLocation is your current Location
                    // use GPS / Network provider to find your location or just use FusedLocation api
                    // mLastLocation must be find first before this process
                    return compares(mLastLocation.distanceTo(a), mLastLocation.distanceTo(b)) < 0 ?
                            compares(mLastLocation.distanceTo(a), mLastLocation.distanceTo(b)):
                            1;
                }
            });
}


Change the TOPNEAREST to any integer: ex : Top 3 nearest location just put 3
Assign marker to the choosen nearest
private void findNearestBranch() {
        if (mLastLocation != null) {

            SortLocationList();

            nearest = new Location("nearest");
            nearest.setLatitude(Double.parseDouble(bdList.get(0).getLatitude()));
            nearest.setLongitude(Double.parseDouble(bdList.get(0).getLongitude()));

            store = new ArrayList<>();
            final LatLngBounds.Builder builder = new LatLngBounds.Builder();

            for(int x=0;x<TOPNEAREST;x++){
                MarkerOptions marker = new MarkerOptions()
                        .position(new LatLng(Double.parseDouble(bdList.get(x).getLatitude()), Double.parseDouble(bdList.get(x).getLongitude())))
                        .icon(BitmapDescriptorFactory.fromResource(R.drawable.store_pin));
                Marker mark = googleMap.addMarker(marker);
                store.add(mark);
                builder.include(marker.getPosition());
            }

            final LatLngBounds bounds = builder.build();
            googleMap.animateCamera(CameraUpdateFactory.newLatLngBounds(bounds, 200));
        }
}

No comments:

Post a Comment