← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 18750: GIS dynamic loading of GM api.

 

Merge authors:
  Jan Henrik Øverland (janhenrik-overland)
------------------------------------------------------------
revno: 18750 [merge]
committer: Jan Henrik Overland <janhenrik.overland@xxxxxxxxx>
branch nick: dhis2
timestamp: Mon 2015-03-30 16:24:23 +0200
message:
  GIS dynamic loading of GM api.
modified:
  dhis-2/dhis-web/dhis-web-apps/src/main/webapp/dhis-web-mapping/index.html
  dhis-2/dhis-web/dhis-web-apps/src/main/webapp/dhis-web-mapping/scripts/app.js
  dhis-2/dhis-web/dhis-web-apps/src/main/webapp/dhis-web-mapping/scripts/core.js
  dhis-2/dhis-web/dhis-web-dashboard-integration/src/main/webapp/dhis-web-dashboard-integration/plugin/map.js


--
lp:dhis2
https://code.launchpad.net/~dhis2-devs-core/dhis2/trunk

Your team DHIS 2 developers is subscribed to branch lp:dhis2.
To unsubscribe from this branch go to https://code.launchpad.net/~dhis2-devs-core/dhis2/trunk/+edit-subscription
=== modified file 'dhis-2/dhis-web/dhis-web-apps/src/main/webapp/dhis-web-mapping/index.html'
--- dhis-2/dhis-web/dhis-web-apps/src/main/webapp/dhis-web-mapping/index.html	2015-01-12 14:27:58 +0000
+++ dhis-2/dhis-web/dhis-web-apps/src/main/webapp/dhis-web-mapping/index.html	2015-03-30 14:18:36 +0000
@@ -23,7 +23,9 @@
 		window.google = null;
 	</script>
 
+<!--
     <script type="text/javascript" src="https://maps.google.com/maps/api/js?sensor=false";></script>
+-->
 
     <script type="text/javascript" src="../dhis-web-commons/javascripts/ext/ext-all.js"></script>
     <script type="text/javascript" src="../dhis-web-commons/javascripts/openlayers/OpenLayers.js"></script>

=== modified file 'dhis-2/dhis-web/dhis-web-apps/src/main/webapp/dhis-web-mapping/scripts/app.js'
--- dhis-2/dhis-web/dhis-web-apps/src/main/webapp/dhis-web-mapping/scripts/app.js	2015-03-19 17:39:10 +0000
+++ dhis-2/dhis-web/dhis-web-apps/src/main/webapp/dhis-web-mapping/scripts/app.js	2015-03-30 14:18:36 +0000
@@ -1864,9 +1864,15 @@
 			items = [],
 			item,
 			panel,
-			visibleLayer = window.google ? layers.googleStreets : layers.openStreetMap,
+			visibleLayer = function() {
+                return window.google ? layers.googleStreets : layers.openStreetMap;
+            }(),
 			orderedLayers = gis.olmap.layers.reverse();
 
+        // gm first
+        orderedLayers.push(orderedLayers.shift());
+        orderedLayers.push(orderedLayers.shift());
+
 		for (var i = 0; i < orderedLayers.length; i++) {
 			layer = orderedLayers[i];
 
@@ -9372,12 +9378,72 @@
 
 		fn = function() {
 			if (++callbacks === requests.length) {
+
+                // instance
 				gis = GIS.core.getInstance(init);
 
+                // ux
 				GIS.app.createExtensions();
 
+                // extend instance
 				GIS.app.extendInstance(gis);
 
+                // google maps
+                var gm_fn = function() {
+                    var googleStreets = new OpenLayers.Layer.Google('Google Streets', {
+                        numZoomLevels: 20,
+                        animationEnabled: true,
+                        layerType: gis.conf.finals.layer.type_base,
+                        layerOpacity: 1,
+                        setLayerOpacity: function(number) {
+                            if (number) {
+                                this.layerOpacity = parseFloat(number);
+                            }
+                            this.setOpacity(this.layerOpacity);
+                        }
+                    });
+                    googleStreets.id = 'googleStreets';
+                    gis.layer.googleStreets = googleStreets;
+
+                    var googleHybrid = new OpenLayers.Layer.Google('Google Hybrid', {
+                        type: google.maps.MapTypeId.HYBRID,
+                        numZoomLevels: 20,
+                        animationEnabled: true,
+                        layerType: gis.conf.finals.layer.type_base,
+                        layerOpacity: 1,
+                        setLayerOpacity: function(number) {
+                            if (number) {
+                                this.layerOpacity = parseFloat(number);
+                            }
+                            this.setOpacity(this.layerOpacity);
+                        }
+                    });
+                    googleHybrid.id = 'googleHybrid';
+                    gis.layer.googleHybrid = googleHybrid;
+
+                    gis.olmap.addLayers([googleStreets, googleHybrid]);
+                    gis.olmap.setBaseLayer(googleStreets);
+                };
+
+                if (GIS_GM.ready) {
+                    console.log('GM is ready -> skip queue, add layers, set as baselayer');
+                    gm_fn();
+                }
+                else {
+                    if (GIS_GM.offline) {
+                        console.log('Deactivate base layer');
+                        gis.olmap.baseLayer.setVisibility(false);
+                    }
+                    else {
+                        console.log('GM is not ready -> add to queue');
+                        GIS_GM.array.push({
+                            scope: this,
+                            fn: gm_fn
+                        });
+                    }
+                }
+
+                // viewport
 				gis.viewport = createViewport();
 			}
 		};
@@ -9393,6 +9459,37 @@
 
         dhis2.gis.store.open();
 
+        // inject google maps
+        GIS_GM = {
+            ready: false,
+            array: [],
+            offline: false
+        };
+
+        GIS_GM_fn = function() {
+            console.log("GM called back, queue length: " + GIS_GM.array.length);
+            GIS_GM.ready = true;
+
+            for (var i = 0, obj; i < GIS_GM.array.length; i++) {
+                obj = GIS_GM.array[i];
+
+                if (obj) {
+                    console.log("Running queue obj " + (i + 1));
+                    obj.fn.call(obj.scope);
+                }
+            }
+        };
+
+        Ext.Loader.injectScriptElement('//maps.googleapis.com/maps/api/js?callback=GIS_GM_fn',
+            function() {
+                console.log("GM available (online)");
+            },
+            function() {
+                console.log("GM not available (offline)");
+                GIS_GM.offline = true;
+            }
+        );
+
 		// requests
 		Ext.Ajax.request({
 			url: 'manifest.webapp',

=== modified file 'dhis-2/dhis-web/dhis-web-apps/src/main/webapp/dhis-web-mapping/scripts/core.js'
--- dhis-2/dhis-web/dhis-web-apps/src/main/webapp/dhis-web-mapping/scripts/core.js	2015-03-19 17:39:10 +0000
+++ dhis-2/dhis-web/dhis-web-apps/src/main/webapp/dhis-web-mapping/scripts/core.js	2015-03-30 14:18:36 +0000
@@ -104,36 +104,36 @@
 			createSelectionHandlers,
 			layerNumbers = ['1', '2', '3', '4'];
 
-		if (window.google) {
-			layers.googleStreets = new OpenLayers.Layer.Google('Google Streets', {
-				numZoomLevels: 20,
-				animationEnabled: true,
-				layerType: gis.conf.finals.layer.type_base,
-				layerOpacity: 1,
-				setLayerOpacity: function(number) {
-					if (number) {
-						this.layerOpacity = parseFloat(number);
-					}
-					this.setOpacity(this.layerOpacity);
-				}
-			});
-			layers.googleStreets.id = 'googleStreets';
+		//if (window.google) {
+			//layers.googleStreets = new OpenLayers.Layer.Google('Google Streets', {
+				//numZoomLevels: 20,
+				//animationEnabled: true,
+				//layerType: gis.conf.finals.layer.type_base,
+				//layerOpacity: 1,
+				//setLayerOpacity: function(number) {
+					//if (number) {
+						//this.layerOpacity = parseFloat(number);
+					//}
+					//this.setOpacity(this.layerOpacity);
+				//}
+			//});
+			//layers.googleStreets.id = 'googleStreets';
 
-			layers.googleHybrid = new OpenLayers.Layer.Google('Google Hybrid', {
-				type: google.maps.MapTypeId.HYBRID,
-				numZoomLevels: 20,
-				animationEnabled: true,
-				layerType: gis.conf.finals.layer.type_base,
-				layerOpacity: 1,
-				setLayerOpacity: function(number) {
-					if (number) {
-						this.layerOpacity = parseFloat(number);
-					}
-					this.setOpacity(this.layerOpacity);
-				}
-			});
-			layers.googleHybrid.id = 'googleHybrid';
-		}
+			//layers.googleHybrid = new OpenLayers.Layer.Google('Google Hybrid', {
+				//type: google.maps.MapTypeId.HYBRID,
+				//numZoomLevels: 20,
+				//animationEnabled: true,
+				//layerType: gis.conf.finals.layer.type_base,
+				//layerOpacity: 1,
+				//setLayerOpacity: function(number) {
+					//if (number) {
+						//this.layerOpacity = parseFloat(number);
+					//}
+					//this.setOpacity(this.layerOpacity);
+				//}
+			//});
+			//layers.googleHybrid.id = 'googleHybrid';
+		//}
 
 		layers.openStreetMap = new OpenLayers.Layer.OSM.Mapnik('OpenStreetMap', {
 			layerType: gis.conf.finals.layer.type_base,
@@ -3573,9 +3573,9 @@
 		gis.layer = GIS.core.getLayers(gis);
 		gis.thematicLayers = [gis.layer.thematic1, gis.layer.thematic2, gis.layer.thematic3, gis.layer.thematic4];
 
-		if (window.google) {
-			layers.push(gis.layer.googleStreets, gis.layer.googleHybrid);
-		}
+		//if (window.google) {
+			//layers.push(gis.layer.googleStreets, gis.layer.googleHybrid);
+		//}
 
 		layers.push(
 			gis.layer.openStreetMap,

=== modified file 'dhis-2/dhis-web/dhis-web-dashboard-integration/src/main/webapp/dhis-web-dashboard-integration/plugin/map.js'
--- dhis-2/dhis-web/dhis-web-dashboard-integration/src/main/webapp/dhis-web-dashboard-integration/plugin/map.js	2015-03-23 12:48:05 +0000
+++ dhis-2/dhis-web/dhis-web-dashboard-integration/src/main/webapp/dhis-web-dashboard-integration/plugin/map.js	2015-03-30 14:18:36 +0000
@@ -7283,9 +7283,11 @@
 
                 if (!base || base === 'none' || base === 'off') {
                     gis.layer.googleStreets.setVisibility(false);
-                } else if (base === 'gh' || base === 'googlehybrid') {
+                }
+                else if (base === 'gh' || base === 'googlehybrid') {
                     gis.olmap.setBaseLayer(gis.layer.googleHybrid);
-                } else if (base === 'osm' || base === 'openstreetmap') {
+                }
+                else if (base === 'osm' || base === 'openstreetmap') {
                     gis.olmap.setBaseLayer(gis.layer.openStreetMap);
                 }
             }