3

I am using the Geofield Map module which depends on the geofield and I want to save lng and lat pragmatically in the geofield via my controller. I have tried the methods below and they didn't work:

$WayPointEntity->field_geo_field->setValue("POINT(" .$waypoint['lng']." " .$waypoint['lat'].")");

I also tried:

$WayPointEntity->field_geo_field = array(
                  'input_format' => GEOFIELD_INPUT_LAT_LON,
                  'geom' => array('lat' => $waypoint['lat'], 'lon' => $waypoint['lng']),
              );


  $WayPointEntity->save();
1
  • Any error message ? I just wrote this answer on stackoverflow, it should help !
    – user26231
    Commented May 1, 2018 at 20:48

2 Answers 2

0

Try this :

$point = [
  'lat' => $lat,
  'lon' => $lon,
];
$value = \Drupal::service('geofield.wkt_generator')->WktBuildPoint($point);
$WayPointEntity->field_geo_field->setValue($value);
1

The reason why your first attempt failed is because you forgot to add the array square brackets to your setValue:

$WayPointEntity->field_geo_field->setValue(["POINT (" .$waypoint['lng']." " .$waypoint['lat'].")"]);
1
  • nice and simple.no need for the wkt bs
    – giorgio79
    Commented Jan 8 at 15:47

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.