Search for places around current location of the user - PlaceAutocompleteFragment - google-maps

What I need to do is implementing a PlaceAutocompleteFragment view to look for places around the user's current location.
I was taking a look at the official documentation but couldn't find a proper way to do it.
I've set a Place.TYPE_COUNTRY filter but it shows predictions of places all around the country and most of the results shown when typing are irrelevant for the user.
AutocompleteFilter filter = new AutocompleteFilter.Builder()
.setTypeFilter(Place.TYPE_COUNTRY)
.setCountry(country)
.build();
The PlaceAutocompleteFragment.setBoundsBias(LatLngBounds) method provides a way to restrict results to a specific region.
autocompleteFragment.setBoundsBias(new LatLngBounds(
new LatLng(-33.880490, 151.184363),
new LatLng(-33.858754, 151.229596)));
According to the documentation the code above restricts results to a specific region of Sydney, Australia. I think this is similar to what I want to get.
So the question is, is there a way to get the LatLng values to instantiate the LatLngBounds object programatically?
I mean, I want to get that values dinamically based on the current location of the user.

Do you just need the device's location? If so see Making Your App Location-Aware. You can then expand this into a bounding rect (one way is to use SphericalUtil.computeOffset() from the android-maps-utils library)
Finally, note that the bounds is not a strict restrict on results, it only biases results to the bounds.
Or are you asking how to find out what a good bounds would be to use for an arbitrary city/region in the world where the device is located? If so, then you could do the following
Do a reverse geocode lookup on the device's location using the Google Geocoding API.
Examine the results and pick the result at locality level?
Use the viewport of that result as the bounds.

Related

Google Places API - Nearby Search not as accurate as Maps?

Overall goal: map a list of completely unknown GPS coordinates to closest points of interests (I dont know if the coordinates are referring to a shop, mountain, museum etc.).
The Google Place API, exactly the Nearby Search (https://developers.google.com/places/web-service/search?hl=de#PlaceSearchRequests), is theoretically solving my problem: I can pass GPS coordinates and a radius and get a list of "places" back.
Unfortunately, the results are not really precise. However if I "google" the very same coordinates in Maps, I get very satisfying results.
Examples:
48.12429, 11.56774:
Maps shows shop "Kenneth Kobonpue" really close by;
API call with radius 10/50/100 does not mention this shop at all
48.15100, 11.57964:
Maps shows University of Munich;
API call with radius 10/50/100 just mentions some special university rooms
48.16404, 11.60370:
Maps shows "English Garden" (park in Munich);
API call with radius 10/50/100 does not mention this park once
etc.
Question: Is there an option to get better results using this API? Is there another (Google) API more suitable to my usecase?
Google Maps(maps.google.com) and Google Maps Platform APIs are two different products and will behave differently at times. Their features, results and behaviors are not expected to be always the same.
In general, Places API requests returns the prominent places depending on the parameters you have provided. It is possible that the place you are looking for is not a prominent point-of-interest given the distance from your location.
Taking your sample for the location, "48.16404, 11.60370", you'll get the expected "English Garden" in a distance of 500. Here's a sample request: https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=48.16404%2C%2011.60370&radius=500&key=YOUR_API_KEY
Another alternative is to use the "rankby" parameter and set it to "distance". This will bias the results according to their distance from the given location. However, you need to specify one or more of the following parameters: keyword, name, or type (required parameters if results are rank by distance). Here's another example: https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=48.16404%2C%2011.60370&keyword=garden&rankby=distance&key=YOUR_API_KEY
In addition, by default, each Nearby Search returns up to 20 establishment results per query only. However, each search can return as many as 60 results, split across three pages. It is also possible that the point of interest you are looking for is in the succeeding pages. You can use the next_page_token parameter in another search request to access additional results.
For more information regarding Places Nearby search, you can checkout this link: https://developers.google.com/places/web-service/search#PlaceSearchRequests
This seems to be a recent bug, I am pretty sure I saw thsi working before.
Maps search shows very different set of places vs
using the APi, regardless of ranking parameter https://maps.googleapis.com/maps/api/place/nearbysearch/json?keyword=grocery&radius=5000&location=51.5183332%2C-0.2752538&key=[YOUR_KEY_HERE]
In this case, it is very clear that the platform API returns incorrect results using a different center point than the parameter provided.

Set location on google map

I have created one small application, where user can store his/her contacts with full address including map. Therefore I am using the Google Maps API, where user can point out his/her address.
For this I have given address fields with Country, State and City. When user entered this information, I am showing the default address on Google Maps, then the user has to drag the marker/pointer onto his/her exact location.
Now, in this case I want the zoom level of googles map to be set as per location's population.
This means if the user entered city with no mans land (less population or any desert place or any jungle place or very small area on city then zoom level should be more, so user can see proper area on map) otherwise zoom level should be less.
Can some one guide me how to do this?
This won't be possible using the Google API's alone, (I'm unsure if demographics info has been released for the US, I know it was in the works, but it certainly won't work elsewhere). You will have to find an external source to provide your demographics information for you.
The Google API's do not keep a public record of population density.
You could however be a but more tricksy and use an approximation. You could for example do a places search in the area (using an arbitrary radius), count the number of results and use this to set your zoom level (again using an arbitrary value to determine high/medium/low etc). It's a fair assumption (though still an assumption) that any area with a large number of 'places' returned will be densely populated.
You don't need to base this on the population of the location. Please check the follwing link. I think it should give you an idea of the options you have.
Google Maps API v3 - Geocoder results issue with bounds
Check both my question and my answer. You will probably want to use the viewport object.

Closest of a list of addresses from a location

does anyone know how i can return a googlemap with pins for every location within a set radius. Don't necessarily need a specific distance from potential plotted route. I want the user to be able to specify their address (postcode) and a set distance (10,20,50 miles) and then i can show these results on a map. I have the list of postcodes, held in my database, do i need to generate something like a kml?
Cheers
Tim
This sounds like a classic Store Locator application, which is a bit complex to detail in an SE answer.
Google have an example documented, though.

How can i force google autocomplete to not include any results that out for requested radius?

I've got a problem with Google Autocomplete rest api.
I need to return predictions only for certain radius, but "radius" parameter doesn't filter incorrect results. Currently I get results from another cities and even other countries.
How can i force google autocomplete to not include any results that out of requested radius?
The location and radius parameters only act as a bias to the results, there is currently no way to restrict results to a specific radius. You can however restrict results to a specific country by passing the components parameter as detailed in the documentation.
If you use the location, radius and component parameters you will most likely get the desired results.
An example search for Eiffel Tower, with a lat,lng location in Melbourne, Australia, radius 50km, and country restrict to Australia returns "Eiffel Tower Chocolate & Confectionery" as the real Eiffel tower is not within the radius or in the country that the request is restricted to:
https://maps.googleapis.com/maps/api/place/autocomplete/json?input=Eiffel+Tower&location=-37.816836,144.963123&radius=50000&components=country:au&sensor=false&key=your_api_key
You can use optional parameter radius.
radius — The distance (in meters) within which to return Place
results. Note that setting a radius biases results to the indicated
area, but may not fully restrict results to the specified area. See
Location Biasing.
The only way I can see to force this is to do the filtering yourself in the result array that you get when performing the search. I haven't used the Autocomplete API, but it seems you are using the Maps API to search? In that case, you get a GeocoderResult object which you can inspect the coordinates for the items, and compare them to your desired point and radius.
A bit hacky, but quite straightforward.
This example (uses the Google Maps API v3 places library):
http://www.geocodezip.com/v3_GoogleEx_place-search.html
from this answer:
Google places autocomplete - location and radius not working
uses rankby=distance
You can process the results to remove any not within your desired radius.

How to compare Google Maps coordinates on an accurate way?

I have the coordinates(lat, lng) and the information associated to this point in an excel sheet.
I want to compare the coordinates that I have stored on a database(lat, lng) with the excel ones.
My main idea is to show the correct info about the road point on the info window associated to the marker.
My doubt is about how to choose the coordinates on the most accurate way?.
Any suggestion?
So I think I understand what you are doing - capturing the coordinates of a user's location and then you want to find information about what is nearby to that location. If I have that correct, I suggest you take a look at the Places Library.
Google provides a library/service that allows you to send a requestthat includes a location and radius or coordinates that define a rectangular bounds and the service will return information about nearby places of interest. There are many ways to refine the searches to focus on different types of features.
I think this is what you are looking for - if not, provide a little more detail and I will try to help you.