PropK
PropK

Reputation: 687

google places api rank by distance

How do i use rankBy=distance for sorting a list of places by distance and display that distance beside the name of the business in a listview???

protected String doInBackground(String... args) {
    googlePlaces = new GooglePlaces();                      
    try {
        String types = null;
        double radius = 400;
        nearPlaces = googlePlaces.search(gps.getLatitude(),gps.getLongitude(), radius, types);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

Upvotes: 0

Views: 2896

Answers (2)

Kartick Mishra
Kartick Mishra

Reputation: 69

You can use this search URL to achieve what you described:

private static final String PLACES_SEARCH_URL = "https://maps.googleapis.com/maps/api/place/search/json?rankby=distance&";

public PlacesList search(double latitude, double longitude, double radius, String types) throws Exception {
    this._latitude = latitude;
    this._longitude = longitude;
    this._radius = radius;
    //this._rankby=_rankby;

    try {
        HttpRequestFactory httpRequestFactory = createRequestFactory(HTTP_TRANSPORT);
        HttpRequest request = httpRequestFactory.buildGetRequest(new GenericUrl(PLACES_SEARCH_URL));
        request.getUrl().put("key", API_KEY);
        request.getUrl().put("location", _latitude + "," + _longitude);
        //  request.getUrl().put("radius", _radius);
        request.getUrl().put("rankBy", _radius);
        // in meters
        request.getUrl().put("sensor", "false");
        //request.getUrl().put("rankby", _rankby);
        if(types != null) {
            request.getUrl().put("types", types);
        }

        PlacesList list = request.execute().parseAs(PlacesList.class);
        // Check log cat for places response status
        Log.d("Places Status", "" + list.status);
        return list;
    } catch (HttpResponseException e) {
        Log.e("Error:", e.getMessage());
        return null;
    }
}

Upvotes: 2

Eldon Elledge
Eldon Elledge

Reputation: 61

I am currently working on this myself.

  1. Do not included the "radius" parameter with rankBy=distance, as stated by the Google Places API documentation.
  2. Once you get the list of places back, you will have to make another call to get the details of each place so that you can get the Address & Lat / Lng for each one.
  3. Then using current location, calculate the distance from each place in the results.

Upvotes: 2

Related Questions