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