3

I have a console command plugin that needs to periodically go and fetch property data via API and import it into Craft. I can pull in basic property information but when it comes to importing the data into Smart Map field I'm a bit lost as how to format the data so that it saves correctly. Has anyone had to do this before?

Edit:

I don't think the console command part should make much difference. Basically I am trying to save a smartmapfield on my entry. From the API I am using I get a string with the full address. I first extract the postcode and then use the smartmap plugin to get the Lat and Lng values:

$postcode = $matches[0]; 

//Look up Latitude and Longitude from Google API

$response = craft()->smartMap->lookupCoords($postcode);
$lat = $response['lat'];
$lng = $response['lng'];

What I am not sure of is how to format this data so that it can be saved to the entry. So for standard fields thats straight forward:

$entry->getContent()->title = $property['RoomName'];

How do I do this for my smartmap field?

Mats Mikkel Rummelhoff
  • 22,361
  • 3
  • 38
  • 69
Dave Coggins
  • 588
  • 3
  • 12
  • Hi Dave. I haven't used the console command, but if you can show us what your code looks like so far, I may be able to help. – Lindsey D Jun 16 '16 at 18:36
  • Hi Lindsey I have updated my question with more details, basically I'm trying to work out how to save an entry with smartmap field from within a plugin – Dave Coggins Jun 16 '16 at 21:25

1 Answers1

5

I figured it out, didn't realise all I had to do was pass array like so:

$entry->getContent()->address = array('lat' => $lat, 'lng' => $lng, 'street1' => $street1, 'zip' => $postcode);
Dave Coggins
  • 588
  • 3
  • 12
  • +1 Perfect! Beat me to it, but your answer is spot on. :) – Lindsey D Jun 17 '16 at 20:47
  • Super useful, thanks. Arrived here after having problems instantiating a SmartMap_AddressModel, setting its properties, then assigning it to the field name property of an EntryModel object. – August Miller Aug 04 '16 at 21:49