bvcms /CmsWeb/Areas/Public/Models/SGMapModel.cs

Language C# Lines 121
MD5 Hash 793fa29ebce820fd7551e794fde55f08 Estimated Cost $2,839 (why?)
Repository https://bitbucket.org/mahalowe/bvcms View Raw File View Project SPDX
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
using System;
using System.Collections.Generic;
using System.Linq;
using System.Data.Linq;
using System.Web;
using CmsData;
using System.Web.Mvc;
using System.Text;
using UtilityExtensions;
using System.Net;
using System.Xml.Linq;

namespace CmsWeb.Models
{
    public class SGMapModel
    {
        public int divid { get; set; }
        public SGMapModel(int id)
        {
            divid = id;
        }
        public class SGInfo
        {
            public string desc { get; set; }
            public string addr { get; set; }
            public string name { get; set; }
            public DateTime? schedule { get; set; }
            public string cmshost { get; set; }
            public int id { get; set; }
            public GeoCode gc { get; set; }
        }
        public class MarkerInfo
        {
            public string title { get; set; }
            public string html { get; set; }
            public double latitude { get; set; }
            public double longitude { get; set; }
        }
        public IEnumerable<MarkerInfo> Locations()
        {
            var q = from o in DbUtil.Db.Organizations
                    where o.Location != null && o.Location != ""
                    where o.DivOrgs.Any(dd => dd.DivId == divid) || o.DivisionId == divid
                    join gc in DbUtil.Db.GeoCodes on o.Location equals gc.Address into g
                    from geocode in g.DefaultIfEmpty()
                    select new SGInfo
                    {
                        desc = o.OrganizationName, //o.Description,
                        addr = o.Location,
                        name = o.OrganizationName,
                        schedule = o.OrgSchedules.First().MeetingTime,
                        cmshost = DbUtil.Db.CmsHost,
                        id = o.OrganizationId,
                        gc = geocode,
                    };
            var qlist = q.ToList();
            var addlist = new List<GeoCode>();
            var ret = new List<MarkerInfo>();

            foreach (var i in qlist.Where(ii => ii.gc == null))
            {
                i.gc = addlist.SingleOrDefault(g => g.Address == i.addr);
                if (i.gc == null)
                {
                    i.gc = GetGeocode(i.addr);
                    addlist.Add(i.gc);
                }
            }
            if (addlist.Count > 0)
                DbUtil.Db.GeoCodes.InsertAllOnSubmit(addlist);
            DbUtil.Db.SubmitChanges();

            string template = @"
<div>
{0}<br />
{1:ddd h:mm tt}<br />
<a href='{2}OnlineReg/Index/{3}' target='_top'>Signup</a>
</div>";
            return from i in qlist
                   where i.gc.Latitude != 0
                   select new MarkerInfo
                   {
                       html = template.Fmt(i.desc, i.schedule, i.cmshost, i.id),
                       latitude = i.gc.Latitude,
                       longitude = i.gc.Longitude,
                   };
        }
        public StringBuilder sb = new StringBuilder();
        private GeoCode GetGeocode(string address)
        {
            var wc = new WebClient();
            var uaddress = HttpUtility.UrlEncode(address);
            var uri = new Uri("http://maps.googleapis.com/maps/api/geocode/xml?address={0}&sensor=false".Fmt(uaddress));
            var xml = wc.DownloadString(uri);
            var xdoc = XDocument.Parse(xml);
            var status = xdoc.Descendants("status").Single().Value;
            if (status == "ZERO_RESULTS")
                return new GeoCode { Address = address };
            try
            {
                var loc = xdoc.Document.Descendants("location");
                var lat = Convert.ToDouble(loc.Descendants("lat").First().Value);
                var lng = Convert.ToDouble(loc.Descendants("lng").First().Value);
                return new GeoCode
                {
                    Address = address,
                    Latitude = lat,
                    Longitude = lng,
                };
            }
            catch (Exception ex)
            {
                sb.AppendLine(address);
                sb.AppendLine(status);
                sb.Append(ex.Message);
                return new GeoCode { Address = address };
            }
        }
    }
}
Back to Top