]>
Commit | Line | Data |
---|---|---|
7ee898bc | 1 | import {Component, EventEmitter,CORE_DIRECTIVES,} from 'angular2/angular2'; |
a2d7d6b4 | 2 | import {Headers, Http} from 'angular2/http'; |
3562820e | 3 | import {Search} from "../search/search"; |
1e6ce2f5 | 4 | |
cb2c4ba8 | 5 | @Component({ |
28765058 | 6 | selector: 'mou-map', |
3562820e RM |
7 | directives: [CORE_DIRECTIVES, Search], |
8 | events: ['newactive', 'outevent'], | |
28765058 | 9 | templateUrl: './components/map/map.html' |
cb2c4ba8 JHR |
10 | }) |
11 | ||
12 | ||
28765058 | 13 | export class Map { |
6ced1bc7 | 14 | |
dd095993 | 15 | map:Object; |
6ced1bc7 | 16 | http: Http; |
dd095993 JHR |
17 | |
18 | constructor(http:Http) { | |
3562820e RM |
19 | this.outevent = new EventEmitter(); |
20 | ||
7ee898bc | 21 | this.newactive = new EventEmitter(); |
6ced1bc7 JHR |
22 | this.map = new google.maps.Map(document.getElementById("map"),{center: {lat:0,lng:0}, zoom:12}); |
23 | this.init(); | |
24 | this.http = http; | |
25 | ||
c7e8b786 | 26 | this.getData('?paging=false&level=2',this); |
1e6ce2f5 EHF |
27 | } |
28 | ||
3562820e RM |
29 | inevent(e){ |
30 | this.outevent.next(e); | |
31 | } | |
32 | ||
a2d7d6b4 | 33 | |
6ced1bc7 | 34 | init() { |
a2d7d6b4 | 35 | |
6ced1bc7 | 36 | let initMap = this.initMap; |
6ced1bc7 | 37 | let map = this.map; |
dd095993 JHR |
38 | if (navigator.geolocation) { |
39 | navigator.geolocation.getCurrentPosition(function (position) { | |
6ced1bc7 | 40 | let pos = new google.maps.LatLng(position.coords.latitude, position.coords.longitude); |
30593bb6 | 41 | initMap(pos,map); |
dd095993 | 42 | }, function () { |
bbee9db0 | 43 | //handleNoGeoLocation() |
dd095993 JHR |
44 | } |
45 | ); | |
46 | } else { | |
47 | alert("You do not support geolocation"); | |
48 | } | |
a2d7d6b4 | 49 | |
cdcaf46c | 50 | |
a2d7d6b4 JHR |
51 | } |
52 | ||
dd095993 | 53 | |
30593bb6 | 54 | initMap(location,map){ |
6ced1bc7 | 55 | |
6ced1bc7 | 56 | map.setCenter(location,12); |
6ced1bc7 JHR |
57 | |
58 | ||
dd095993 | 59 | |
6ced1bc7 | 60 | map.addListener('click', function (event) { |
30593bb6 | 61 | console.log("jule husker"); |
6ced1bc7 JHR |
62 | } |
63 | ); | |
64 | ||
65 | } | |
66 | ||
a2d7d6b4 JHR |
67 | logError(error) { |
68 | console.error(error); | |
69 | ||
cb2c4ba8 | 70 | } |
28765058 | 71 | |
c7e8b786 EHF |
72 | getData(query,instance){ |
73 | console.log(instance.http); | |
74 | instance.http.get(dhisAPI+'/api/organisationUnits'+query) | |
6ced1bc7 JHR |
75 | .map(res => res.json()) |
76 | .subscribe( | |
c7e8b786 EHF |
77 | res => instance.parseResult(res), |
78 | error => instance.logError(error) | |
6ced1bc7 JHR |
79 | ); |
80 | ||
81 | } | |
82 | ||
83 | parseResult(res){ | |
84 | ||
85 | if(res.organisationUnits) { | |
86 | for (let item in res.organisationUnits) { | |
c7e8b786 | 87 | this.getData('/' + res.organisationUnits[item].id,this); |
6ced1bc7 | 88 | } |
0a4273ac JHR |
89 | //liten hack |
90 | }//else if(res.name != false){ | |
91 | // for (let item in res.children) { | |
92 | // this.getData('/' + res.children[item].id,this); | |
93 | //} | |
94 | //} | |
95 | else { | |
6ced1bc7 JHR |
96 | |
97 | this.drawPolygon(res);}; | |
98 | } | |
99 | drawPolygon(item){ | |
c7e8b786 | 100 | let instance = this; |
1f8c27ee JHR |
101 | let feature; |
102 | let incoming: string; | |
103 | incoming = item.featureType.toLowerCase(); | |
104 | switch(incoming){ | |
105 | case "point": | |
106 | feature = 'Point'; | |
107 | break; | |
108 | case "multi_polygon": | |
109 | feature = 'MultiPolygon'; | |
110 | break; | |
111 | case "polygon": | |
112 | feature = 'MultiPolygon'; | |
113 | break; | |
114 | default: | |
115 | } | |
116 | // TODO: test på feature og behandle type: NONE | |
117 | if(feature !== undefined) { | |
118 | let unit = { | |
119 | "type": "Feature", | |
120 | "geometry": { | |
121 | "type": feature, | |
122 | "coordinates": JSON.parse(item.coordinates) | |
123 | }, | |
124 | "properties": { | |
125 | "name": item.name, | |
126 | "id": item.id | |
127 | } | |
128 | }; | |
129 | this.map.data.addGeoJson(unit); | |
c7e8b786 | 130 | |
1f8c27ee | 131 | this.map.data.addListener('click', function(event) { |
bbee9db0 | 132 | //TODO: spør om man vil ned/opp eller se info |
1f8c27ee | 133 | |
bbee9db0 JHR |
134 | let id = event.feature.O.id; |
135 | console.log(id); | |
136 | ||
0a4273ac JHR |
137 | instance.map.data.forEach(function(feature) { |
138 | instance.map.data.remove(feature); | |
139 | }); | |
140 | // instance.getData('/' + id+'/children',instance); | |
c7e8b786 | 141 | instance.getData('/' + id,instance); |
6ced1bc7 | 142 | |
bbee9db0 | 143 | }); |
0a4273ac | 144 | |
bbee9db0 | 145 | |
1f8c27ee JHR |
146 | }else { |
147 | // ToDO: | |
148 | console.log("fiks meg! gi warning på topp av kart"); | |
149 | } | |
6ced1bc7 | 150 | |
6ced1bc7 JHR |
151 | |
152 | } | |
153 | ||
dd095993 | 154 | |
6ced1bc7 JHR |
155 | createOrgUnit(){ |
156 | console.log('you just added a new organisation unit'); | |
157 | } | |
dd095993 | 158 | |
7ee898bc EHF |
159 | update(event){ |
160 | this.newactive.next(event); | |
161 | } | |
6ced1bc7 | 162 | } |
dd095993 | 163 | |
dd095993 JHR |
164 | |
165 | ||
166 | ||
167 |