0

Currently, I am working on a restaurant app where people can take photos and tag them to a Google Place. However, since my app is targeting developing countries like India, the Google Places are not all very well defined. So I am allowing users to search for a place which they think more accurately reflects the location of their favourite restaurant. I am using PlaceAutocompleteFragment fragment of Google Places API for android as input mechanism for users to search for their favourite joint. However, to make it easier for them to choose, I want to bias results towards a specific region - more specifically the city that they are performing the search from.

However, how do I get get city of the current location of the user? I can get latitudes, longitudes. But how do I retrieve the city corresponding to a certain lat and long?

Do I need to reverse geocode the address from lat, long to get address? But if I do that, then how do I reliably get a city name? And thereafter what would be the correct provision in the PlaceAutocompleteFragment to set a 'city' filter?

So what would be the easiest way to do it? Any ideas? Appreciate it.

Abhishek
  • 1,261
  • 1
  • 13
  • 30
  • I'm not sure what your problem is. `setBoundsBias()` takes a `LatLngBounds` as an argument; why do you need the city name? – Matt Dec 10 '17 at 18:31
  • @HEATH3N Because different cities have restaurants with same name. Additionally different cities may have different sizes. So for e.g. a metropolitan may have radius of 40-50 km, a small city may be just 10-20 km. Neighbouring cities may have several places sharing common regional names – Abhishek Dec 11 '17 at 06:49
  • Hmm, I would think you'd want to display a list of matches anyway, no? If the user were to search for a common chain restaurant like McDonald's, there might be 10 in a single city (or maybe that's just the US :/ ). You can either let them choose the one that they think it is (show all the locations on a map with the distance from user) or if it's a requirement that they be at the location, just limit the radius to a small distance and choose the first result. I will keep looking; there may be a way with either Mapbox or OSM. – Matt Dec 11 '17 at 07:14
  • Yes, exactly. You got my problem. Now imagine if I set a bound of 50Km radius, the number of options may come out even more making the experience that much worse.... So, my thought-process is that generally people will be very familiar with options within their city, so to just limit the radius to the city, if that is possible.... – Abhishek Dec 11 '17 at 07:23
  • Aha! You can use [Mapzen API](https://mapzen.com/products/search/geocoding/?query=Bedok%2C%20Singapore%2C%20Singapore&endpoint=place&gid=openstreetmap%3Avenue%3Anode%3A207725957&selectedLat=1.32398&selectedLng=103.93022&lng=103.82011&lat=1.33021&zoom=11.602664502454616); see `bbox` result. You can use that to generate `LatLngBounds` in Google Maps and pass that to `setBoundsBias()`. Mapbox also has the feature; I will try to make an example if I have time tomorrow. – Matt Dec 11 '17 at 07:40
  • Very much appreciate it... – Abhishek Dec 11 '17 at 10:05

2 Answers2

4
autocompleteFragment.setBoundsBias(new LatLngBounds(southwestLatLng, northeastLatLng));

setBoundsBias() takes a LatLngBounds which is made up of two LatLng objects representing the southwest and northeast points of a bounding rectangle. The Google APIs don't appear to provide a method of getting a city's bounds, so you will have to figure out what is an acceptable radius to search within and subtract/add from the user's current latitude and longitude to derive the two points of the bounding rectangle. See: Calculating bounding box a certain distance away from a lat/long coordinate in Java

Matt
  • 2,953
  • 3
  • 27
  • 46
1

Once you get the address form Geocoder you can get the city name form the address itself. Hope below code will help you on this

Geocoder geocoder = new Geocoder(this, Locale.getDefault());
 List<Address> addresses = geocoder.getFromLocation(MyLat, MyLong, 1);
 String cityName = addresses.get(0).getAddressLine(0);
 String stateName = addresses.get(0).getAddressLine(1);
 String countryName = addresses.get(0).getAddressLine(2);

For more in detailed google map example you check the links below: http://www.demoadda.com/demo/android/load-googlemap_107

Durga M
  • 534
  • 5
  • 17
  • Is it always the case that cityName will be in the first address line? Or can it vary between the different address lines... – Abhishek Dec 11 '17 at 07:10