geocoder-php / Geocoder

The most featured Geocoder library written in PHP.
https://geocoder-php.org
MIT License
3.94k stars 516 forks source link

Algolia provider returning empty results. #1023

Open mikebronner opened 4 years ago

mikebronner commented 4 years ago

Asking on behalf of @tanthammar, who posted this issue here: https://github.com/geocoder-php/GeocoderLaravel/issues/163


General Information

GeocoderLaravel Version: ^4.2 Laravel Version: 6 PHP Version: 7.3.8 Operating System and Version: OSX 10.14.6

Issue Description

I do understand that this might not be the proper place to ask, but I was hoping that someone might have a solution. :)

Unable to get this to work with the Algolia geocoder provider No errors, but always an empty collection returned.

(I have it working with the Nominatim and GoogleMaps)

Steps to Replicate

composer require geocoder-php/algolia-places-provider^0.1.1

in config/geocoder.php

'providers' => [
        Chain::class => [
            AlgoliaPlaces::class => [
                'my-api-key',
                'my-app-id'
            ],
            Nominatim::class => [
                'https://nominatim.openstreetmap.org',
                'en'
            ],
            GoogleMaps::class => [
                env('GOOGLE_MAPS_LOCALE', 'en-US'),
                env('GOOGLE_MAPS_API_KEY'),
            ],
            GeoPlugin::class  => [],
        ],
    ],

Stack Trace

[if an error is occuring, provide the complete stack trace here]

atymic commented 4 years ago

I've just double checked by running the test suite and it seems to be working correctly.

Can you share the code you are using to get the result, and the address you are geocoding?

Thanks

tanthammar commented 4 years ago

Hello, thank you for your reply.

I got it working, but the returned search result are a bit confusing.

Strange error. This works in Nominatim but returns error from this driver.

>>> $addresses = app('geocoder')->geocode('113 46, Stockholm')->dump('geojson');
TypeError: Return value of Geocoder/Dumper/GeoJson::dump() must be of the type string, bool returned
>>> 

This however works

$addresses = app('geocoder')->geocode('113 45, Stockholm')->dump('geojson');
>>> multiple results

Inconsistent results: Via geocoder

>>> $addresses = app('geocoder')->geocode('Idungatan 7c, 113 45, Stockholm')->dump('geojson');
=> Illuminate\Support\Collection {#3489
     all: [],
   }

Via the search field in Algolia app, places, dashboard I do get a suggestion, even though it is not the correct lat/long. (It suggests the city "Stockholm" in Sweden) Suppose this is not this repos fault :). I was expecting the same result or any result.

{
  "query": "Idungatan 7c, Stockholm",
  "suggestion": {
    "name": "Stockholm",
    "country": "Sverige",
    "countryCode": "se",
    "type": "city",
    "latlng": {
      "lat": 59.3326,
      "lng": 18.0649
    },
    "postcode": "100 05",
    "highlight": {
      "name": "<em>Stockholm</em>",
      "country": "Sverige"
    },
    "value": "Stockholm, Sverige"
  }
}
atymic commented 4 years ago

Hmm. I've double checked that the code is querying correctly without any filters and I still get no results.

Could this be due to https://www.algolia.com/doc/api-reference/api-parameters/aroundLatLngViaIP/?

tanthammar commented 4 years ago

I am not sure. This is all very new to me. Did you look into the first error?

atymic commented 4 years ago

@tanthammar

I'm guessing it's related to that. Can you try direct with that param disabled?

In regards to the first error, could you please add dd(json_last_error()) after the geocode call?

I'm pretty sure something is going borked with the array which causes the json encode to fail.

Thanks!

atymic commented 4 years ago

@tanthammar

Were you able to do the above?

atymic commented 4 years ago

@tanthammar

Were you able to do the above?