]> git.uio.no Git - u/erikhf/frm.git/blobdiff - src/components/map/map.ts
expaned to alle levels downwords
[u/erikhf/frm.git] / src / components / map / map.ts
index 8bed45fa1b77530e3fec77aecaebfbfb7415d585..ab9f39c5d54fa7379454aa9eee5ead18e25d2cb2 100644 (file)
@@ -1,58 +1,51 @@
-import {Component, CORE_DIRECTIVES,} from 'angular2/angular2';
+import {Component, EventEmitter,CORE_DIRECTIVES,} from 'angular2/angular2';
 import {Headers, Http} from 'angular2/http';
 
 
 @Component({
     selector: 'mou-map',
     directives: [CORE_DIRECTIVES],
+    events: ['newactive'],
     templateUrl: './components/map/map.html'
 })
 
 
 export class Map {
-    result:Object;
+
     map:Object;
-    pos:Object;
-    marker:Object;
+    http: Http;
+    LEVEL: number;
+    runned: boolean;
 
     constructor(http:Http) {
-        this.initMap();
-
-        var authHeader = new Headers();
-        authHeader.append('Authorization', 'Basic YWRtaW46ZGlzdHJpY3Q=');
-        this.result = {organisationUnits: []};
-        // http.get(dhisAPI+'/api/organisationUnits?paging=false', {headers: authHeader})
-        http.get('http://mydhis.com:8082/api/organisationUnits?paging=false', {headers: authHeader})
-            .map(res => res.json()).subscribe(
-            res => this.result = res,
-            error => this.logError(error)
-        );
+        this.newactive = new EventEmitter();
+        this.map = new google.maps.Map(document.getElementById("map"),{center: {lat:0,lng:0}, zoom:12});
+        this.init();
+        this.http = http;
+        this.LEVEL = 2;
+        this.runned = false;
+        this.getData('?paging=false&level=2',this);
+    }
+
+    setRunned(value){
+        this.runned = value;
     }
 
+    addLevel(){
+        this.LEVEL++;
+    }
 
-    initMap() {
+    init() {
 
+        let initMap = this.initMap;
+        let map = this.map;
         if (navigator.geolocation) {
             navigator.geolocation.getCurrentPosition(function (position) {
-                    this.pos = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
-                    this.map = new google.maps.Map(document.getElementById("map"),
-                        {center: this.pos, zoom: 12});
-                    this.marker = new google.maps.Marker({
-                        position: this.pos,
-                        map: this.map,
-                        title: 'Me'
-                    });
-
-                    let infowindow = new google.maps.InfoWindow({
-                        content: "This is You"
-                    });
-                    this.marker.addListener('click', function () {
-                        infowindow.open(this.map, this.marker);
-                    });
-                   // this.map.addListener('click', this.addMarker(position.coords.latitude, position.coords.longitude));
-
+                    //let pos = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
+                let pos = {lat:10,lng:39}
+                    initMap(pos,map);
                 }, function () {
-                    //handleNoGeolocation(true);
+                //handleNoGeoLocation()
                 }
             );
         } else {
@@ -63,36 +56,125 @@ export class Map {
     }
 
 
-    //Other map functions
-   // addMarker(lat, lng) {
+    initMap(location,map){
+
+        map.setCenter(location,3);
 
-     //   let marker = new google.maps.Marker({
-       //     position: {lat, lng},
-         //   map: this.map,
-        //    title: 'Me'
-        //});
-    //}
+
+
+        map.addListener('click', function (event) {
+                console.log(event.latlng);
+            }
+        );
+
+    }
 
     logError(error) {
         console.error(error);
 
     }
 
-}
+    getData(query,instance){
+        console.log(instance.http);
+        instance.http.get(dhisAPI+'/api/organisationUnits'+query)
+            .map(res => res.json())
+            .subscribe(
+                res => instance.parseResult(res,instance),
+                error => instance.logError(error)
+            );
+
+    }
+
+    parseResult(res,instance){
+
+        if(res.organisationUnits) {
+            for (let item in res.organisationUnits) {
+                this.getData('/' + res.organisationUnits[item].id,this);
+            }
+            //liten hack
+        }else if(!res.displayName && res.children){
+            for (let item in res.children) {
+                if(res.children[item].level == instance.LEVEL){
+                    this.getData('/' + res.children[item].id,this);
+                }
+            }
+            instance.setRunned(false);
+        }
+        else {
+            this.drawPolygon(res);
+        };
+    }
+    drawPolygon(item){
+        let instance = this;
+        let feature;
+        let incoming: string;
+        incoming = item.featureType.toLowerCase();
+        switch(incoming){
+            case "point":
+                feature = 'Point';
+                break;
+            case "multi_polygon":
+                feature = 'MultiPolygon';
+                break;
+             case "polygon":
+                 feature = 'MultiPolygon';
+                break;
+            default:
+        }
+          // TODO: test på feature og behandle type: NONE
+        if(feature !== undefined) {
+            let unit = {
+                "type": "Feature",
+                "geometry": {
+                    "type": feature,
+                    "coordinates": JSON.parse(item.coordinates)
+                },
+                "properties": {
+                    "name": item.name,
+                    "id": item.id
+                }
+            };
+            if(unit.geometry.type == 'Point'){
+                unit.geometry.type.);
+            }
+            this.map.data.addGeoJson(unit);
+
+            this.map.data.addListener('click', function(event) {
+               //TODO: spør om man vil ned/opp eller se info
+                if(instance.runned == false){
+                    instance.setRunned(true);
+
+
+                    let id = event.feature.O.id;
+                    console.log(id);
+
+                    instance.map.data.forEach(function(feature) {
+                        instance.map.data.remove(feature);
+                    });
+                    instance.addLevel();
+                    instance.getData('/' + id+'/children',instance);
+                }
 
+            });
 
-/* showOnMap(){
- var bermudaTriangle = new google.maps.Polygon({
- paths: triangleCoords,
- strokeColor: '#FF0000',
- strokeOpacity: 0.8,
- strokeWeight: 2,
- fillColor: '#FF0000',
- fillOpacity: 0.35
- });
- bermudaTriangle.setMap(this.map);
+        }else {
+            // ToDO:
+            console.log("fiks meg! gi warning på topp av kart");
+        }
+
+
+    }
+
+
+    createOrgUnit(){
+        console.log('you just added a new organisation unit');
+    }
+
+    update(event){
+        this.newactive.next(event);
+    }
+}
 
- }*/