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

<p>I started by looking at <code>SimpleMap_MapModel.php</code> and its attributes.</p>

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

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

<pre><code>$entry-&gt;setContent([
  'simplemap_field' =&gt; [
    'lat' =&gt; $lat,
    'lng' =&gt; $long
  ]
]);
</code></pre>

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

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

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

<p>The <code>address</code> 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.</p>

<p>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.</p>

CC BY-SA 3.0
2017-04-14T21:43:19.163Z
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
CC BY-SA 3.0
2017-04-17T19:35:18.760Z
@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
CC BY-SA 3.0
2017-04-14T17:25:19.917Z
2017-04-14T17:31:25.850Z
2017-04-14T17:31:25.850Z
August Miller
<p>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 <a href="https://github.com/ethercreative/simplemap" rel="nofollow noreferrer">simpleMap</a> field where that data will go.</p>

<p>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.</p>

<p>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.</p>

<pre><code>$entry-&gt;setContent([
    'simplemap_field' =&gt; [$lat, $long],
    'some_other_field' =&gt; 'some text',
]);
</code></pre>

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

<pre><code>$smap = New SimpleMap([$lat, $long]);
...
    'simplemap_field' =&gt; $smap,
...
</code></pre>

<plugin-development><plugin-simplemap>
Programaticaly create entry with simpleMaps field
# Programaticaly create entry with simpleMaps field

(Asked by: username_0 on 2017-04-14)

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,
...

**Tags:** plugin-development, plugin-simplemap

---

## 1 answers

### Answer by username_1 on 2017-04-14. Score: 1 | Accepted answer

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.

> **Comments:**
> **username_0**: 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.  
> **username_1**: @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.  

---

