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 are correct, Places API Textsearch does not support the
rankBy=distance
parameter. If you believe that this would be a useful feature, please submit a Places API Feature Request.You can however obtain the result you are looking for by performing a Places API Search Request using the
rankBy=distance
parameter and thekeyword=restaurant
parameter:The
keyword
parameter is matched against all available fields, including but not limited to name, type, and address, as well as customer reviews and other third-party content.