Merge branch 'map'
[u/erikhf/frm.git] / src / components / map / map.ts
CommitLineData
a2d7d6b4
JHR
1import {Component, CORE_DIRECTIVES,} from 'angular2/angular2';
2import {Headers, Http} from 'angular2/http';
cb2c4ba8 3
1e6ce2f5 4
cb2c4ba8 5@Component({
28765058 6 selector: 'mou-map',
cb2c4ba8 7 directives: [CORE_DIRECTIVES],
28765058 8 templateUrl: './components/map/map.html'
cb2c4ba8
JHR
9})
10
11
28765058 12export class Map {
6ced1bc7 13
dd095993 14 map:Object;
dd095993 15 marker:Object;
6ced1bc7 16 http: Http;
dd095993
JHR
17
18 constructor(http:Http) {
6ced1bc7
JHR
19
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
JHR
30 let initMap = this.initMap;
31 let addMarker = this.addMarker;
32 let map = this.map;
dd095993
JHR
33 if (navigator.geolocation) {
34 navigator.geolocation.getCurrentPosition(function (position) {
6ced1bc7
JHR
35 let pos = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
36 initMap(pos,map,addMarker);
dd095993 37 }, function () {
bbee9db0 38 //handleNoGeoLocation()
dd095993
JHR
39 }
40 );
41 } else {
42 alert("You do not support geolocation");
43 }
a2d7d6b4 44
cdcaf46c 45
a2d7d6b4
JHR
46 }
47
dd095993 48
6ced1bc7
JHR
49 initMap(location,map,addMarker){
50
6ced1bc7 51 map.setCenter(location,12);
6ced1bc7
JHR
52 addMarker(location,map,'This is YOU');
53
54
dd095993 55
6ced1bc7
JHR
56 map.addListener('click', function (event) {
57 addMarker(event.latLng,map, 'Want to add a new marker here ? <br> <button onclick=\"createOrgUnit()\">Yes</button> <button onclick=\"deleteMarker()">No</button> ');
58 }
59 );
60
61 }
62
63 addMarker(location, map, title) {
64
65 let marker = new google.maps.Marker({
66 position: location,
67 map: map
68 });
69
70 let infowindow = new google.maps.InfoWindow({
71 content: title
72 });
73
74 marker.addListener('click', function () {
75 console.log(marker);
76 infowindow.open(map,marker);
77 });
78
6ced1bc7 79 }
dd095993 80
a2d7d6b4
JHR
81 logError(error) {
82 console.error(error);
83
cb2c4ba8 84 }
28765058 85
c7e8b786
EHF
86 getData(query,instance){
87 console.log(instance.http);
88 instance.http.get(dhisAPI+'/api/organisationUnits'+query)
6ced1bc7
JHR
89 .map(res => res.json())
90 .subscribe(
c7e8b786
EHF
91 res => instance.parseResult(res),
92 error => instance.logError(error)
6ced1bc7
JHR
93 );
94
95 }
96
97 parseResult(res){
98
99 if(res.organisationUnits) {
100 for (let item in res.organisationUnits) {
c7e8b786 101 this.getData('/' + res.organisationUnits[item].id,this);
6ced1bc7 102 }
0a4273ac
JHR
103 //liten hack
104 }//else if(res.name != false){
105 // for (let item in res.children) {
106 // this.getData('/' + res.children[item].id,this);
107 //}
108 //}
109 else {
6ced1bc7
JHR
110
111 this.drawPolygon(res);};
112 }
113 drawPolygon(item){
c7e8b786 114 let instance = this;
1f8c27ee
JHR
115 let feature;
116 let incoming: string;
117 incoming = item.featureType.toLowerCase();
118 switch(incoming){
119 case "point":
120 feature = 'Point';
121 break;
122 case "multi_polygon":
123 feature = 'MultiPolygon';
124 break;
125 case "polygon":
126 feature = 'MultiPolygon';
127 break;
128 default:
129 }
130 // TODO: test på feature og behandle type: NONE
131 if(feature !== undefined) {
132 let unit = {
133 "type": "Feature",
134 "geometry": {
135 "type": feature,
136 "coordinates": JSON.parse(item.coordinates)
137 },
138 "properties": {
139 "name": item.name,
140 "id": item.id
141 }
142 };
143 this.map.data.addGeoJson(unit);
c7e8b786 144
1f8c27ee 145 this.map.data.addListener('click', function(event) {
bbee9db0 146 //TODO: spør om man vil ned/opp eller se info
1f8c27ee 147
bbee9db0
JHR
148 let id = event.feature.O.id;
149 console.log(id);
150
0a4273ac
JHR
151 instance.map.data.forEach(function(feature) {
152 instance.map.data.remove(feature);
153 });
154 // instance.getData('/' + id+'/children',instance);
c7e8b786 155 instance.getData('/' + id,instance);
6ced1bc7 156
bbee9db0 157 });
0a4273ac 158
bbee9db0 159
1f8c27ee
JHR
160 }else {
161 // ToDO:
162 console.log("fiks meg! gi warning på topp av kart");
163 }
6ced1bc7 164
6ced1bc7
JHR
165
166 }
167
dd095993 168
6ced1bc7
JHR
169 createOrgUnit(){
170 console.log('you just added a new organisation unit');
171 }
dd095993 172
6ced1bc7
JHR
173 deleteMarker(){
174 console.log('you just deleted the marker');
175 }
6ced1bc7
JHR
176
177
178}
dd095993 179
dd095993
JHR
180
181
182
183