Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • John Bergman 483 posts 1132 karma points
    May 27, 2017 @ 16:21
    John Bergman
    1

    Programmatically create map - any examples?

    Are there any examples around showing how to programmatically create a map and set the location info from the back office, I am working on an importer for some information, and have not been able to get this to successfully work.

  • Jonathan Richards 288 posts 1742 karma points MVP
    May 29, 2017 @ 11:50
    Jonathan Richards
    0

    Hi John,

    Currently your are unable to create Terratype.Models.Model objects from scratch, which is what you would need to create some sort of import feature.

    For testing purposes, I do have a Unit Test that creates this class, but it cheats - It override the private accessor to set values.

    Unit test that creates Terratype maps

    The other solution would be to change the json yourself that Terratype stores - you can see this json by switching on debug mode when you create yourself a Terratype property type.

    Cheers

    Jonathan

  • John Bergman 483 posts 1132 karma points
    May 29, 2017 @ 17:15
    John Bergman
    1

    OK, I'll have a look at the unit test, I don't mind fiddling with JSON if needed, but would rather go a supported route.

    Any chance for a release that would expose the private assessors, even if it were a helper class, I don't really want to mess around with reflection unless I have to.

    As an alternative, is there a way to inject a longitude/latitude while rendering? I could keep the address data separate and just merge things while rendering the front end.

  • Jonathan Richards 288 posts 1742 karma points MVP
    May 29, 2017 @ 18:07
    Jonathan Richards
    0

    Hi John,

    Any chance for a release that would expose the private assessors

    I know it might sound like an oversight, but I was trying to stop people creating Terratypes configurations themselves and forcing them to only use the property type editor in the developer section. The setters as are, allows you to create any old configuration including map abominations, 99% of the time would crash Terratype, so what I need to do is add to them is lots of checking code. I'm not against doing this, I just require a free bit of time, I will add this to the roadmap.

    is there a way to inject a longitude/latitude while rendering

    Yes. Using the Position property of the Option class.

    @Html.Terratype(new Options { Position = new Terratype.CoordinateSystems.Wgs84("-51,-1") }, Model.Content.Map)
    

    (Options, as well as what is a Wgs84, is fully documented here)

    Cheers

    Jonathan

  • Jonathan Richards 288 posts 1742 karma points MVP
    Jun 16, 2017 @ 12:08
    Jonathan Richards
    0

    Hi John,

    This is a follow on, of your request via twitter. And my response.

    Recap: I have through this thread given you some different options on how to create Terratype properties;

    1. Create a Terratype DataType (Currently can only do via private fields)
    2. Set a Terratype Property Value (Need to set the json value yourself)
    3. Override the values when Rendering (My previous post in this thread)

    I feel I should expand on item 2: how to do set a Terratype Property Value in json using code.

    Obviously you would have executed some of these steps already, but I will repeat here, to cover everything.

    1. Install Umbraco
    2. Install Terratype with at least one Terratype provider
    3. Create a Terratype Datatype of your choosing in the developer section. Pick the style, icons and default values you require.
    4. Create a doctype in the settings section. For easy of example, The alias of this doctype will be 'mydoctype'
    5. Add the Terratype datatype that we created in step 3, to this doctype. The alias of this property will be 'mymap'. Feel free to add other properties but this example will not discuss them.

    .

    /// <summary>
    /// Create a content node in the root of the content tree
    /// </summary>
    /// <param name="nodeName">Name of the node</param>
    /// <param name="latitude">Latitude of the map marker</param>
    /// <param name="longitude">Longitude of the map marker</param>
    /// <param name="label">The text to appear if the user clicks the map marker</param>
    /// <returns></returns>
    public bool CreateMapContent(string nodeName, double latitude, double longitude, string label)
    {
        var content = Umbraco.Core.ApplicationContext.Current.Services.ContentService.CreateContent(nodeName, -1, "mydoctype", 0);
        var json = "{zoom: 5,label: {content: " + label + ",id: \"standard\",enable: true,editPosition: 0," +
            "view: \"/App_Plugins/Terratype/views/label.standard.html?cache=1.0.12\",controller: \"terratype.label.standard\"}" +
            "position: {id: \"WGS84\",datum: " + latitude.ToString(System.Globalization.CultureInfo.InvariantCulture) + "," +
            longitude.ToString(System.Globalization.CultureInfo.InvariantCulture) + "}}";
        content.SetValue("mymap", json);
        return ApplicationContext.Current.Services.ContentService.SaveAndPublishWithStatus(content).Success;
    }
    

    So to create content with a Terratype map, you would call CreateMapContent() with the values your desire. For example

    CreateMapContent("My node", 50.01, -2.56, "You have pressed the map icon");
    

    Please note that the json will change in the future, but hopefully I will always keep backward compatibility. I do plan to create some helper functions to help with the json creation process, but for now, thats all there is.

    Hopefully this helps a little more

    Cheers

    Jonathan

  • John Bergman 483 posts 1132 karma points
    Jun 17, 2017 @ 19:36
    John Bergman
    1

    Is there a way in the JSON to set the text of the address that corresponds to the longitude/latitude?

    For other's benefit, the snippet that generates the JSON above doesn't quite work, here is an updated one:

          var json = "{zoom: 5,label: {content: \"" + mapContent + "\",id: \"standard\",enable: true,editPosition: 0," +
            "view: \"/App_Plugins/Terratype/views/label.standard.html?cache=1.0.12\",controller: \"terratype.label.standard\"}," +
            "position: {id: \"WGS84\",datum: \"" + Latitude.ToString(System.Globalization.CultureInfo.InvariantCulture) + "," +
            Longitude.ToString(System.Globalization.CultureInfo.InvariantCulture) + "\"}}";
    

    Most of the issues was related to quoting the values correctly. (Thanks Jonathon for pointing me at least in the right direction :-)

  • Jonathan Richards 288 posts 1742 karma points MVP
    Jun 19, 2017 @ 08:12
    Jonathan Richards
    0

    Hi John,

    Sorry for the json, I'm pleased that you fixed it and got it working.

    As for

    Is there a way in the JSON to set the text of the address that corresponds to the longitude/latitude?

    Not currently, this would need a separate process to carry out the geocoding or reverse geocoding. There are lots of restrictions around this sort of process; Google for example limit it to 2500 requests per day. I do have plans to create a premium package that would download Open Street Map data to allow unlimited usage, but I'm many months away from being able to offer this.

    Cheers

    Jonathan

  • John Bergman 483 posts 1132 karma points
    Jun 22, 2017 @ 20:18
    John Bergman
    101

    Following up as promised. You can, in face add the location to the json (see below), however the code as Jonathon mentioned does not geocode nor make sure that it is valid.

    Here is a sample json with the address

        var json = "{zoom: 12,label: {content: \"" + mapContent + "\",id: \"standard\",enable: true,editPosition: 0," +
              "view: \"/App_Plugins/Terratype/views/label.standard.html?cache=1.0.12\",controller: \"terratype.label.standard\"}," +
              "position: {id: \"WGS84\",datum: \"" + Latitude.ToString(System.Globalization.CultureInfo.InvariantCulture) + "," +
              Longitude.ToString(System.Globalization.CultureInfo.InvariantCulture) + "\"}, lookup: \"" + mapAddress + "\" }";
    

    We are using this here to import data, and have not encountered any issues with our first block of around 500 records.

Please Sign in or register to post replies

Write your reply to:

Draft