Bayan
Bayan

Reputation: 283

Find the nearest location in google maps for android

Is there a way to find the nearest location in Google map according to a specific point ( Long/lat), i read some thing about Google Place API, but i did not realize how to use it.. And is it possible to find the next nearest one too???

Upvotes: 3

Views: 5864

Answers (3)

Anshul
Anshul

Reputation: 145

HERE IS THE WAY YOU CAN 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: 2

Chris Green
Chris Green

Reputation: 4427

Using the Google Places API Web Service you could perform a Places Search Request where location is the users lat,lng:

https://maps.googleapis.com/maps/api/place/search/json?location=-33.8670522,151.1957362&rankby=distance&types=food&sensor=false&key=AddYourOwnKeyHere

Make sure you specify the rankby=distance parameter instead of the radius parameter if you wish to obtain the closest places in chronological order instead of the most prominent places in the specified radius.

I have also specified a types=food parameter in the request above to only return food type places in my response. Additional supported types can be found here.

To use the Google Places API Web Service you will need to follow the instructions here to obtain an API Key to use the service, this key will need to be sent with your request under the key=AddYourKeyHere parameter.

If you are looking to use this service in a JavaScript application, check out the Places Library of the Google Maps API v3.

Upvotes: 3

Shankar Agarwal
Shankar Agarwal

Reputation: 34765

Google Places API.

Url::

https://maps.googleapis.com/maps/api/place/search/json?location=-33.8670522,151.1957362&radius=500&types=food&name=harbour&sensor=false&key=AddYourOwnKeyHere

Need to change lat,long, radius, types, name and key.

Note:: Need to Sign up for the developer preview.

Upvotes: -1

Related Questions