]> 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 c69e3709c4d493e6d6295eafb5c920eac51cd360..ab9f39c5d54fa7379454aa9eee5ead18e25d2cb2 100644 (file)
@@ -1,10 +1,11 @@
-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'
 })
 
@@ -12,30 +13,39 @@ import {Headers, Http} from 'angular2/http';
 export class Map {
 
     map:Object;
-    marker:Object;
     http: Http;
+    LEVEL: number;
+    runned: boolean;
 
     constructor(http:Http) {
-
+        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);
+    }
 
-        this.getData('?paging=false&level=2');
+    setRunned(value){
+        this.runned = value;
     }
 
+    addLevel(){
+        this.LEVEL++;
+    }
 
     init() {
 
         let initMap = this.initMap;
-        let addMarker = this.addMarker;
         let map = this.map;
         if (navigator.geolocation) {
             navigator.geolocation.getCurrentPosition(function (position) {
-                    let pos = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
-                    initMap(pos,map,addMarker);
+                    //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 {
@@ -46,101 +56,112 @@ export class Map {
     }
 
 
-    initMap(location,map,addMarker){
-
+    initMap(location,map){
 
-        map.setCenter(location,12);
-
-        addMarker(location,map,'This is YOU');
+        map.setCenter(location,3);
 
 
 
         map.addListener('click', function (event) {
-                addMarker(event.latLng,map, 'Want to add a new marker here ? <br> <button onclick=\"createOrgUnit()\">Yes</button> <button onclick=\"deleteMarker()">No</button> ');
+                console.log(event.latlng);
             }
         );
 
     }
 
-    addMarker(location, map, title) {
-
-        let marker = new google.maps.Marker({
-            position: location,
-            map: map
-        });
-
-        let infowindow = new google.maps.InfoWindow({
-            content: title
-        });
-
-        marker.addListener('click', function () {
-            console.log(marker);
-            infowindow.open(map,marker);
-        });
-
-
-    }
-
     logError(error) {
         console.error(error);
 
     }
 
-    getData(query){
-        this.http.get(dhisAPI+'/api/organisationUnits'+query)
+    getData(query,instance){
+        console.log(instance.http);
+        instance.http.get(dhisAPI+'/api/organisationUnits'+query)
             .map(res => res.json())
             .subscribe(
-                res => this.parseResult(res),
-                error => this.logError(error)
+                res => instance.parseResult(res,instance),
+                error => instance.logError(error)
             );
 
     }
 
-    parseResult(res){
+    parseResult(res,instance){
 
         if(res.organisationUnits) {
             for (let item in res.organisationUnits) {
-                this.getData('/' + res.organisationUnits[item].id);
+                this.getData('/' + res.organisationUnits[item].id,this);
             }
-        } else {
-
-            this.drawPolygon(res);};
+            //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){
-        console.log(item);
-        console.log(item.featureType);
-        console.log(item.coordinates);
-        console.log(item.name);
-        let unit = {
-            "type": "Feature",
-            "geometry": {
-                "type": "MultiPolygon",
-                "coordinates": JSON.parse(item.coordinates)},
-            "properties": {
-                "name": item.name
+        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.center(unit.geometry.coordinates);
+            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);
+                }
+
+            });
+
+        }else {
+            // ToDO:
+            console.log("fiks meg! gi warning på topp av kart");
+        }
 
-    center(coordinates){
-        // let bounds = new google.maps.LatLngBounds(coordinates);
-        /* console.log(coordinates.Array);
-         for (let i = 0; i < coordinates.length; i++) {
-         for(let j = 0; j < coordinates[i]; j++) {
-         console.log(coordinates.Array[j]);
-         bounds.extend(coordinates.Array[j]);
-         }
-
-         }*/
-        // for (let i = 0; i < coordinates.length; i++) {
-        //     bounds.extend(new google.maps.LatLng(coordinates.[i][1], coordinates[i][2]));
-        // }
-        // console.log("center: " + bounds.getCenter());
 
     }
 
@@ -149,12 +170,9 @@ export class Map {
         console.log('you just added a new organisation unit');
     }
 
-    deleteMarker(){
-        console.log('you just deleted the marker');
+    update(event){
+        this.newactive.next(event);
     }
-    //Other map functions
-
-
 }