Reputation: 461
I have written some code to search nearby places based on their prominence. Next, I'd like to have my app search places based on ascending distance from the user. In order to do that, I learned that I need to use rankby=distance
but rankby
does not allow a value for radius
- so the following radius-based search doesn't work:
nearPlaces = googlePlaces.search(
gps.getLatitude(),
gps.getLongitude(),
radius,
types
);
I've seen several blogs and articles where people asked similar questions, but none of them provided an answer which seems to work. What should I be using, if not the above?
Upvotes: 1
Views: 6485
Reputation: 25
You can bypass it. I used 'GOOGLE PLACES WEB SERVICES' https://developers.google.com/places/webservice/search :
get a JSON of all your places using PLACES API (use HttpURLConnection to send the request) :
String nearByPlaceSearchURL = "https://maps.googleapis.com/maps/api/place/nearbysearch/json?"
+ "location=" + myLatPosition + "," + myLonPosition
+ "&radius=" + myPlaceDistanceMeters
+ "&types=" + myPlaceName
+ "&key=" + MyConstants.KEY_FOR_PLACES_AND_DISTANCES_API;
Parse the returned JSON and get the coordinates, LAT/LON, of each one of the places.
Get a JSON of all the distances from your current position to each one of the places using DISTANCE MATRIX API:
String distancesSearchURL = "https://maps.googleapis.com/maps/api/distancematrix/json?"
+ "origins=" + myLatPosition + "," + myLonPosition
+ "&destinations=" + allPlacesCoordinates
+ "&mode=walking"
+ "&key=" + MyConstants.KEY_FOR_PLACES_AND_DISTANCES_API;
Note that the distance is a walking,driving or cycling distance, which is the distance we are interested in. The radius distance (air distance) is not relevant. The walking/driving/cycling distances are bigger than the air distance.Therefore, for example, you can search for a place in radius of 10 km but the walking distance to it will be 11 km.
Create an Object for each one of the places with its name and distance from your current position as internal variables.
Generate an ArrayList containing all your Objects places.
Upvotes: 0
Reputation: 69
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: 1