1 import {Component, EventEmitter,CORE_DIRECTIVES,} from 'angular2/angular2';
2 import {Headers, Http} from 'angular2/http';
6 directives: [CORE_DIRECTIVES],
7 events: ['newactive', 'neworg'],
8 templateUrl: './components/map/map.html'
29 constructor(http:Http) {
32 this.newactive = new EventEmitter();
33 this.neworg = new EventEmitter();
34 this.map = new google.maps.Map(document.getElementById("map"), {
35 center: {lat: 0, lng: 0},
37 mapTypeControlOptions: {
38 position: google.maps.ControlPosition.BOTTOM_CENTER
41 position: google.maps.ControlPosition.LEFT_BOTTOM
43 streetViewControl: false
51 this.currentPos = null;
52 this.uprunned = false;
53 this.currentMarker = null;
54 this.isSearched = false;
55 // this.COLORS = {'red','brown',',yellow','green',',pink','purple','gray','black'};
56 this.hideModal = document.getElementById("topLevel").style.visibility = "hidden";
57 this.hideModal = document.getElementById("middleLevel").style.visibility = "hidden";
58 this.hideModal = document.getElementById("bottomLevel").style.visibility = "hidden";
59 this.hideModal = document.getElementById("divModal").style.display = "none";
74 setcurrentPos(latlng) {
75 this.currentPos = latlng;
79 return this.currentPos;
95 this.uprunned = value;
113 let pos = {lat: 9.1, lng: -11.6};
114 map.setCenter(pos, 0);
120 console.error(error);
124 getData(query, instance, isParent) {
125 instance.http.get(dhisAPI + '/api/organisationUnits' + query)
126 .map(res => res.json())
128 res => instance.parseResult(res, instance, isParent),
129 error => instance.logError(error)
134 this.http.get(dhisAPI + '/api/organisationUnitLevels')
135 .map(res => res.json())
137 res => this.saveLevelTotalandGetdata(res, this),
138 err => this.logError(err)
142 saveLevelTotalandGetdata(res, instance) {
143 instance.allLevels = res.pager.total;
144 instance.getData('?paging=false&level=2', instance, false);
147 parseResult(res, instance, isParent) {
149 instance.setParent(res.parent.id);
150 instance.getData('/' + res.parent.id + '/children', instance, false);
153 if (res.organisationUnits) {
154 for (let item in res.organisationUnits) {
155 this.getData('/' + res.organisationUnits[item].id, this);
158 instance.setupRunned(false);
159 instance.setRunned(false);
160 } else if (!res.displayName && res.children) {
161 for (let item in res.children) {
162 if (res.children[item].level == instance.LEVEL) {
163 this.getData('/' + res.children[item].id, this);
166 instance.setRunned(false);
167 instance.setupRunned(false);
170 this.drawPolygon(res, instance);
175 drawPolygon(item, instance) {
178 incoming = item.featureType.toLowerCase();
183 case "multi_polygon":
184 feature = 'MultiPolygon';
187 feature = 'MultiPolygon';
192 if (feature !== undefined) {
197 "coordinates": JSON.parse(item.coordinates)
207 if (unit.geometry.type == 'Point') {
208 unit.properties.icon = {
209 path: google.maps.SymbolPath.CIRCLE,
210 strokeColor: 'black',
213 instance.map.panTo({lat:unit.geometry.coordinates[1],lng:unit.geometry.coordinates[0]});
216 this.map.data.addGeoJson(unit);
217 this.map.data.setStyle(function (feature) {
220 if (feature.getProperty('icon') !== null) {
221 icon = feature.getProperty('icon');
223 color = feature.getProperty('color');
224 return /** @type {google.maps.Data.StyleOptions} */({
231 if(instance.isSearched){
232 instance.seeDetails();
234 this.map.data.addListener('click', function (event) {
235 instance.setActiveId(event.feature.O.id);
236 instance.setcurrentPos(event.latLng);
238 if (instance.uprunned == false && instance.LEVEL == 2) {
239 this.hideModal = document.getElementById("topLevel").style.visibility = "visible";
240 this.hideModal = document.getElementById("middleLevel").style.visibility = "hidden";
241 this.hideModal = document.getElementById("bottomLevel").style.visibility = "hidden";
242 instance.showModal();
245 else if (instance.runned == false && instance.LEVEL < instance.allLevels) {
246 this.hideModal = document.getElementById("topLevel").style.visibility = "hidden";
247 this.hideModal = document.getElementById("middleLevel").style.visibility = "visible";
248 this.hideModal = document.getElementById("bottomLevel").style.visibility = "hidden";
249 instance.showModal();
250 } else if (instance.runned == false && instance.LEVEL <= instance.allLevels) {
252 this.hideModal = document.getElementById("topLevel").style.visibility = "hidden";
253 this.hideModal = document.getElementById("middleLevel").style.visibility = "hidden";
254 this.hideModal = document.getElementById("bottomLevel").style.visibility = "visible";
256 instance.setcurrentPos(event.latLng);
257 instance.showModal();
263 /* getpolygonCenter(coordinates,instance){
264 let bounds = new google.maps.LatLngBounds();
265 let polygonCoords = Array;
266 for(let i = 0; i < coordinates.length; i++){
267 for(let j = 0; j<coordinates[i].length; j++) {
268 for (let k = 0; k<coordinates[i][j].length; k++) {
269 polygonCoords[i] = new google.maps.LatLng(coordinates[i][j][k][0],coordinates[i][j][k][1]);
274 for (let i = 0; i < polygonCoords.length; i++) {
275 bounds.extend(polygonCoords[i]);
278 instance.map.panTo(bounds.getCenter());
283 let map = this.getMap();
284 let id = this.activeId;
285 let level = this.LEVEL;
286 this.setRunned(true);
289 map.data.forEach(function (feature) {
290 if (!(feature.O.id == id && level == 3)) {
291 map.data.remove(feature);
297 this.getData('/' + id + '/children', this);
304 this.setupRunned(true);
308 this.map.data.forEach(function (feature) {
309 instance.map.data.remove(feature);
312 if (this.currentMarker !== null) {
313 this.currentMarker.setMap(null);
315 let parent = instance.getParent();
316 instance.getData('/' + parent, instance, true);
322 let map = this.getMap();
323 let id = this.activeId;
325 map.data.forEach(function (feature) {
326 if (feature.getProperty('id') == id) {
327 feature.setProperty('color', 'red');
328 if (feature.getProperty('icon') !== null) {
329 feature.O.icon.strokeColor = 'red';
331 this.isSearched=false;
334 feature.setProperty('color', 'gray');
335 if (feature.getProperty('icon') !== null) {
336 feature.O.icon.strokeColor = 'black';
340 this.newactive.next(this.activeId);
345 let pos = this.getcurrentPos();
348 let parent = this.getParent();
350 let location = {lat: lat, lng: lng};
351 let event = {location, parent};
352 this.neworg.next(event);
354 this.setRunned(false);
358 this.newactive.next(event);
359 let map = this.getMap();
360 let http = this.getHttp();
362 map.data.forEach(function (feature) {
363 map.data.remove(feature);
365 http.get(dhisAPI + '/api/organisationUnits/' + event)
366 .map(res => res.json())
368 res=> this.mapUpdate(res, this)
373 mapUpdate(res, instance) {
374 this.setLevel(res.level);
375 this.setActiveId(res.id);
376 this.isSearched = true;
377 this.setParent(res.parent.id);
379 instance.getData('/' + res.parent.id + '/children', instance);
380 if (res.coordinates == null || instance.LEVEL == instance.allLevels) {
381 instance.http.get(dhisAPI + '/api/organisationUnits/' + res.parent.id)
382 .map(res => res.json())
384 res => instance.drawPolygon(res, instance)
392 if (this.currentMarker)
393 this.currentMarker.setMap(null);
395 this.currentMarker = new google.maps.Marker({
400 path: google.maps.SymbolPath.CIRCLE,
404 this.currentMarker.setMap(map);
405 map.panTo(this.currentMarker.getPosition());
406 //console.log(this.currentMarker.getPosition());
410 return this.hideModal = document.getElementById("divModal").style.display = "block";
414 this.hideModal = document.getElementById("topLevel").style.visibility = "hidden";
415 this.hideModal = document.getElementById("middleLevel").style.visibility = "hidden";
416 this.hideModal = document.getElementById("bottomLevel").style.visibility = "hidden";
417 this.hideModal = document.getElementById("divModal").style.display = "none";
419 this.setRunned(false);