[map] This fix
[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
51
52 map.setCenter(location,12);
53
54 addMarker(location,map,'This is YOU');
55
56
dd095993 57
6ced1bc7
JHR
58 map.addListener('click', function (event) {
59 addMarker(event.latLng,map, 'Want to add a new marker here ? <br> <button onclick=\"createOrgUnit()\">Yes</button> <button onclick=\"deleteMarker()">No</button> ');
60 }
61 );
62
63 }
64
65 addMarker(location, map, title) {
66
67 let marker = new google.maps.Marker({
68 position: location,
69 map: map
70 });
71
72 let infowindow = new google.maps.InfoWindow({
73 content: title
74 });
75
76 marker.addListener('click', function () {
77 console.log(marker);
78 infowindow.open(map,marker);
79 });
80
81
82 }
dd095993 83
a2d7d6b4
JHR
84 logError(error) {
85 console.error(error);
86
cb2c4ba8 87 }
28765058 88
c7e8b786
EHF
89 getData(query,instance){
90 console.log(instance.http);
91 instance.http.get(dhisAPI+'/api/organisationUnits'+query)
6ced1bc7
JHR
92 .map(res => res.json())
93 .subscribe(
c7e8b786
EHF
94 res => instance.parseResult(res),
95 error => instance.logError(error)
6ced1bc7
JHR
96 );
97
98 }
99
100 parseResult(res){
101
102 if(res.organisationUnits) {
103 for (let item in res.organisationUnits) {
c7e8b786 104 this.getData('/' + res.organisationUnits[item].id,this);
6ced1bc7
JHR
105 }
106 } else {
107
108 this.drawPolygon(res);};
109 }
110 drawPolygon(item){
c7e8b786 111 let instance = this;
1f8c27ee
JHR
112 let feature;
113 let incoming: string;
114 incoming = item.featureType.toLowerCase();
115 switch(incoming){
116 case "point":
117 feature = 'Point';
118 break;
119 case "multi_polygon":
120 feature = 'MultiPolygon';
121 break;
122 case "polygon":
123 feature = 'MultiPolygon';
124 break;
125 default:
126 }
127 // TODO: test på feature og behandle type: NONE
128 if(feature !== undefined) {
129 let unit = {
130 "type": "Feature",
131 "geometry": {
132 "type": feature,
133 "coordinates": JSON.parse(item.coordinates)
134 },
135 "properties": {
136 "name": item.name,
137 "id": item.id
138 }
139 };
140 this.map.data.addGeoJson(unit);
c7e8b786 141
1f8c27ee 142 this.map.data.addListener('click', function(event) {
bbee9db0 143 //TODO: spør om man vil ned/opp eller se info
1f8c27ee 144
bbee9db0
JHR
145 let id = event.feature.O.id;
146 console.log(id);
147
c7e8b786 148 instance.getData('/' + id,instance);
6ced1bc7 149
bbee9db0
JHR
150 });
151
152
1f8c27ee
JHR
153 }else {
154 // ToDO:
155 console.log("fiks meg! gi warning på topp av kart");
156 }
6ced1bc7 157
6ced1bc7
JHR
158
159 }
160
dd095993 161
6ced1bc7
JHR
162 createOrgUnit(){
163 console.log('you just added a new organisation unit');
164 }
dd095993 165
6ced1bc7
JHR
166 deleteMarker(){
167 console.log('you just deleted the marker');
168 }
6ced1bc7
JHR
169
170
171}
dd095993 172
dd095993
JHR
173
174
175
176