Merge branch 'sidebar'
[u/erikhf/frm.git] / src / components / map / map.ts
CommitLineData
7ee898bc 1import {Component, EventEmitter,CORE_DIRECTIVES,} from 'angular2/angular2';
a2d7d6b4 2import {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 13export 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
JHR
90
91 this.drawPolygon(res);};
92 }
93 drawPolygon(item){
c7e8b786 94 let instance = this;
1f8c27ee
JHR
95 let feature;
96 let incoming: string;
97 incoming = item.featureType.toLowerCase();
98 switch(incoming){
99 case "point":
100 feature = 'Point';
101 break;
102 case "multi_polygon":
103 feature = 'MultiPolygon';
104 break;
105 case "polygon":
106 feature = 'MultiPolygon';
107 break;
108 default:
109 }
110 // TODO: test på feature og behandle type: NONE
111 if(feature !== undefined) {
112 let unit = {
113 "type": "Feature",
114 "geometry": {
115 "type": feature,
116 "coordinates": JSON.parse(item.coordinates)
117 },
118 "properties": {
119 "name": item.name,
120 "id": item.id
121 }
122 };
123 this.map.data.addGeoJson(unit);
c7e8b786 124
1f8c27ee 125 this.map.data.addListener('click', function(event) {
bbee9db0 126 //TODO: spør om man vil ned/opp eller se info
1f8c27ee 127
bbee9db0
JHR
128 let id = event.feature.O.id;
129 console.log(id);
130
0a4273ac
JHR
131 instance.map.data.forEach(function(feature) {
132 instance.map.data.remove(feature);
133 });
134 // instance.getData('/' + id+'/children',instance);
c7e8b786 135 instance.getData('/' + id,instance);
6ced1bc7 136
bbee9db0 137 });
0a4273ac 138
bbee9db0 139
1f8c27ee
JHR
140 }else {
141 // ToDO:
142 console.log("fiks meg! gi warning på topp av kart");
143 }
6ced1bc7 144
6ced1bc7
JHR
145
146 }
147
dd095993 148
6ced1bc7
JHR
149 createOrgUnit(){
150 console.log('you just added a new organisation unit');
151 }
dd095993 152
7ee898bc
EHF
153 update(event){
154 this.newactive.next(event);
155 }
6ced1bc7 156}
dd095993 157
dd095993
JHR
158
159
160
161