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 May 1 '18 at 20:48
0

Try this :

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

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'].")"]);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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