PageRenderTime 55ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://bitbucket.org/rbhakser/bvcms
C# | 120 lines | 117 code | 3 blank | 0 comment | 14 complexity | 793fa29ebce820fd7551e794fde55f08 MD5 | raw file
Possible License(s): CC-BY-SA-3.0, Apache-2.0, BSD-3-Clause, AGPL-3.0, MPL-2.0-no-copyleft-exception
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Data.Linq;
  5. using System.Web;
  6. using CmsData;
  7. using System.Web.Mvc;
  8. using System.Text;
  9. using UtilityExtensions;
  10. using System.Net;
  11. using System.Xml.Linq;
  12. namespace CmsWeb.Models
  13. {
  14. public class SGMapModel
  15. {
  16. public int divid { get; set; }
  17. public SGMapModel(int id)
  18. {
  19. divid = id;
  20. }
  21. public class SGInfo
  22. {
  23. public string desc { get; set; }
  24. public string addr { get; set; }
  25. public string name { get; set; }
  26. public DateTime? schedule { get; set; }
  27. public string cmshost { get; set; }
  28. public int id { get; set; }
  29. public GeoCode gc { get; set; }
  30. }
  31. public class MarkerInfo
  32. {
  33. public string title { get; set; }
  34. public string html { get; set; }
  35. public double latitude { get; set; }
  36. public double longitude { get; set; }
  37. }
  38. public IEnumerable<MarkerInfo> Locations()
  39. {
  40. var q = from o in DbUtil.Db.Organizations
  41. where o.Location != null && o.Location != ""
  42. where o.DivOrgs.Any(dd => dd.DivId == divid) || o.DivisionId == divid
  43. join gc in DbUtil.Db.GeoCodes on o.Location equals gc.Address into g
  44. from geocode in g.DefaultIfEmpty()
  45. select new SGInfo
  46. {
  47. desc = o.OrganizationName, //o.Description,
  48. addr = o.Location,
  49. name = o.OrganizationName,
  50. schedule = o.OrgSchedules.First().MeetingTime,
  51. cmshost = DbUtil.Db.CmsHost,
  52. id = o.OrganizationId,
  53. gc = geocode,
  54. };
  55. var qlist = q.ToList();
  56. var addlist = new List<GeoCode>();
  57. var ret = new List<MarkerInfo>();
  58. foreach (var i in qlist.Where(ii => ii.gc == null))
  59. {
  60. i.gc = addlist.SingleOrDefault(g => g.Address == i.addr);
  61. if (i.gc == null)
  62. {
  63. i.gc = GetGeocode(i.addr);
  64. addlist.Add(i.gc);
  65. }
  66. }
  67. if (addlist.Count > 0)
  68. DbUtil.Db.GeoCodes.InsertAllOnSubmit(addlist);
  69. DbUtil.Db.SubmitChanges();
  70. string template = @"
  71. <div>
  72. {0}<br />
  73. {1:ddd h:mm tt}<br />
  74. <a href='{2}OnlineReg/Index/{3}' target='_top'>Signup</a>
  75. </div>";
  76. return from i in qlist
  77. where i.gc.Latitude != 0
  78. select new MarkerInfo
  79. {
  80. html = template.Fmt(i.desc, i.schedule, i.cmshost, i.id),
  81. latitude = i.gc.Latitude,
  82. longitude = i.gc.Longitude,
  83. };
  84. }
  85. public StringBuilder sb = new StringBuilder();
  86. private GeoCode GetGeocode(string address)
  87. {
  88. var wc = new WebClient();
  89. var uaddress = HttpUtility.UrlEncode(address);
  90. var uri = new Uri("http://maps.googleapis.com/maps/api/geocode/xml?address={0}&sensor=false".Fmt(uaddress));
  91. var xml = wc.DownloadString(uri);
  92. var xdoc = XDocument.Parse(xml);
  93. var status = xdoc.Descendants("status").Single().Value;
  94. if (status == "ZERO_RESULTS")
  95. return new GeoCode { Address = address };
  96. try
  97. {
  98. var loc = xdoc.Document.Descendants("location");
  99. var lat = Convert.ToDouble(loc.Descendants("lat").First().Value);
  100. var lng = Convert.ToDouble(loc.Descendants("lng").First().Value);
  101. return new GeoCode
  102. {
  103. Address = address,
  104. Latitude = lat,
  105. Longitude = lng,
  106. };
  107. }
  108. catch (Exception ex)
  109. {
  110. sb.AppendLine(address);
  111. sb.AppendLine(status);
  112. sb.Append(ex.Message);
  113. return new GeoCode { Address = address };
  114. }
  115. }
  116. }
  117. }