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