Advertisement
Guest User

Untitled

a guest
Jul 12th, 2017
700
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.49 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data.SqlClient;
  4. using System.Globalization;
  5. using System.IO;
  6. using System.Text;
  7. using System.Threading;
  8. using System.Windows.Forms;
  9.  
  10. namespace GeoDisplay
  11. {
  12. public partial class Form1 : Form
  13. {
  14. public StringBuilder tekst; //StringBuilder zawierający kod strony HTML do wyświetlenia
  15.  
  16. string tempTable; //nazwa tymczasowej tabeli na ID
  17.  
  18. public Form1(string[] args)
  19. {
  20. WebBrowserHelper.FixBrowserVersion(); //wymuszenie, aby przeglądarka korzystała z IE 9.0+
  21. InitializeComponent();
  22. webBrowser1.ScriptErrorsSuppressed = true; //wyłączenie obsługi błędów w przeglądarce
  23. Thread.CurrentThread.CurrentCulture = CultureInfo.GetCultureInfo("en-US"); //wymuszenie korzystania z kropek zamiast przecinków przy zmiennych rzeczywistych
  24. string server; //nazwa serwera
  25. string database; //nazwa bazy danych
  26. string username; //nazwa użytkownika
  27. string pwd; //hasło
  28. if (args.Length < 6) //przypisanie wartości domyślnych
  29. {
  30. server = "F-HV-64\\SQL2008R2";
  31. database = "KOSZALIN_17_01_2017";
  32. username = "sa";
  33. pwd = "haslo";
  34. tempTable = "t_dane_geolok_pozycje";
  35. }
  36. else
  37. {
  38. server = args[1];
  39. database = args[2];
  40. username = args[3];
  41. pwd = args[4];
  42. tempTable = args[5];
  43. }
  44. // utworzenie połączenia z bazą danych
  45. SqlConnection conn = new SqlConnection("user id=" + username + ";password="+pwd+";server="+server+";Trusted_Connection=no;database="+database+";Connect Timeout=10");
  46. var adresy = GetDatabase(conn); //pobranie potrzebnej zawartości
  47. //wpisanie pierwszej połowy strony
  48. tekst = new StringBuilder("<html><head><style>#map {height: 100 %;} html, body {font-family: Arial, Helvetica, sans-serif;height: 100%;margin: 0;padding: 0;} .content {color: #303030;text-align: center;}</style></head><body><div class=\"content\"><h1 id = \"lattitude\"></h1></div><div id=\"map\"></div><script>function initMap(){var icons =[];icons[0] = 'http://icons.iconarchive.com/icons/robinweatherall/recycling/16/bin-small-icon.png';icons[1] = 'http://iconshow.me/media/images/Application/Map-Markers-icons/png/32/MapMarker_Flag4_Right_Chartreuse.png';icons[2] = 'http://icons.veryicon.com/32/System/Eicodesign/Recycle%20bin%20green.png';icons[3] = 'http://icons.veryicon.com/32/System/Just%20Bins/bin%20white%20full.png';markers=[];");
  49.  
  50. foreach (var adres in adresy) //Wpisanie markerów
  51. {
  52. tekst.Append("var marker = new google.maps.Marker({position: new google.maps.LatLng(");
  53. tekst.Append(adres["X"]);
  54. tekst.Append(",");
  55. tekst.Append(adres["Y"]);
  56. tekst.Append("),icon: icons[");
  57. tekst.Append(adres["icon"]);
  58. tekst.Append("],map: map,visible: true,title: '"+adres["adres"]+"'});markers.push(marker);google.maps.event.addListener(marker, 'click', function() {new google.maps.InfoWindow({content: '<div id=\"content\"><h2 id=\"firstHeading\" class=\"firstHeading\">");
  59. tekst.Append(adres["adres"]);
  60. tekst.Append("</h2></div>'}).open(map,this);}); ");
  61. }
  62. var dir = Directory.GetCurrentDirectory(); //pobranie aktualnej ścieżki
  63. //dopisanie reszty strony
  64. tekst.Append("var map = new google.maps.Map(document.getElementById('map'), {zoom: (markers.length>0?12:7),center: (markers.length>0?markers[0].position:{lat:52.5,lng:19.5}),mapTypeId: google.maps.MapTypeId.ROADMAP});map.setOptions({styles: [{featureType: \"poi\",elementType: \"labels\",stylers: [{ visibility: \"off\" }]}]});var clusterStyles = [{textColor: 'black',url: 'https://image.ibb.co/cy8LHv/m1.png',height: 30,width: 30},{textColor: 'white',url: 'https://image.ibb.co/mh3WPa/m2.png',height: 30,width: 30},{textColor: 'white',url: 'https://image.ibb.co/eve5ja/m3.png',height: 30,width: 30}];var mcOptions = {gridSize: 50,styles: clusterStyles,maxZoom: 15,minimumClusterSize: 5};var markerCluster = new MarkerClusterer(map, markers, mcOptions, {imagePath: 'm'});}</script><script src=\"file:///" + dir+"\\markerclusterer.js\"></script><script type=\"text/javascript\" src=\"http://google-maps-utility-library-v3.googlecode.com/svn/trunk/infobox/src/infobox.js\"></script><script src=\"https://maps.googleapis.com/maps/api/js?key=AIzaSyB8YetP-YQHuW1P6wQKem7fX4UXLgkv5PY&callback=initMap\" async defer></script></body></html>");
  65.  
  66. //wyświetlanie strony
  67. DisplayHtml(tekst.ToString());
  68. }
  69.  
  70. /// <summary>
  71. /// funkcja pobiera potrzebne dane z bazy
  72. /// </summary>
  73. /// <param name="conn">Obiekt połącz</param>
  74. /// <returns>Lista wierszy</returns>
  75. private List<Dictionary<string, string>> GetDatabase(SqlConnection conn)
  76. {
  77. List<Dictionary<string, string>> Addresses = new List<Dictionary<string, string>>();
  78. try {
  79. conn.Open();
  80. SqlCommand myCommand = new SqlCommand("SELECT t_d.cit_nazwa,t_d.str_nazwa,t_d.nr_domu,t_d.latitude,t_d.longitude,t_d.icon from t_dane_geolok as t_d INNER JOIN (SELECT MAX(CAST(ROWID as varchar(MAX))) as ROWID, latitude,longitude from t_dane_geolok GROUP BY latitude,longitude) tbl ON tbl.ROWID=t_d.ROWID INNER JOIN " + tempTable + " ON " + tempTable + ".id_$=t_d.id_$ WHERE t_d.latitude IS NOT NULL;", conn);
  81. var mr = myCommand.ExecuteReader();
  82. if (mr.HasRows)
  83. while (mr.Read())
  84. {
  85. var adres = CultureInfo.CurrentCulture.TextInfo.ToTitleCase((mr["cit_nazwa"] + ", " + mr["str_nazwa"] + " " + mr["nr_domu"]).ToLower());
  86. var dict = new Dictionary<string, string>();
  87. dict.Add("adres", adres);
  88. dict.Add("X", mr["latitude"].ToString());
  89. dict.Add("Y", mr["longitude"].ToString());
  90. dict.Add("icon", mr["icon"].ToString());
  91. Addresses.Add(dict);
  92. }
  93. mr.Close();
  94. myCommand = new SqlCommand("DROP TABLE " + tempTable + ";", conn);
  95. myCommand.ExecuteNonQuery();
  96. } catch (Exception e)
  97. {
  98. MessageBox.Show("Wystąpił błąd: " + e.Message);
  99. }
  100. finally
  101. {
  102. conn.Close();
  103. }
  104. return Addresses;
  105. }
  106.  
  107. /// <summary>
  108. /// Wyswietla kod HTML w przeglądarce
  109. /// </summary>
  110. /// <param name="html">Kod HTML</param>
  111. private void DisplayHtml(string html)
  112. {
  113. webBrowser1.Navigate("about:blank");
  114. if (webBrowser1.Document != null)
  115. {
  116. webBrowser1.Document.Write(string.Empty);
  117. }
  118. webBrowser1.DocumentText=html;
  119. }
  120.  
  121. /// <summary>
  122. /// Wyłączono nawigację na stronie
  123. /// </summary>
  124. private void webBrowser1_Navigating(object sender, WebBrowserNavigatingEventArgs e)
  125. {
  126. if (e.Url.ToString() != "about:blank")
  127. e.Cancel = true;
  128. }
  129. }
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement