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