<p>I think what you are running into is the getField() method on #147 of the MapService. What this does is return a model based on whether this is an existing record or a post request and if neither return an empty model. </p>

<pre><code>if ( 
        // Is this a Post request and isnt there a console request AND is there a value
        !\Craft::$app-&gt;request-&gt;isConsoleRequest
        &amp;&amp; \Craft::$app-&gt;request-&gt;isPost
        &amp;&amp; $value
    ) {

        // Create a new map model with the $value param - creating a model that takes the $value into account
        $model = new Map($value);
    } else if ($record) {

        // Is there a record? Create a model that takes these record params into account
        $model = new Map($record-&gt;getAttributes());
    } else {

        // We havent got a post request.or a record. This is what is happening to your input. 
        $model = new Map();
    }
EDIT---------------------------------------------
If you Edit $model = new Map(); TO $model = new Map($value); Your above code works fine. 
</code></pre>

<p>I am not sure as to the inner workings of Simple Map - have only used the C2 version. The best way to fix this for the long term is probably to create an issue on SimpleMap's github page. My recommendation is to replace the above else{} method, with what i described below. Maybe there is some way to get around this that doesnt involve source edits - like i said i dont know Simple map inside out - i might just be missing something obvious. In case there isn't i would recommend fixing the MapService with something like the below: </p>

<pre><code>else {

    Didnt meet the satisfied conditions. Is $value an array and does it exist? 
    if(!$value || !is_array($value)){

        // Not happy with this input - create a blank map. 
        $model = new Map();
    } 

        We're happy with the sended value - it isnt a post request. Lets take the value into account
        $model = new Map($value);
    }
</code></pre>

<p>I ran the above code with your example and it ran just fine. </p>

<p>I think it goes without saying that it is a terrible idea to just edit just your MapService file with the above code as a permanent solution as that will give you instability/incompatibility problems. What i would recommend is editing the method temporarily to test if this solves your problem - just to make sure. If it does it's probably wise to open an issue. Either if it does or doesn't - probably smart to open a github issue to see what the devs themselves say. </p>

<p>P.S - your code example is missing one array brackets at the end - probably smart to edit that before pasting it back.  </p>

<p>Hope i was able to help in someway :)</p>

CC BY-SA 3.0
2018-03-24T12:14:54.333Z
2018-03-24T12:14:54.333Z
Gįëł Tėttëłæår
<p>I am trying to fill in lat/lng data in the <a href="https://github.com/ethercreative/simplemap" rel="nofollow noreferrer">Simple Map</a> field of an entry through a plugin. All information from entry is saved except for Simple Map. Apparently I am doing something wrong there.</p>

<p>Here is my code:</p>

<pre><code>$entry = new Entry(); // use craft\elements\Entry;
$entry-&gt;.... // set fields
...
$entry-&gt;setFieldValues([
    'otherFields' =&gt; $value,
     ...
    'mySimpleMapFieldName' =&gt; [
       'lat' =&gt; 52.27, //$location['lat'],
       'lng' =&gt; 0.51 //$location['lng'],
]);
Craft::$app-&gt;elements-&gt;saveElement($entry);
</code></pre>

<plugin-development><craft3><plugin-simplemap>
Craft 3 add Simple Map in entry through plugin
# Craft 3 add Simple Map in entry through plugin

(Asked by: username_0 on 2018-03-23)

I am trying to fill in lat/lng data in the Simple Map field of an entry through a plugin. All information from entry is saved except for Simple Map. Apparently I am doing something wrong there.
Here is my code:
$entry = new Entry(); // use craft\elements\Entry;
$entry->.... // set fields
...
$entry->setFieldValues([
    'otherFields' => $value,
     ...
    'mySimpleMapFieldName' => [
       'lat' => 52.27, //$location['lat'],
       'lng' => 0.51 //$location['lng'],
]);
Craft::$app->elements->saveElement($entry);

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


---

## 1 answers

### Answer by username_1 on 2018-03-24. Score: 1

I think what you are running into is the getField() method on #147 of the MapService. What this does is return a model based on whether this is an existing record or a post request and if neither return an empty model. 
if ( 
        // Is this a Post request and isnt there a console request AND is there a value
        !\Craft::$app->request->isConsoleRequest
        && \Craft::$app->request->isPost
        && $value
    ) {

        // Create a new map model with the $value param - creating a model that takes the $value into account
        $model = new Map($value);
    } else if ($record) {

        // Is there a record? Create a model that takes these record params into account
        $model = new Map($record->getAttributes());
    } else {

        // We havent got a post request.or a record. This is what is happening to your input. 
        $model = new Map();
    }
EDIT---------------------------------------------
If you Edit $model = new Map(); TO $model = new Map($value); Your above code works fine. 

I am not sure as to the inner workings of Simple Map - have only used the C2 version. The best way to fix this for the long term is probably to create an issue on SimpleMap's github page. My recommendation is to replace the above else{} method, with what i described below. Maybe there is some way to get around this that doesnt involve source edits - like i said i dont know Simple map inside out - i might just be missing something obvious. In case there isn't i would recommend fixing the MapService with something like the below: 
else {

    Didnt meet the satisfied conditions. Is $value an array and does it exist? 
    if(!$value || !is_array($value)){

        // Not happy with this input - create a blank map. 
        $model = new Map();
    } 

        We're happy with the sended value - it isnt a post request. Lets take the value into account
        $model = new Map($value);
    }

I ran the above code with your example and it ran just fine. 
I think it goes without saying that it is a terrible idea to just edit just your MapService file with the above code as a permanent solution as that will give you instability/incompatibility problems. What i would recommend is editing the method temporarily to test if this solves your problem - just to make sure. If it does it's probably wise to open an issue. Either if it does or doesn't - probably smart to open a github issue to see what the devs themselves say. 
P.S - your code example is missing one array brackets at the end - probably smart to edit that before pasting it back.  
Hope i was able to help in someway :)


---

