2

I have read in a lot of sources that I should use the geofield_compute_values() when trying to programmatically save coordinates in Drupal, but it does not work for me. That function doesn't exist in Drupal 8.5.2.

I've installed the Geofield module using Composer and I can use it as usual in the admin area. There are no problems with saving.

This is the code I tried, which returns an error about a not existing function (geofield_compute_values()).

$geofield_data = geofield_compute_values(['lat' => $lat, 'lon' => $lon    ], GEOFIELD_INPUT_LAT_LON);
$cbisProduct->set('field_koordinater', $geofield_data);

I have also tried this out with no successful result and no errors.

$geofield = [
  'geom' => "POINT (" . $lon . " " . $lat . ")",
  'geo_type' => 'point',
  'lat' => $lat,
  'lon' => $lon,
  'left' => $lon,
  'top' => $lat,
  'right' => $lon,
  'bottom' => $lat,
];
$cbisProduct->set('field_koordinater', $geofield);

I also tried the solution given in How to save LNG and LAT progamatically in a geofield, but it didn't work either.

How can I programmatically save coordinates?

1 Answer 1

-1

Try this:

$entity->set('field_geofield_name', [sprintf("'POINT (%s %s)'", $longitude, $latitude)]);

Cheers!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.