aloisdeniel/flutter_geocoder

Find Address From Query only gets one address

madatr opened this issue · 4 comments

Hi,
Thanks for creating the useful library.

I am currently trying to implement a method to get possible addresses from a string.

Everything works as expected but i've noticed that 99% the findAddressesFromQuery function returns one address hit only. I've tried using broad queries to test it such as searching an area with multiple similar results but had no luck reproducing the expected results.

Ex: UAE Abu Dhabi Batten Embassy (An area with 20+ embassies but I only get one hit)

Google maps search:

Screenshot 2021-02-12 at 15 16 20

function used:

String query = "UAE Abu Dhabi Batten Embassy";
var addresses = await Geocoder.local.findAddressesFromQuery(query);

I was hoping someone could give me pointers on how to get the desired results.

Thanks,

try to change Geocode.local to Geocode.google

var addresses = await Geocoder.google(apiKey, language: "ID_id").findAddressesFromQuery(query)

or maybe you can use Google Place AutoComplete. you can just use it like you are doing for fetching data.

is this google api free? I tried earlier to look at different google apis and there were so many I didn't understand which one is which.

I need to search for a place and get the coordinates of the selected place.

is this google api free? I tried earlier to look at different google apis and there were so many I didn't understand which one is which.

I need to search for a place and get the coordinates of the selected place.

you need to activate google place API, to do that you need to login into your GCP then go to APIs & Services page and click on ENABLE API & SERVICE. then search Place API and activate it. but the important thing is to make sure that you had an API Key from Google.

Example code:

Future<List<Suggestion>> findLocation(String query, String sessionToken) async {
    final client = Client();
    var encoded = Uri.encodeComponent(query);
    final request = 'https://maps.googleapis.com/maps/api/place/autocomplete/jsoninput=$encoded&language=ID_id&components=country:id&key=$apiKey&sessiontoken=$sessionToken';
    final response = await client.get(request);
    if (response.statusCode == 200) {
      final result = json.decode(response.body);
      if (result['status'] == 'OK') {
        "DO WHAT DO YOU WANT WITH THE RESULT"
      }
      if (result['status'] == 'ZERO_RESULTS') {
        return [];
      }
      return [];
    } else {
      return [];
    }
  }

for your case, maybe you can use this API URL:

https://maps.googleapis.com/maps/api/place/autocomplete/json?input=UAE%20Abu%20Dhabi%20Batten%20Embassy&key="YOUR-API-KEY"

Screen Shot 2021-03-05 at 10 08 26

Good luck.

Super! Thanks a lot @sud0su for your kind help and tips.