The Address module by Commerce Guys is a great piece of work, offering the right entry format for the entire world's addresses, and the underlying codebase can be used by any php project - not just Drupal. It's in use on a few of our D8 sites and works very well, unfortunately it doesn't connect with Google Maps, and the Geolocation module that handles rendering Google Maps doesn't offer an interface that provides both a written address and a map.
So, what to do? We could leave users entering their full address, and then ask them to place a pin on a map - but that's double entry and is asking for errors to creep in. What we really want is the address entered in the Address field and then rendered by the Geolocation module.
By quickly creating a Service that takes a provided address and returns a latitude and longitude we can perform a lookup using Google's API and update the node as it is (pre)saved. After a bit of working out how Google's current API key interface works to get valid keys to both perform server lookups and fulfil browser requests we were on our way.
The outline of the module & its creation are:
- Use Drupal Console to create a module and then a service in that module
- Add http_client ( this is Guzzle ) as a service you want to add from the container
- Your service interface needs to:
- have a way to accept the Address field and turn it into a google URL friendly string
- have Guzzle lookup the address - this should provide you with a JSON array containing the Lat & Lng for your address
- have return calls for lat & lng
- Use hook_node_presave($node) to load your service for the right node type(s), populate the address, make the request for lat & lng and then overwrite your Geolocation field with the returned values.
We're happy to send out copies of this module if you would like it - it will need changing for field names, API key & node type as it's very closely coupled to the site's structure.
The module is available to clone from https://github.com/BlueFusionNZ/geocode_lookup . Please send pull requests with improvements, fixes & comments.