I am working on a site for a non-profit organization. The organization is active in remote areas of Africa, where there may be no street addresses.

For some content, we are using the Address Field to automatically georeference the content. For content where there is no address, It would be nice to provide a fallback whereby users can use the default geocoder map (or another map interface) to manually choose coordinates.

Comments

greenskunk’s picture

You can still choose coordinates using the OpenLayers, Geofield, Addressfield, Geocoder modules. Have you seen the video that is linked on the Geocoder project page http://vimeo.com/38558646 ?

rich.3po’s picture

I too am looking for this - google is inaccurate with many postcode lookups in the UK, and addresses in Ireland don't even have a postcode so they are often way off the mark

I didn't notice anything in that video that allows for geocode from address with lat/long fallback?

If i have time to do this properly i'll look to create a patch for this

rich.3po’s picture

Status: Active » Needs review
StatusFileSize
new4.57 KB

Heres a first attempt at a patch to provide this functionality.

- It provides an option on the field config form to allow site admin to enable lat/long override as an option
- On the node (or whatever entity) form, the user can tick an 'override' checkbox which will expose the lat/long widget as a manual override. This uses the standard lat/long widget provided by geofield module.
- If the box is not checked, it falls back to using standard address geolocation

The patch probably needs re-rolling since i was unable to clone this project locally (not sure why..), so its generated straight off my project code.

Please can you review and consider rolling into the module? I can think of several projects i've worked on which would benefit from this

Cheers

rich.3po’s picture

Sorry accidentally left a hard-coded field name in there.... i'll get another patch done

rich.3po’s picture

StatusFileSize
new4.59 KB

new patch:

rich.3po’s picture

StatusFileSize
new4.64 KB

Updated patch attached.

Main points that need review:

1) I've added a new widget config setting ("latlng_override"). This settings won't exist for existing installations until the field form is re-saved, so ideally we need an update hook to roll this in.

2) I've not had a chance to test/review for multi-value fields, so this needs checking...

Dentorat’s picture

You've got a couple more instances of hard coded fieldnames in geocoder_element_validate, they should be $element['#field_name']. I've also noticed that if i save the node with override unchecked, it's checked again on node edit and that inputting a 0 into one of the fields doesn't work (this is very unlikely in my case, but I was testing).

I can come back to this tomorrow and try to address those issues if I dont see a reply, but thank you very much for the patch!

Dentorat’s picture

StatusFileSize
new4.43 KB

I've updated it to genericize the remaining field names and I switched it to check rounding to 3 digits since the value in the db is stored as 6 total digits (rounded to 4 for longitudes < 100, rounded to 3 for longitudes >= 100).
I've created a patch based off of dev

This does not address the 0 value issue and I have not tested this for multivalue fields

rich.3po’s picture

Nice one - my time is quite short the rest of this week but i'll try take a look at that "0" issue when i get a chance

artatac’s picture

Issue summary: View changes

I wondered if there was a similar patch available for the 7.2 branch

brylie’s picture

Version: 7.x-1.x-dev » 7.x-1.2
Status: Needs review » Patch (to be ported)
simon georges’s picture

Version: 7.x-1.2 » 7.x-1.x-dev
Status: Patch (to be ported) » Needs review

Let's keep the main version / status for the maintaining team (if any) to find them more easily.

larowlan’s picture

+++ b/geocoder.widget.inc
@@ -142,6 +149,68 @@ function geocoder_field_widget_settings_form($this_field, $instance) {
+    $element = geofield_field_widget_form($form, $form_state, $field, $instance, $langcode, $items, $delta, $base);

This adds a hard dependency on geofield - we need to update the info file (patch coming)

larowlan’s picture

StatusFileSize
new14.11 KB

re-roll and fixes #13

simon georges’s picture

Could we eventually not introduce the dependency to geofield module?
Are there use cases where we would have geocoder without the geofield module?

joelpittet’s picture

StatusFileSize
new5.46 KB

@Simon Georges not sure about the answers to your question but the idea behind this patch seems really good to have when geocoding doesn't give any or incorrect locations. Though I do agree it would be nice to not have the dependency.

I've changed out the !isset() for an empty() check because it is easier to read, nearly as fast, and should give FALSE results which !isset() will only deal with NULLs.

Also this is a re-roll because dev moved and the whitespace fixes are no longer allowing #14 to apply.

And I added a check to drush backfill, but not sure if that's a good idea or if it will work, need a guinue pig on that one.

joelpittet’s picture

Status: Needs review » Needs work

Doesn't quite work so well the fields for lat/lon that are trying to react on the checkbox may have changed.
The ones I have are rendering as hidden with field_location[und][geom][lat] as their name.

And I get a notice:
Notice: Undefined index: lat in geocoder_field_widget_form() (line 170 of /sites/all/modules/contrib/geocoder/geocoder.widget.inc).

Which is looking for $element['lat']['#default_value'].

cameron prince’s picture

StatusFileSize
new5.88 KB

Here's an updated patch with a few tweaks to get the feature working and error-free. There is still a bug with the states function that I haven't solved yet. When editing a node with a geofield, the lat/long fields are visible all the time, regardless of the override checkbox. Unchecking the checkbox does cause the values to be reverted back to the look-up and it now remains unchecked when saved. It just doesn't hide the fields.

GoempieK’s picture

Did you get this to work?
I need the exact same thing, have a lot of adreses that are gaspumps near the freeway with no exact adres.

jibran’s picture

StatusFileSize
new2.7 KB

Here is the interdiff from 16 and 18.

jibran’s picture

Status: Needs work » Needs review
StatusFileSize
new7.67 KB
new7.87 KB

I have added language and multivalue support to the patch and also added hook_field_attach_load to persist the geocoder_overridden on node load. This works nicely with workbench modration.

Edit: I have also fixed the states issue with lat lon textfields.

larowlan’s picture

  1. +++ b/geocoder.drush.inc
    @@ -61,4 +65,4 @@ function geocoder_drush_backfill() {
    \ No newline at end of file
    

    whitespace issue?

  2. +++ b/geocoder.widget.inc
    @@ -174,19 +181,120 @@ function geocoder_field_widget_settings_form($this_field, $instance) {
    +    // At present, the only way we know if the user has overridden the lat/long
    +    // values is to perform a geocode and compare with the existing values.
    +    $geocoded_value = geocoder_widget_get_field_value($instance['entity_type'], $instance, $form['#entity']);
    

    Good idea

  3. +++ b/geocoder.widget.inc
    @@ -174,19 +181,120 @@ function geocoder_field_widget_settings_form($this_field, $instance) {
    +function geocoder_element_validate($element, &$form_state) {
    ...
    +function geocoder_process_override($element) {
    

    nit: missing doc blocks

  4. +++ b/geocoder.widget.inc
    @@ -174,19 +181,120 @@ function geocoder_field_widget_settings_form($this_field, $instance) {
    +  // Loop over all the entities looking for entities with attached images.
    

    c/p error? no images here

  5. +++ b/geocoder.widget.inc
    @@ -174,19 +181,120 @@ function geocoder_field_widget_settings_form($this_field, $instance) {
    +        && ($geocoded_value = geocoder_widget_get_field_value($entity_type, $instance, $entity))) {
    ...
    +      && ($geocoded_value = geocoder_widget_get_field_value($entity_type, $instance, $entity))) {
    

    This should hit the cache in geocoder_cache_get() so shouldn't be that expensive

jibran’s picture

StatusFileSize
new1.92 KB
new8.08 KB

I found a bug while testing it locally so added a fixed for that also addressed #22. Thanks @larowlan for the review.

  1. Yeah, It's an existing issue and patch is fixing that.
  2. :)
  3. Fixed.
  4. Fixed.
  5. Yup.
jibran’s picture

StatusFileSize
new1.08 KB
new7.96 KB

c/p error once again fixed now.

jibran’s picture

StatusFileSize
new565 bytes
new8.1 KB

Found another minor issue.

broon’s picture

Status: Needs review » Reviewed & tested by the community

Very useful extension of the module and exactly what I needed. I applied the patch from #25 to 7.x-1.3 manually in an existing site and it works w/o flaws. Nice job!

autopoietic’s picture

Confirm tested and seems to work well, though the hiding of the lat/lon form element until override is checked (via #state) does not work in my example (may be to do with some custom form_altering).

One other issue, which may be as designed, is that if a location has failed to geocode, and there is therefore no location stored, when it is overridden the manually determined location works as expected, but when the override is unchecked, the location remains unless the content of the manual lat/lon fields is deleted.

bessone’s picture

Tested on last 7.x-1.x-dev, i have this error in node form:

Notice: Undefined index: lat in geocoder_field_widget_form() (linea 199 di /sites/all/modules/geocoder/geocoder.widget.inc).

DevJoshLopez’s picture

I also applied the patch from #25 to 7.x-1.3 manually in an existing site and it works but getting this error:

Notice: Undefined index: lat in geocoder_field_widget_form() (line 199 of /home/moonbots2017/public_html/sites/all/modules/geocoder/geocoder.widget.inc).

DevJoshLopez’s picture

.

jibran’s picture

+++ b/geocoder.widget.inc
@@ -174,19 +181,132 @@ function geocoder_field_widget_settings_form($this_field, $instance) {
+      (round($element['lat']['#default_value'], 3) != round($geocoded_value[$element['#language']][$delta]['lat'], 3)
+        || round($element['lon']['#default_value'], 3) != round($geocoded_value[$element['#language']][$delta]['lon'], 3))

Just needs isset check here.

DevJoshLopez’s picture

Thank you Jibran. Where does that code go? Doing this manually. Does it go below this?

$form['latlng_override'] = array(
    '#type' => 'checkbox',
    '#title' => t('Allow manual lat/long override'),
    '#default_value' => isset($settings['latlng_override']) ? $settings['latlng_override'] : FALSE,
    '#description' => t('Allow the user to override geolocation lat/long values manually. Useful when geocoding is inaccurate.'),
  );
letrotteur’s picture

Got the lat notice too and found out that lat and lon is stored under $element['geom']['#default_value']['lat'] instead of $element['lat']['#default_value'] in my project.

letrotteur’s picture

Made a patch based on my previous comment (geom instead of lat and lon) And added some logic for cases where we would want to override empty value (geocoded didnt returned anything or we only want to enter value manually.

Anonymous’s picture

geocoder-fallback-geocoding-1844840-34.patch worked for my site. I installed the Geocoder 7.x-1.x-dev - to get the most up to date, applied the patch - manually, and I got the one address marker that was about 5 miles off target to be right on.
Thanks for the great work!

jafacakes2011’s picture

I have a content type that is set up with a location field set to Geocode from an address field. On existing nodes with Geocoded data this works fine. However, When I make a new node the geocode fields remain empty.

I managed to fix this issue by going into the field setting and checking: Allow manual lat/long override. With that enabled, when I make a new node with an address, the geocoded field works as I expect. I'm not sure why, but it seems that no request is being sent when the Allow override is disabled.

I am running the patch from #34, and that has fixed the other issue I had with the Override option always being set to TRUE.

jibran’s picture

StatusFileSize
new2.19 KB

So #25 is for geofield 7.x-1.x and #34 for 7.x-2.x here is a interdiff between two patches.

jibran’s picture

StatusFileSize
new1.48 KB
new8.19 KB

Fixed some CS issue with #34 and added a condition to info file.

Note: This patch works with geofield 2.x branch.

jibran’s picture

StatusFileSize
new386 bytes
new8.14 KB

Here is a reroll and #25 with the version check.

pol’s picture

Status: Reviewed & tested by the community » Postponed (maintainer needs more info)

Hi,

What is the status of this patch ?

Thanks.

jibran’s picture

Status: Postponed (maintainer needs more info) » Reviewed & tested by the community

Patch from #25 has already has been RTBC in #26. It works well with geofield 7.x-1.x and #39 is just a reroll of that.

Patch form #38 is for geofield 7.x-2.x and in #35 it is reported to be working fine.

pol’s picture

Ok committed to 7.x-1.x.

  • Pol committed 3c3a7ae on 7.x-1.x authored by jibran
    Issue #1844840 by jibran, rich.3po, larowlan, letrotteur, Dentorat,...
pol’s picture

Status: Reviewed & tested by the community » Fixed
jibran’s picture

Thanks for the commit. Can we commit #38 to 2.x branch?

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.

Anonymous’s picture

Hi,

What was the reason for implementing the presave function in patch #39 (see geocoder_field_attach_presave)? because the logic is wrong when this is used at a new node. It does the following check: if ($instance['widget']['type'] === 'geocoder'&& !empty($entity->{$field_name}) && ($geocoded_value = geocoder_widget_get_field_value($entity_type, $instance, $entity))) { .. } which won't work because $entity->{$field_name} is empty in a new node, hence the fact that !empty($entity->{$field_name}) will always return FALSE. If you would remove this then it still doesn't work because inside the if-statement it loops over the field foreach ($entity->{$field_name} as $langcode => $items) { ... } which doesn't work as well because as I said this field is empty initially, so the actual values will never be set on a new node.

If I go back to v1.3 I see it does check if the field value is FALSE instead of empty() which does work. Is there a reason why this function has been built like this? Because now I am never able to use geocoder v1.4 in new projects where there's no content.

The setup I use is an address field + geofield, where the geofield is set to the address field by setting the widget "Geocode from another field".

If someone could fix this and at least explain the actual purpose of this, that'd be great.

- Thanks.