ArnoldV/Our.Umbraco.GMaps

How to set geolocates via SurfaceController

Closed this issue · 2 comments

Hi,

I would like to set geolocates as string of lat/long via SurfaceController. But the Gmaps model can't be found.

var location = new Our.Umbraco.GMaps.Models.GmapsCoordinate  ->  (error here, class can't be found)
{
      Latitude = 48.8566,
      Longitude = 2.3522
};

var contentItem = _contentService.GetById(nodeExists.Id);
contentItem.SetValue("Location", location.ToString());

_contentService.SaveAndPublish(contentItem);

Also already tried with just string but it seems invalid format and error display maps in the backend umbraco.

var contentItem = _contentService.GetById(nodeExists.Id);
contentItem.SetValue("Location", "48.8566, 2.3522");

_contentService.SaveAndPublish(contentItem);

Can you please suggest?

Regards,

If you have the SurfaceController in a Core project and the package installed in a Web project, you need to reference to the Our.umbraco.GMaps.dll in Web project.

You also need to store the data in the property as JSON.

Either contruct as JSON string like this:

string json = @"{
  'address': {
    'latlng': '',
    'full_address': '',
    'postalcode': '',
    'city': '',
    'state': '',
    'country': ''
  }
}";

or serialize the model:

var gmaps = new GmapsModel
{
    Address = new GmapsAddress()
    {
        Coordinates = $"{googleAddress.Coordinates.Latitude.ToString(CultureInfo.InvariantCulture)}, {googleAddress.Coordinates.Longitude.ToString(CultureInfo.InvariantCulture)}",
        FullAddress = googleAddress.FormattedAddress,
        PostalCode = store.Zip,    
        City = store.City,
        State = "",
        Country = store.CountryName
     }
};

var mapJson = Newtonsoft.Json.JsonConvert.SerializeObject(gmaps);

node.SetValue("location", mapJson);

Hi bjarnef,

Json string is works for me. Thank you very much.