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?

2
  • 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
    Commented Jun 16, 2016 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 Commented Jun 16, 2016 at 21:25

1 Answer 1

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);
2
  • +1 Perfect! Beat me to it, but your answer is spot on. :)
    – Lindsey D
    Commented Jun 17, 2016 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. Commented Aug 4, 2016 at 21:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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