I'm working on a website that contains many addresses in a region where geocoding is not reliable. The calculated location by geocoding is often hundreds of meters away from the actual location. Therefore, all locations are measured on the spot and stored in a geofield (gmap and location are not used).
I would like to use GetLocations to display driving instructions to the location of the node on the screen.
Hardcoding like / getdirections_box/latlon/51.3,4 works fine, but I need to make use of the tokens for the nid, or the location-geofield to be useful.
Is there a way to make use of geofield?

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

hutch’s picture

Getdirections does not have support for Geofield at the moment, mainly because I have not found a reliable way of getting the lat,lon when the nid is given, or any other primary identifier.
It does support data from Getlocations_fields and the Location module as these modules store their locative information in a known table and there are functions for reaching it.

GeertWil’s picture

Thanks for the fast reply, hutch.

I don't know if this is useful, but Geofield also stores it's data in a known field.

On my site I use a field named field_bdr_locatie to store the geofield. All my tables have a btm_ prefix, so the geofield data is stored in btm_field_data_field_bdr_locatie.
For a location with a latitude of 51.1018 and a longitude of 3.44138, find the following fields in the table:
- field_bdr_locatie_wkt (stored data: POINT (3.4413833333333 51.101797216667)
- field_bdr_locatie_lat (stored data: 51.1018)
- field_bdr_locatie_lon (stored data: 3.44138)
- field_bdr_locatie_top (stored data: 51.1018)
- field_bdr_locatie_left (stored data: 3.44138)
- field_bdr_locatie_bottom (stored data: 51.1018)
- field_bdr_locatie_right (stored data: 3.44138)
- field_bdr_locatie_geo_type (stored data: POINT)

Because geofield can be used to store polygons, it seems obvious that top, left, bottom and right are only useful for defining polygons.
I suppose I only need the data in the fields field_bdr_locatie_lat and field_bdr_locatie_lon.
I don't know much about PHP, so I have no clue how to reach those two fields in the btm_field_data_field_bdr_locatie table.

hutch’s picture

Yes, a human can figure it out in no time, but that is not the same as a function that will return the location information on any drupal install that includes Geofield and Getdirections. Computers are not as smart as we are (and a good thing too). I have however started work on a function that aims to do that.
In your case there is a field_name 'field_bdr_locatie' which you can see in table field_config which ties it to the module and type (both geofield in this case) and table field_config_instance which provides entity_type and bundle. Armed with this info and some more bits and pieces I should be able to get a $location array containing the lat and lon, plus if available any address details for the "To" or "From" lines in Getdirections.

More tomorrow ;-)

hutch’s picture

Status: Active » Needs review

Please test the latest dev, I have attempted support for Geofield, it works for me but needs to be tested by others. There are Views for Geofield, you will want to add a filter to limit the view to your content type(s)

GeertWil’s picture

Thank you very much hutch, I will give it a try.

GeertWil’s picture

Hi Hutch,
I have almost no experience with PHP, but could see a lot has changed in the code of the latest dev version to make the use of geofield possible. I couldn’t find out how to use it yet.
I think I use geofield in a special way on my site. I measured the exact locations of a large number of companies on the field and imported that data to the site. Latitudes and longitudes are stored in a geofield field.
Where several locations are shown on a single screen, I use a combination of Views, Geofield, Openlayers and OsmMapnik to display the data..
Getdirections is only used on detail pages where only a single node is displayed.
The driving instructions are retrieved via the following link: div class="rij-instructies" a class = "colorbox-load" href = "/btm/getdirections_box/location/to/[node: nid]?Width = 850 & , height = 600 & iframe = true" Hoe rijd ik naar dit bedrijf?/ a / div. (Removed opening en closing tags to make the code visible).
To be able to use those links I also installed the locations module en stored all addresses en locations a second time. This of course not very desirable, so for me it would be very nice to use a link like above to show the driving instructions based on the longitudes en latitudes stored in geofield.
I could not figure out if this is already possible in the latest dev build?

hutch’s picture

I have geofield working with getdirections (and getlocations) on my dev box.
To find out why your not able to use getdirections I would suggest that you forget colorbox for the moment.
First try the path:
"/btm/getdirections/location/to/xxx"
where 'xxx' is the node id (nid) of a geofield-enabled node.
If that does not work then we need to dig a bit deeper, using phpmyadmin or similar.

For instance, I have called the geofield I added to my content type 'field_location'. This then leads me to a table 'field_data_field_location' where the geofield data is stored.
There are two columns I am interested in, field_location_lat and field_location_lon.
Next, there is a table called 'field_config' which has columns 'field_name', 'type' and 'module'
'field_name' contains 'field_location'
'type' contains 'geofield'
'module' contains 'geofield'

Check if your setup is along the same lines.

hutch’s picture

Something has occurred to me. Could you try the following modification:
Line 1718, replace

 $geo = $obj->$geofield;

with

 $geo = $obj->{$geofield};

It works either way on my setup but there might be a difference in PHP version...

GeertWil’s picture

Hutch, thanks pal, you're my hero.
Everything works (almost) perfectly now, even in a colorbox.

I had quite some problems getting rid of the location module. I couldn't disable location cck the normal way, so I had to delete all location files, tables an fields by hand. As long as some location traces remained in the database, some nasty errors showed up, but now all errors are gone.

When I refresh a screen with geofield content, most of the time I get the following notice twice:
Notice: Undefined index: country in getdirections_setlocation() (regel 894 van C:\xampp\htdocs\btm\sites\all\modules\getdirections\getdirections.module).
Not a big deal.

Many, many thanks. I owe you one.

hutch’s picture

The fix for that notice is already in the bag, I just haven't pushed it up to drupal.org yet, waiting to see if there are any other bugs ;-). I have added curly brackets in all the right places and pushed it up.

The trick in getting rid of tables is to ensure you have deleted all the location-enabled node, deleted all the content types then run cron and it will let you uninstall in the usual way. Drupal needs to do some housekeeping first.

GeertWil’s picture

Version: 7.x-2.1 » 7.x-2.2

Hi Hutch,

I didn't test it in every situation I can think of, but it looks like version 2.2. works without any problems or errons with Geofield.
Thanks

sinasalek’s picture

Why not a field formatter or even a field widget ? Isn't it easier?
getdirections settings can be configured per field,
and it no longer needs a direct link like /getdirections/ , it can be link to a node with a geo field formatted using getdirections formatter
views, panels , etc support out of the box :)

http://api.drupal.org/api/drupal/modules!field!field.api.php/function/ho...
http://api.drupal.org/api/drupal/modules!field!field.api.php/function/ho...

hutch’s picture

Can you expand on this idea? I can't quite see how to do it.

sinasalek’s picture

Yeah sure
Right now if i want to represent my node geofield on it's node page using getdirections i'll have to use write code (using getdirections api and drupal theme system), also i can't easily use different getdirections settings for that particular field since the settings are global.
Instead of having a single generic page for configuration (/admin/config/services/getdirections) and another to displaying
(/getdirections)
It's better to have per field settings and representation, this way it's extremely easier to use
It's like the geofield module itself which has several different widgets for entering data and different formatter for showing, one of them is a map actually that can show the points on the map
It's actually the correct way of dealing with fields in Drupal 7

I can probably implement this myself , i can either implement it as a different module or a patch, let me know if you're interested

hutch’s picture

Yes I'm very interested, I would have thought as a submodule, eg in the getdirections folder as modules/getdirections_fields/getdirections_fields.module etc

I name it "fields" rather than "field" to avoid namespace hassles for functions.

If you can get something going I can get things integrated more closely with getdirections which will probably need modding.

Looking forward to seeing what you come up with,
Thanks!

sinasalek’s picture

FileSize
7.74 KB

Well it seems that due to the way module is designed it can't be implemented without refactoring part of the code.
It requires more time than i can afford unfortunately :)
But i'll share with you what i came up, it partially works but as i said needs some refactoring on get directions side
Hope you find it useful

sinasalek’s picture

FileSize
7.74 KB
hutch’s picture

OK, I will have a look.

hutch’s picture

Please see new issue #1918910: Getdirections Fields for more on Getdirections_fields

Summit’s picture

Issue summary: View changes

Wrong edit, sorry/