[maps] removed markers
[u/erikhf/frm.git] / src / components / map / map.ts
... / ...
CommitLineData
1import {Component, EventEmitter,CORE_DIRECTIVES,} from 'angular2/angular2';
2import {Headers, Http} from 'angular2/http';
3
4
5@Component({
6 selector: 'mou-map',
7 directives: [CORE_DIRECTIVES],
8 events: ['newactive'],
9 templateUrl: './components/map/map.html'
10})
11
12
13export class Map {
14
15 map:Object;
16 http: Http;
17
18 constructor(http:Http) {
19 this.newactive = new EventEmitter();
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
24 this.getData('?paging=false&level=2',this);
25 }
26
27
28 init() {
29
30 let initMap = this.initMap;
31 let map = this.map;
32 if (navigator.geolocation) {
33 navigator.geolocation.getCurrentPosition(function (position) {
34 let pos = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
35 initMap(pos,map);
36 }, function () {
37 //handleNoGeoLocation()
38 }
39 );
40 } else {
41 alert("You do not support geolocation");
42 }
43
44
45 }
46
47
48 initMap(location,map){
49
50 map.setCenter(location,12);
51
52
53
54 map.addListener('click', function (event) {
55 console.log("jule husker");
56 }
57 );
58
59 }
60
61 logError(error) {
62 console.error(error);
63
64 }
65
66 getData(query,instance){
67 console.log(instance.http);
68 instance.http.get(dhisAPI+'/api/organisationUnits'+query)
69 .map(res => res.json())
70 .subscribe(
71 res => instance.parseResult(res),
72 error => instance.logError(error)
73 );
74
75 }
76
77 parseResult(res){
78
79 if(res.organisationUnits) {
80 for (let item in res.organisationUnits) {
81 this.getData('/' + res.organisationUnits[item].id,this);
82 }
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 {
90
91 this.drawPolygon(res);};
92 }
93 drawPolygon(item){
94 let instance = this;
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);
124
125 this.map.data.addListener('click', function(event) {
126 //TODO: spør om man vil ned/opp eller se info
127
128 let id = event.feature.O.id;
129 console.log(id);
130
131 instance.map.data.forEach(function(feature) {
132 instance.map.data.remove(feature);
133 });
134 // instance.getData('/' + id+'/children',instance);
135 instance.getData('/' + id,instance);
136
137 });
138
139
140 }else {
141 // ToDO:
142 console.log("fiks meg! gi warning på topp av kart");
143 }
144
145
146 }
147
148
149 createOrgUnit(){
150 console.log('you just added a new organisation unit');
151 }
152
153 update(event){
154 this.newactive.next(event);
155 }
156}
157
158
159
160
161