Advertisement
dawmaj

Untitled

Jan 23rd, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.59 KB | None | 0 0
  1. import { Component, OnInit } from '@angular/core';
  2. import { CommonModule } from "@angular/common";
  3. import { AuthorizationService } from './../authorization.service';
  4. import OlMap from 'ol/Map';
  5. import Overlay from 'ol/Overlay';
  6. import OlVectorSource from 'ol/source/Vector';
  7. import OlVectorLayer from 'ol/layer/Vector';
  8. import OlView from 'ol/View';
  9. import OlFeature from 'ol/Feature';
  10. import OlPoint from 'ol/geom/Point';
  11. import OlXyzSource from 'ol/source/XYZ';
  12. import OlTileLayer from 'ol/layer/Tile';
  13. import LineString from 'ol/geom/LineString';
  14. import Style from 'ol/style/Style';
  15. import Stroke from 'ol/style/Stroke';
  16. import Fill from 'ol/style/Fill';
  17. import Icon from 'ol/style/Icon';
  18. import { fromLonLat, toLonLat } from 'ol/proj';
  19.  
  20.  
  21. @Component({
  22. selector: 'app-map',
  23. templateUrl: './map.component.html',
  24. styleUrls: ['./map.component.css']
  25. })
  26. export class MapComponent implements OnInit {
  27.  
  28.  
  29. constructor(protected Auth: AuthorizationService) { }
  30.  
  31. map: OlMap;
  32. vectorSource: OlVectorSource;
  33. vectorLine: OlVectorSource;
  34. vectorLayer: OlVectorLayer;
  35. xyzSource: OlXyzSource;
  36. tileLayer: OlTileLayer;
  37. view: OlView;
  38. overlay = Overlay;
  39. latitude: number = 52.4082663;
  40. longitude: number = 16.9335199;
  41. lessonsId: Array<string> = [];
  42. mapNotDefined = false;
  43. mapRendered = false;
  44. previousMap: string = "";
  45. closer: any;
  46. container: any;
  47. content: any;
  48.  
  49. ngOnInit() {
  50. this.closer = document.getElementById('popup-closer');
  51. this.content = document.getElementById('popup-content');
  52. this.container = document.getElementById('popup');
  53. this.Auth.getSchool(localStorage.getItem('userToken')).subscribe(dat => {
  54. this.Auth.getLessons(localStorage.getItem('userToken'), dat.id).subscribe(da => {
  55. for (var i = 0; i < da.length; i++)
  56. {
  57. this.lessonsId.push(da[i].id);
  58. //console.log(this.lessonsId[i]);
  59. }
  60. this.getMap(this.lessonsId[this.lessonsId.length - 1]);
  61. })
  62.  
  63. });
  64.  
  65. }
  66.  
  67. getMap(id)
  68. {
  69. setTimeout(() => this.mapRendered = true, 10);
  70. setTimeout(() => this.mapRendered = false, 10);
  71. this.vectorSource = new OlVectorSource({});
  72. this.vectorLine = new OlVectorSource({});
  73. //console.log(id + " " +this.previousMap);
  74. var contents = this.content;
  75. //var container = document.getElementById('popup');
  76. this.Auth.getMap(id).subscribe(map => {
  77.  
  78. console.log(this.closer);
  79. var overlay = new Overlay({
  80. element: this.container,
  81. autoPan: true,
  82. autoPanAnimation: {
  83. duration: 250
  84. }
  85. });
  86.  
  87. this.closer.onclick = function() {
  88. overlay.setPosition(undefined);
  89. this.closer.blur();
  90. return false;
  91. };
  92.  
  93. this.previousMap = id.toString();
  94.  
  95. var places = [];
  96. var coords = [];
  97.  
  98. for (var i = 0; i < map.mapMarkers.length; i++)
  99. {
  100. places.push([map.mapMarkers[i].coordinates.longitude, map.mapMarkers[i].coordinates.latitude, map.mapMarkers[i].title]);
  101.  
  102. }
  103. //console.log(places);
  104.  
  105. var points = [];
  106.  
  107. for (var i = 0; i < map.mapLines.length; i++)
  108. {
  109. points.push([map.mapLines[i].longitude, map.mapLines[i].latitude]);
  110. }
  111.  
  112. for (var i = 0; i < places.length; i++) {
  113. //console.log(places[i][0], places[i][1],places[i][2]);
  114.  
  115. var iconFeature = new OlFeature({
  116. description: places[i][2],
  117. geometry: new OlPoint(fromLonLat([places[i][0], places[i][1]])),
  118. });
  119.  
  120. var iconStyle= new Style({
  121. image: new Icon({
  122. crossOrigin: 'anonymous',
  123. src: '../assets/images/marker2.png'
  124. })
  125. })
  126.  
  127. iconFeature.setStyle(iconStyle);
  128. this.vectorSource.addFeature(iconFeature);
  129. }
  130.  
  131. for (var i = 0; i < points.length; i++) {
  132. points[i] = fromLonLat(points[i]);
  133. //console.log("P" + points[i]);
  134. }
  135. var featureLine = new OlFeature({
  136. geometry: new LineString(points)
  137. });
  138. this.vectorLine.addFeature(featureLine);
  139.  
  140.  
  141. var vectorLineLayer = new OlVectorLayer({
  142. source: this.vectorLine,
  143. style: new Style({
  144. fill: new Fill({ color: '#000000', weight: 2 }),
  145. stroke: new Stroke({ color: '#000000', width: 2 })
  146. })
  147. });
  148. this.view = new OlView({
  149. center: [points[0][0], points[0][1]],
  150. zoom: 15
  151. });
  152.  
  153.  
  154. this.vectorLayer = new OlVectorLayer({
  155. source: this.vectorSource
  156. });
  157.  
  158. /* XYZ */
  159.  
  160. this.xyzSource = new OlXyzSource({
  161. url: 'http://tile.osm.org/{z}/{x}/{y}.png'
  162. });
  163.  
  164. this.tileLayer = new OlTileLayer({
  165. source: this.xyzSource
  166. });
  167. console.log(vectorLineLayer);
  168.  
  169. /* View and map */
  170. this.map = new OlMap({
  171. target: 'map',
  172. // Added both layers
  173. overlays: [overlay],
  174. layers: [this.tileLayer, vectorLineLayer, this.vectorLayer],
  175. view: this.view
  176. });
  177.  
  178. this.map.on('singleclick', function(evt) {
  179. var coordinate = evt.coordinate;
  180. var hdms = toLonLat(coordinate);
  181. for (var i = 0; i < places.length; i++) {
  182. var coord1 = hdms[0].toFixed(3);
  183. var coord2 = hdms[1].toFixed(3);
  184. var pl1 = places [i][0].toFixed(3);
  185. var pl2 = places [i][1].toFixed(3);
  186. //console.log(coord1 + " " + coord2);
  187. //console.log(pl1 + " " + pl2);
  188. if ((coord1 == pl1) && (coord2 == pl2))
  189. {
  190. contents.innerHTML = '<p>Twój błąd:</p><code>' + places[i][2] +
  191. '</code>';
  192. overlay.setPosition(coordinate);
  193. break;
  194. }
  195. }
  196. });
  197.  
  198. this.mapNotDefined = false;
  199. this.mapRendered = false;
  200. console.log(this.mapRendered + " " + this.mapNotDefined)
  201. }, error => {
  202. if (error.status == 404)
  203. {
  204. this.mapNotDefined = true;
  205. this.mapRendered = false;
  206. this.previousMap = id.toString();
  207. //console.log(this.mapRendered + " " + this.mapNotDefined)
  208. setTimeout(() => this.mapRendered = true, 500);
  209. setTimeout(() => this.mapRendered = false, 500);
  210. }
  211. });
  212. }
  213. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement