Reputation: 2709
I am new to coding in android and I am trying to use the google map API to display places nearby.
My code is:
public void onClick(View v) {
mMap.clear();
String search = "cvs"
String url = getUrl(latitude, longitude, search);
Object[] dataTransfer = new Object[2];
dataTransfer[0] = mMap;
dataTransfer[1] = url;
GetNearbyData getNearbyData = new GetNearbyData();
getNearbyData.execute(dataTransfer);
}
private String getUrl(double latitude, double longitude, String
nearbyPlace) {
StringBuilder googlePlacesUrl = new
StringBuilder("https://maps.googleapis.com/maps/api/place/nearbysearch/json?");
googlePlacesUrl.append("location=" + latitude + "," + longitude);
googlePlacesUrl.append("&radius=" + PROXIMITY_RADIUS);
googlePlacesUrl.append("&type=" + nearbyPlace);
googlePlacesUrl.append("&sensor=true");
googlePlacesUrl.append("&key=" + "{MY_KEY}");
return (googlePlacesUrl.toString());
}
the URL output is: https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=40.7692494,-73.9842065&radius=10000&type=cvs&sensor=true&key={MY_KEY}
The problem starts when I change the search word, when I put (under the URL type) cvs/parks/starbucks... (and probably other places) I keep getting the same result. For example: if my location is central park NY, I keep getting results for Chrysler Building Hotel Pennsylvania Hudson New York and more. Those palaces are not related to my search word (cvs/parks/starbucks...) Important note is that for banks I do get banks' results.
Can anyone explain me how to solve this problem? why for some words I do get correct results and for other I do not?
Thanks
Upvotes: 0
Views: 1251
Reputation: 32178
Please note that none of cvs
, parks
or starbucks
belong to the list of supported types that you can use in nearby search. You can consult the list of supported types in the official Places API documentation:
https://developers.google.com/places/supported_types
I believe you have to use a keyword
parameter when you construct the URL instead of the type
parameter.
keyword — A term to be matched against all content that Google has indexed for this place, including but not limited to name, type, and address, as well as customer reviews and other third-party content.
source: https://developers.google.com/places/web-service/search#PlaceSearchRequests
For example if I search 'cvs'
https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=40.7692494%2C-73.9842065&radius=10000&keyword=cvs&key=MY_API_KEY
I can see places that are relevant for this search as shown in my screenshot
Search for 'parks'
https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=40.7692494%2C-73.9842065&radius=10000&keyword=parks&key=MY_API_KEY
gives me the following results
Finally search for 'starbucks'
https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=40.7692494%2C-73.9842065&radius=10000&keyword=starbucks&key=MY_API_KEY
returns Starbucks places
I hope this helps!
Upvotes: 2