1

In a plugin I am writing, I am importing data from a older system and part of it is latitude and longitude data. One of the fields in Craft is a simpleMap field where that data will go.

I would like to create a new entry that has a simpleMap field as one of the fields. The github page doesn't have any documentation on how to do this, it mostly covers how to read data and display it in a template.

I have latitude and longitude data and I would like to add it to the simpleMap field. This code doesn't work but its what I'm looking for.

$entry->setContent([
    'simplemap_field' => [$lat, $long],
    'some_other_field' => 'some text',
]);

I think I probably have to create a simpleMap object first and pass it to the entry field, something like this:

$smap = New SimpleMap([$lat, $long]);
...
    'simplemap_field' => $smap,
...
1

I actually just had to do this for a project, so I hope this will shed some light on how to set the props:

I started by looking at SimpleMap_MapModel.php and its attributes.

The available properties are: ownerId, fieldId, ownerLocale, lat, lng, zoom, address, and parts. (Don't touch ownerId, fieldId, and ownerLocale—they'll get set automatically when saved!)

So, this means you can set any of the above by simply assigning an associative array to your EntryModel, before saving:

$entry->setContent([
  'simplemap_field' => [
    'lat' => $lat,
    'lng' => $long
  ]
]);

If you have the data already, and want to populate the address "parts", you can json_encode those into the parts attribute:

$entry->setContent([
  'simplemap_field' => [
    'lat' => $lat,
    'lng' => $long,
    'parts' => json_encode([
      'street1' => $address,
      'locality' => $city,
      'administrative_area_level_1' => $state,
      'country' => $country
    ])
  ]
]);

Not a huge fan of the way those "parts" are saved, but it's basically the serialized/cached response from Google.

The address attribute, as far as I could tell, is basically the value that ends up in the search bar, when you're using the field in the front-end. During our import, we were able to build a meaningful string here, from other data (i.e. a venue name and city), so if any records didn't have a complete set of information, admin didn't have to reconstruct the search, manually.

Unfortunately, SimpleMap will not geocode (or reverse-geocode) your information to fill in gaps… you'll need to manually save each entry. This was something we struggled with when building out import scripts, but fortunately only the more recent location-sensitive content really needed to be updated.

  • It looks like the lat and long fields are ignored when displaying the map in the control panel. If the address field is empty, the map will use its default location. All the fields get updated if the map is adjusted. – Sheldon Apr 14 '17 at 21:43
  • @Sheldon This is a good observation—I hadn't noticed this behavior, as (at minimum) we had some kind of venue information to populate the address param (which then caused a map query via JS, whenever an edit screen was loaded). Bummer that the plugin does not fall back to raw Lat/Lng data when rendering the map in the admin. – August Miller Apr 17 '17 at 19:35

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.