Copied to clipboard

Flag this post as spam?

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


  • Matt Tolliday 13 posts 81 karma points
    Mar 29, 2015 @ 14:25
    Matt Tolliday
    2

    Property Value Converter

    Not really a question, and i'm sure it can be improved, but I wanted to strongly type the output from this awesome editor. Wrote a tiny Value Converter for it. I can share here. Not sure if worth including in your lib. Great work!

    [PropertyValueType(typeof(Location))]
    [PropertyValueCache(PropertyCacheValue.All, PropertyCacheLevel.Content)]
    public class AngularGoogleMapsConverter : IPropertyValueConverter 
    {
        public bool IsConverter(PublishedPropertyType propertyType)
        {
            return propertyType.PropertyEditorAlias == "AngularGoogleMaps";
        }
    
        public object ConvertDataToSource(PublishedPropertyType propertyType, object source, bool preview)
        {
            return ConvertSource(source);
        }
    
        public object ConvertSourceToObject(PublishedPropertyType propertyType, object source, bool preview)
        {
            return source;
        }
    
        public object ConvertSourceToXPath(PublishedPropertyType propertyType, object source, bool preview)
        {
            return null;
        }
    
        private static Location ConvertSource(object source)
        {
            if (source == null)
            {
                return null;
            }
    
            var sourceString = source.ToString();
    
            var splitSource = sourceString.Split(',');
    
            if (!splitSource.Any())
            {
                return new Location();
            }
    
            try
            {
                var lat = Convert.ToDecimal(splitSource[0]);
                var lng = Convert.ToDecimal(splitSource[1]);
                var zoom = Convert.ToInt32(splitSource[2]);
    
                var location = new Location { Lat = lat, Long = lng, Zoom = zoom };
    
                return location;
            }
            catch (Exception ex)
            {
                return new Location();
            }
        }
    }
    
    public struct Location
    {
        public decimal Lat { get; set; }
    
        public decimal Long { get; set; }
    
        public int Zoom { get; set; } 
    }
    
    
    <script>
    var map;
    function initialize() {
        var mapOptions = {
            zoom: @Model.Location.Zoom,
            center: new google.maps.LatLng(@Model.Location.Lat, @Model.Location.Long)
        };
        map = new google.maps.Map(document.getElementById('map-canvas'),
            mapOptions);
    }
    
    google.maps.event.addDomListener(window, 'load', initialize);
    
    </script>
    
  • Jonathan Richards 288 posts 1742 karma points MVP
    Mar 30, 2015 @ 18:43
    Jonathan Richards
    0

    Coolio, saved me a bundle of time, #h5yr

  • Jonathan Richards 288 posts 1742 karma points MVP
    May 25, 2015 @ 23:02
    Jonathan Richards
    0

    Have implemented this code. It is now availbale in version 1.0.6

     

Please Sign in or register to post replies

Write your reply to:

Draft