Google-Maps-for-Craft icon indicating copy to clipboard operation
Google-Maps-for-Craft copied to clipboard

Error when Google maps can't find the location

Open Jammooka opened this issue 10 years ago • 2 comments

Using the fieldtype in a field called groupAddress works brilliantly, unless Google Maps can't find the location.

{% set location = craft.request.getParam('location') %}
{% set params = {
    address: location,
    distance: 10,
    unit: 'miles'
} %}
{% set locEntries = craft.entries.groupAddress(params).section('groupEntries').relatedTo(service).order('distance asc') %}

If I enter something like "lalalala", which returns no results on google maps, this then throws the common "Column not found" error.

CDbCommand failed to execute the SQL statement: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'distance' in 'order clause'. 

If I leave off the .order('distance asc') parameter, then it doesn't throw an error, but it just shows all results. Outputting {{ locEntries | length }} returns the correct amount of results (even showing 0 when no results are found close to the entered location), but then if entering e.g. lalalala again, it shows the total of all entries, when I'd expect 0.

I can't see any way to check if the location is a valid one before trying to filter the results by it, else it'd be a simple conditional workaround, though obviously it returning 0 results and not throwing an error would be preferable.

Jammooka avatar Sep 02 '15 15:09 Jammooka

Any solution to this? Getting the same error, but there's no way to check if Google returns results before doing the select with .order('distance asc') added to the query...

Hoping to fix this before my site goes live!

kblizeck avatar Oct 19 '15 17:10 kblizeck

Sorry for the delayed reply on this. Was out with some serious health and personal issues most of last year. Im starting to get back into full health again and working my plugins. I have been working with a couple other users getting this same issue address. From what I can tell, the plugin code is working in the most recent 2 master branches. However, I think you need to conditionally order by distance only if there is an address available. Something like this...

.order((address ? 'distance' : 'title') ~ ' asc')

objectivehtml avatar Jan 14 '16 19:01 objectivehtml