This page describes how you can configure Addressfield Autocomplete module with Address Field, geocode the coordinates to a Geofield and show the point on map.

Overview

Let’s say we have a content type "Tournament" which has an Address Field for the place where the tournament is played.

  • Address Field module is used to store the address
  • We will use Addressfield Autocomplete module that provides a nice autocomplete search widget for increased usability when user is entering the address

We also want to show the entered address on a map. For this purpose we need to add a Geofield to our content type.

  • Geofield module is used to store the coordinates of the address
  • This coordinate point can then be shown on a map. You can either use Geofield Map module or OpenLayers module for displaying the geofield on a map.
  • Geofield Map is included in the Geofield module and is very simple, lightweight and easy to use. If you need more configuration options for the map display, then Openlayers is worth checking. This documentation will not cover configuration of the Openlayers map.

Two ways to derive geofield coordinates from the address

There are two different ways how we can derive the geofield coordinates from our address field.

Option 1: Let Addressfield Autocomplete populate the coordinates to geofield

When Addressfield Autocomplete makes a search query to Google geocoding API, Google returns also the coordinates of the search result. Addressfield Autocomplete can write these coordinates to our geofield.
Screenshot showing widget settings

  • Go to manage your content type fields
  • Set the widget of the geofield to Fill from Addressfield Autocomplete
  • Go to edit the geofield and select the address field to be the source where data is filled from.

Notes about this option

  • Integration to Geofield is currently included only in the latest development release of the Addressfield Autocomplete module. At the time this document was written, Addressfield Autocomplete is in beta. The beta-2 release does not include the integration to Geofield.
  • User can fine tune the exact map location when editing the node by moving the marker on the map. If the user moves the marker, the new fine tuned coordinates are stored to the geofield which is obviously a good thing.
  • If your content is created via any kind of import or integration, this option is not feasible because Adressfield Autocomplete is fetching the coordinates from Google on the client side with javascript. If you need server side geocoding, see option 2 described below.
  • This option does not require Geocoder module.

Option 2: Use Geocoder module to derive geofield coordinates from the address

The coordinates of the address field can also be derived by passing the address to Geocoder module. In this option the geocoding API call is done on server side when the node is saved. Geocoder module supports multiple service providers, including Google Geocoding API and Yahoo Placefinder.
Screenshot showing widget settings

  • Go to manage your content type fields
  • Set the widget of the geofield to Geocode from another field
  • Go to edit the geofield and select the address field as the source where data is filled from.

Notes about this option

  • This option requires Geocoder module.
  • Geocoding is done from the address field value only. If you fine tune the position of the map marker when editing the node, the fine tuned map marker position will not be reflected on the map.
  • If your content is created via any kind of import or integration, this option is feasible because the geocoding is done on server side.
  • Note that Geocoding APIs might have usage limits. For example, when this document was written, Google allows 5 requests per second and max 2500 requests in 24 hours. Option 1 described above is not limited with these usage limits because the geocoding is done on client side by the user’s browser.

Showing the Geofield on map when node is viewed

Our geofield is now configured so that it will contain the coordinates of our address field. To show the coordinates on a map when the node is viewed:

  • Go to the Manage Display of the content type
  • Set the Format to be Geofield Map.

Screenshot showing geofield display settings

Geofield is a lightweight and simple module for map display. If you need more flexibility on how the map is displayed, you might want to try the OpenLayers module.

Comments

aDarkling’s picture

This page is misleading. In the first section, Addressfield Autocomplete and Geofield are working together.

In the second section, Geofield and Geocoder are working together.

This should actually be 2 separate pages.

TomGould01’s picture

Hi, I have set these fields up as described and that works great but when I create nodes programmatically I am having issues :(

The only data I have to begin with is the classic textual version of an address, Postcode, address 1,2, Locality and country

Has anyone managed to get these populating correctly from that programmatically??

I have tried populating the address fields that I have access to before node save but then get the following error

PHP Error:  Call to a member function getCentroid() on array in /home/tgould/www/ines/docroot/sites/all/modules/contrib/geofield/geofield.module on line 510
PHP Stack trace:
PHP   1. {main}() /usr/share/php/drush/drush.php:0
PHP   2. drush_main() /usr/share/php/drush/drush.php:16
PHP   3. _drush_bootstrap_and_dispatch() /usr/share/php/drush/drush.php:61
PHP   4. drush_dispatch() /usr/share/php/drush/drush.php:92
PHP   5. pwamp_go_import_importer() /usr/share/php/drush/includes/command.inc:175
PHP   6. node_save() /home/tgould/www/ines/docroot/sites/all/modules/custom/pwamp_go_import/pwamp_go_import.module:89
PHP   7. field_attach_presave() /home/tgould/www/ines/docroot/modules/node/node.module:1086
PHP   8. module_invoke_all() /home/tgould/www/ines/docroot/modules/field/field.attach.inc:917
PHP   9. addressfield_autocomplete_field_attach_presave() /home/tgould/www/ines/docroot/includes/module.inc:957
PHP  10. _addressfield_autocomplete_widget_get_field_value() /home/tgould/www/ines/docroot/sites/all/modules/contrib/addressfield_autocomplete/addressfield_autocomplete.module:521
PHP  11. _addressfield_autocomplete_widget_resolve_deltas() /home/tgould/www/ines/docroot/sites/all/modules/contrib/addressfield_autocomplete/addressfield_autocomplete.module:649
PHP  12. _addressfield_autocomplete_widget_values_from_geometry() /home/tgould/www/ines/docroot/sites/all/modules/contrib/addressfield_autocomplete/addressfield_autocomplete.module:668
PHP  13. geofield_get_values_from_geometry() /home/tgould/www/ines/docroot/sites/all/modules/contrib/addressfield_autocomplete/addressfield_autocomplete.module:688
WD php: Error: Call to a member function getCentroid() on array in geofield_get_values_from_geometry() (line 510 of                                      [error]
/home/tgould/www/ines/docroot/sites/all/modules/contrib/geofield/geofield.module).
Error: Call to a member function getCentroid() on array in geofield_get_values_from_geometry() (line 510 of /home/tgould/www/ines/docroot/sites/all/modules/contrib/geofield/geofield.module).
Drush command terminated abnormally due to an unrecoverable error.  
masipila’s picture

Support requests should be posted to module's issue queue, not to documentation page as comments. But to shortly amswer your question: Addressfield Autocomplete is a client side widget that is executed in the web browser. As described on this page, you can't have the field widget set to Addressfield Autocomplete when creating nodes programmatically.