Problem/Motivation

Coming from #1393246: Interactive geocoding widget...

In many cases it would be useful to be able to use geocoding in combination with manual location selection (click on map).

Proposed resolution

Extend the existing openlayers map geofield widget to include optional geocoding (if geocoder is enabled).

Approach:

  • Reuse geocoder's widget settings.
  • If geocoder is enabled, add geocoder settings to existing openlayers map widget's field settings form along with a checkbox to select geocoder functionality. Use #states to conditionally set element visibility--show geocoder widget settings if use_geocoder is checked.
  • If use_geocoder is selected for the widget, on the relevant entity edit form for the field instance's entity type, include a button in the geofield widget to trigger geocoding. See screenshot in #7. Use #ajax to refresh the field portion of the edit form.
  • In a submit callback, geocode and set the form values. Requires #1627906: Enable other modules to use geocoding field processing.

Remaining tasks

User interface changes

Conditionally introduces a new button on the openlayers map widget for geofield.

API changes

Comments

nedjo’s picture

Status: Active » Needs work
StatusFileSize
new4.76 KB

Here's a beginning, along the lines outlined in "proposed resolution". It's a complex issue and there are several possible approaches--not sure this is the best, but thought I'd sketch something in. Not currently functional--geocoding in a validate callback doesn't set the rendered form values. Ideas or improvements on that issue or on the overall approach welcome.

nedjo’s picture

Should this be on 7.x-2.x? Or wait until the widget-revamp branch stabilizes?

Sketched in some relevant assumptions in #1393246-22: Interactive geocoding widget.

nedjo’s picture

StatusFileSize
new5.37 KB

Some minor fixes (output an element wrapper div using #prefix and #suffix, change button from 'submit' to 'button'). However, still not outputting the geocoded values in the returned form element.

Brandonian’s picture

Issue tags: +mapping

Tagging for map sprint on Friday, in case I don't make time to look at this before. Based on the summary, this looks like it's going to be a great addition to Geofield.

@nedjo, this should be based on the 7.x-2.x branch. I merged the two branches a few weeks ago, and haven't done a great job of cleaning up my extra branches in the public repo.

nedjo’s picture

Version: 7.x-1.x-dev » 7.x-2.x-dev
Status: Needs work » Needs review
StatusFileSize
new6.4 KB

Here's a patch on the 2.x branch.

The patch is now working thanks to a tip about working with AJAX submissions. Not sure if that's the recommended approach--unsetting an input, seems like a quirky workaround. Could use review by someone familiar with D7 AJAX forms.

The patch includes a couple of unrelated fixes I needed to do to get it working: removing a duplicate widget declaration from geofield_field_widget_info() and returning a form from openlayers_field_widget_settings_form(). I'll pull those into a separate patch when I get a chance.

This patch still requires the patch at #1627906: Enable other modules to use geocoding field processing.

nedjo’s picture

StatusFileSize
new6.23 KB

Reroll after recent changes. Also improve #states handling.

Requires #1637476: Geofield widget does not show and "Store each simple feature as a separate field." doesnt work and #1637904: Remove duplicate geofield_openlayers widget (the first of which presumably will require manual patching, since it changes contiguous lines).

nedjo’s picture

StatusFileSize
new314.95 KB

Attaching a screenshot of the widget when configured to use geocoder.

When the button is clicked, just the widget part of the form is returned via AJAX, including a point geocoded from the address (if one was entered).

However, if there's an error on the form (e.g., title field is empty), that error message is returned and no geocoding is done. Need to look into the code more closely to see how to avoid triggering validation errors.

nedjo’s picture

Issue summary: View changes

Update summary.

nedjo’s picture

StatusFileSize
new6.42 KB

Here's a considerably improved patch based on study of core's add_more field widget. Now doesn't trigger validation errors.

nedjo’s picture

Status: Needs review » Needs work

Reviewing with Patrick.

Needs testing with multiple values.

+++ b/geofield.widgets.openlayers.inc
@@ -86,6 +137,41 @@ function openlayers_field_widget_form(&$form, &$form_state, $field, $instance,
+      '#limit_validation_errors' => array(array_merge($parents, array($field_name, $langcode)), array($settings['geocoder_field'], $langcode)),

This line could use a comment. Also, determine if both fields need to be excluded or only the geocoder field.

+++ b/geofield.widgets.openlayers.inc
@@ -94,6 +180,46 @@ function openlayers_field_widget_form(&$form, &$form_state, $field, $instance,
+    drupal_array_set_nested_value($form_state, array_merge(array('input'), $parents, array($field_name)), $field_value);

Needs code comment.

Also, add a message if no geocoder results returned.

+++ b/geofield.widgets.openlayers.inc
@@ -94,6 +180,46 @@ function openlayers_field_widget_form(&$form, &$form_state, $field, $instance,
+function openlayers_widget_geocode($form, $form_state) {
...
+  return $form[$field_name];

This looks like it will fail if e.g. the geofield is nested in a fieldset. Find #parents and use drupal_array_get_nested_value().

phayes’s picture

@nedjo,

I've updated geocoder_widget_get_field_value in geocoder, so now you can pass an array of deltas instead of the whole entity.

nedjo’s picture

StatusFileSize
new7.11 KB

Thanks Patrick. Here's an updated patch addressing most of the issues noted in #9.

Changes:

  • Return just the individual field delta from the AJAX call. This change means the patch at least minimally works with multiple geofield values. It should also be fine with geofields nested in fieldsets, though I haven't tested that.
  • Improve inline code comments.
  • Manually wrap the geocoder elements of field settings in a fieldset for readability.
  • Suppress the geocoder button when generating the widget for the field settings default value.

Remaining issues:

  • For some reason on the returned content with multiple values the delta's weight widget is visible. Not sure if there's much we can do about that without hacking. If geocoder goes back to using a single widget for multiple values presumably this problem will disappear.
  • In #1637108-2: Lat/Lon FormAPI: Add button to force geocode Brandonian suggested "I think we should change the text on the button from 'Geocode,' which is technical jargon, to something like 'Find Me' or 'Find my Location,' which reflects what's happening in a way that an end user is more likely to understand." Presumably we should do the same here. Currently the label is "Geocode from [field label] field". What should it be instead? Maybe "Place" or "Map" or "Locate" instead of "Geocode"?
nedjo’s picture

Issue summary: View changes

Reference field_form_set_state().

nedjo’s picture

Status: Needs work » Needs review
StatusFileSize
new7.28 KB

Fixed the visible weight issue from #11 by unsetting the weight prior to returning the form element (same as is done in addressfield module with the address widget).

Changed "Geocode from [field label] field" to "Find using [field label] field" to be consistent with the labelling introduced in #1637108: Lat/Lon FormAPI: Add button to force geocode.

Successfully tested with:

  • Single value geofield
  • Multi-value geofield
  • Unlimited value geofield
  • Multiple value address field--geocodes to geofield by corresponding delta
  • Geofield nested in field group
  • Address field nested in field group

All identified issues now addressed so this is ready for review.

nedjo’s picture

Issue summary: View changes

Update remaining tasks.

henryblyth’s picture

The patch in #12 does not work for me. It is the first patch I have tried.

I can set everything up, and the AJAX call goes through and returns a map, with an ID increment. However, there is no new data on the map. Manually added points persist, but no point from addressfield data is added. Tested on both a clean content type setup and using a current addressfield that another field geocodes and displays correctly on an openlayers map.

Using all the latest versions:

  • addressfield 7.x-1.0-beta3
  • geofield 7.x-2.x-dev (Jun 23)
  • geocoder 7.x-1.1+8-dev
  • geophp 7.x-1.4
  • openlayers 7.x-2.0-beta1+46-dev

I can't wait for this to appear in a stable release! It will be extremely helpful for us.

Regards,
Henry Blyth
Oxfordshire County Council

nedjo’s picture

@henryblyth: thanks for testing!

There seems to be a problem with the geocoder dev release--it doesn't contain the latest code. Please try with a git clone of the geocoder 7.x-1.x branch. Please also apply the latest patches from the issues linked in the "Remaining tasks" section of this issue's summary.

phayes’s picture

Nejdo,

I've completed phase#1 of refactoring the openlayers widget. Please see: #1273802: Geofield/Openlayers widget refactor

I would love your commentary, especially around how I handled multiple-deltas. It works, but I feel it's pretty hacky, and would appreciate any insights or recommendations.

robcarr’s picture

I'm a bit confused regarding comments #14 and #15:

  • Should the patch at #12 be rolled against a Git clone of geofield-7.x-1.x-dev (@nedjo you mention geocoder)? If the 7.x-1.x branch is broken, is there anyone who can fix it?
  • @phayes does your commit to the 7.x-2.x branch accommodate the functionality of the patch at #12? I can't see any reference to Geocoder in the geofield.widgets.openlayers.inc file of that branch
phayes’s picture

Hi arggh,

No not yet, just laying some groundwork. I'll be taking a crack at applying the patch this week

leramulina’s picture

Subscribe

phayes’s picture

Status: Needs review » Fixed

I've committed this. Worked pretty much right out of the box with only minor changes. Thanks Nedjo.

steinmb’s picture

Great work phayes and nedjo. Is the only follow up task remaining? #1637904: Remove duplicate geofield_openlayers widget

phayes’s picture

Yup,

There's that, cleaning up doxygen, and updating documentation.

henryblyth’s picture

Rolled it into a local-copy of a production site and it worked nicely.

Thanks very much guys!

Status: Fixed » Closed (fixed)

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

osopolar’s picture

See follow-up issue for client side geocoding:
#2078255: Add optional client side geocoding to map input widget.

osopolar’s picture

Issue summary: View changes

Remove completed tasks.