Yes, you cannot use radius and rankBy in same request. But you can use rankBy=distance
and then count distance youself based on geometry.location.lat
and geometry.location.lng
. For exmaple in groovy i've done like that:
GeoPosition
and Place
classes are implemented by me, so don't expect to find them at the core libs :)
TreeMap<Double, Place> nearbyPlaces = new TreeMap<Double, Place>()
if(isStatusOk(nearbySearchResponse))
nearbySearchResponse.results.each {
def location = it.geometry.location
String placeid = it.place_id
GeoPosition position = new GeoPosition(latitude: location.lat,
longitude: location.lng)
Place place = new Place(position)
double distance = distanceTo(place)
//If the place is actually in your radius (because Places API oftenly returns places far beyond your radius) then you add it to the TreeMap with distance to it as a key, and it will automatically sort it for you.
if((distance <= placeSearcher.Radius()))
nearbyPlaces.put(distance, place)
}
where distance is counted like that (Haversine formula):
public double distanceTo(GeoPosition anotherPos){
int EARTH_RADIUS_KM = 6371;
double lat1Rad = Math.toRadians(this.latitude);
double lat2Rad = Math.toRadians(anotherPos.latitude);
double deltaLonRad = Math.toRadians(anotherPos.longitude - this.longitude);
return 1000*Math.acos(
Math.sin(lat1Rad) * Math.sin(lat2Rad) +
Math.cos(lat1Rad) * Math.cos(lat2Rad) * Math.cos(deltaLonRad)
) * EARTH_RADIUS_KM;
}
Simply put nearby places allows you to search more vaguely but only return specific results.
In Text Search, if you search for "restaurants", it will search for locations, establishments or names with the text "restaurants".Anything that is related to restaurants.
Meanwhile in nearby search, if you search for "restaurants", it will return restaurant establishments with or without the text "restaurants" but only establishments of "restaurants"
Best Answer
You need to pass any one of the supported type to your query against types.
Restricts the results to Places matching at least one of the specified types. Types should be separated with a pipe symbol (type1|type2|etc). See the list of supported types.
https://developers.google.com/places/documentation/supported_types