Ricky Khatri
Ricky Khatri

Reputation: 960

Android fetch nearest locations or addresses of on google map according to a gps location

I need to fetch locations or addresses of nearest location of a current gps location to show on google map.Currently i am able to show some locaions on map but that are hardcoded i need to fetch all nearest location before drawing the map.

Is there any suggestion?

Upvotes: 1

Views: 712

Answers (2)

Harpreet
Harpreet

Reputation: 3070

Here is the way, that how can you achieve it.

        LocationManager locationManager = (LocationManager) context
                    .getSystemService(Context.LOCATION_SERVICE);
        LocationListener locationListener = new LocationListener() {
            @Override
        public void onStatusChanged(String provider, int status,
                Bundle extras) {
        }

        @Override
        public void onProviderEnabled(String provider) {
        }

        @Override
        public void onProviderDisabled(String provider) {
        }

        @Override
        public void onLocationChanged(Location location) {
            Double l1 = location.getLatitude();
            Double l2 = location.getLongitude();
            address = GetAddress(l1, l2);               
        }
    };
    locationManager.requestLocationUpdates(
            LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);  

private String GetAddress(Double lat, Double lon) {
    Geocoder geocoder = new Geocoder(context, Locale.ENGLISH);
    String ret = "";
    List<Address> addresses = null;
    try {
        addresses = geocoder.getFromLocation(lat, lon, 1);
        if (!addresses.equals(null)) {
            Address returnedAddress = addresses.get(0);
            StringBuilder strReturnedAddress = new StringBuilder("\n");
            for (int i = 0; i < returnedAddress.getMaxAddressLineIndex(); i++) {
                strReturnedAddress
                        .append(returnedAddress.getAddressLine(i)).append(
                                "\n");
            }
            ret = "Around: " + strReturnedAddress.toString();
        } else {
            ret = "No Address returned!";
        }
    } catch (IOException e) {
        e.printStackTrace();
        ret = "Location: https://maps.google.co.in/maps?hl=en&q=" + lat
                + "," + lon;
    } catch (NullPointerException e) {
        e.printStackTrace();
        ret = lat + "," + lon;
    }
    return ret;
}

And also add these permissions in AndroidManifest:

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

Upvotes: 1

Nirav Ranpara
Nirav Ranpara

Reputation: 13785

First get your current location Lattitude & Longitude, then get Lattitude & Longitude of each locations you have and find out distance of each place from your current location using distanceTo method of Location class and after that find out least distance from your list.

Upvotes: 1

Related Questions