[OpenLayers-Commits] r11988 - in sandbox/ahocevar/google-ng: examples lib lib/OpenLayers/Layer lib/OpenLayers/Tile theme/default

commits-20090109 at openlayers.org commits-20090109 at openlayers.org
Sun May 22 16:14:30 EDT 2011


Author: ahocevar
Date: 2011-05-22 13:14:29 -0700 (Sun, 22 May 2011)
New Revision: 11988

Added:
   sandbox/ahocevar/google-ng/examples/google-ng.html
   sandbox/ahocevar/google-ng/examples/google-ng.js
   sandbox/ahocevar/google-ng/lib/OpenLayers/Layer/GoogleNG.js
   sandbox/ahocevar/google-ng/lib/OpenLayers/Tile/Google.js
Modified:
   sandbox/ahocevar/google-ng/lib/OpenLayers.js
   sandbox/ahocevar/google-ng/theme/default/style.css
Log:
Layer.GoogleNG and Tile.Google using the GMaps v3 API's getTile method to load tiles into the OpenLayers tile grid

Added: sandbox/ahocevar/google-ng/examples/google-ng.html
===================================================================
--- sandbox/ahocevar/google-ng/examples/google-ng.html	                        (rev 0)
+++ sandbox/ahocevar/google-ng/examples/google-ng.html	2011-05-22 20:14:29 UTC (rev 11988)
@@ -0,0 +1,42 @@
+<!DOCTYPE html>
+<html>
+    <head>
+    <meta name="viewport" content="width=device-width; initial-scale=1.0; maximum-scale=1.0; user-scalable=0;" />
+    <meta name="apple-mobile-web-app-capable" content="yes" />
+        <title>OpenLayers Google NG Layer Example</title>
+        <link rel="stylesheet" href="../theme/default/style.css" type="text/css">
+        <link rel="stylesheet" href="../theme/default/google.css" type="text/css">
+        <link rel="stylesheet" href="style.css" type="text/css">
+        <style type="text/css">
+        .olControlAttribution {
+            left: 5px;
+            right: inherit;
+            bottom: 5px; 
+        }
+        </style>
+        <script src="http://maps.google.com/maps/api/js?v=3.5&amp;sensor=false"></script>
+        <script src="../lib/OpenLayers.js"></script>
+        <script src="google-ng.js"></script>
+    </head>
+    <body onload="init()">
+        <h1 id="title">Google NG Layer Example</h1>
+        <div id="tags">
+            Google, api key, apikey
+        </div>
+        <p id="shortdesc">
+            Demonstrate use of tiles from the Google Maps v3 API.
+        </p>
+        <div id="map" class="smallmap"></div>
+        <div id="docs">
+            <p>
+                If you use OpenLayers.Layer.GoogleNG, the getTile method of the
+                GMaps v3 API's MapType is used to load tiles.  This allows for
+                better integration than interacting with a whole map generated
+                by a google.maps.Map instance, as done with
+                OpenLayers.Layer.Google.  See the
+                <a href="google-v3.js" target="_blank">google-ng.js source</a> 
+                to see how this is done.
+            </p>
+        </div>
+    </body>
+</html>

Added: sandbox/ahocevar/google-ng/examples/google-ng.js
===================================================================
--- sandbox/ahocevar/google-ng/examples/google-ng.js	                        (rev 0)
+++ sandbox/ahocevar/google-ng/examples/google-ng.js	2011-05-22 20:14:29 UTC (rev 11988)
@@ -0,0 +1,28 @@
+var map;
+
+function init() {
+    map = new OpenLayers.Map('map');
+    map.addControl(new OpenLayers.Control.LayerSwitcher());
+    
+    var gphy = new OpenLayers.Layer.GoogleNG(
+        {type: google.maps.MapTypeId.TERRAIN}
+    );
+    var gmap = new OpenLayers.Layer.GoogleNG(
+        // ROADMAP, the default
+    );
+    var ghyb = new OpenLayers.Layer.GoogleNG(
+        {type: google.maps.MapTypeId.HYBRID}
+    );
+    var gsat = new OpenLayers.Layer.GoogleNG(
+        {type: google.maps.MapTypeId.SATELLITE}
+    );
+
+    map.addLayers([gphy, gmap, ghyb, gsat]);
+
+    // Google.v3 uses EPSG:900913 as projection, so we have to
+    // transform our coordinates
+    map.setCenter(new OpenLayers.LonLat(10.2, 48.9).transform(
+        new OpenLayers.Projection("EPSG:4326"),
+        map.getProjectionObject()
+    ), 5);
+}

Added: sandbox/ahocevar/google-ng/lib/OpenLayers/Layer/GoogleNG.js
===================================================================
--- sandbox/ahocevar/google-ng/lib/OpenLayers/Layer/GoogleNG.js	                        (rev 0)
+++ sandbox/ahocevar/google-ng/lib/OpenLayers/Layer/GoogleNG.js	2011-05-22 20:14:29 UTC (rev 11988)
@@ -0,0 +1,230 @@
+/* Copyright (c) 2006-2011 by OpenLayers Contributors (see authors.txt for 
+ * full list of contributors). Published under the Clear BSD license.  
+ * See http://svn.openlayers.org/trunk/openlayers/license.txt for the
+ * full text of the license. */
+
+/**
+ * @requires OpenLayers/Layer/XYZ.js
+ */
+
+/** 
+ * Class: OpenLayers.Layer.GoogleNG
+ * Google layer using <OpenLayers.Tile.Google> tiles.
+ * 
+ * Inherits from:
+ *  - <OpenLayers.Layer.XYZ>
+ */
+OpenLayers.Layer.GoogleNG = OpenLayers.Class(OpenLayers.Layer.XYZ, {
+
+    /**
+     * Property: SUPPORTED_TRANSITIONS
+     * {Array} An immutable (that means don't change it!) list of supported 
+     *     transitionEffect values. This layer type supports none.
+     */
+    SUPPORTED_TRANSITIONS: [],
+    
+    /**
+     * Property: attributionTemplate
+     * {String}
+     */
+    attributionTemplate: '<span class="olGoogleAttribution ${mapType}">' +
+         '<div><a target="_blank" href="http://maps.google.com/maps?' +
+         'll=${center}&z=${zoom}&t=${t}"><img width="62" height="24" ' +
+         'src="http://maps.gstatic.com/mapfiles/google_white.png"/></a>' +
+         '</div><a style="white-space: nowrap" target="_blank" ' +
+         'href="http://maps.google.com/maps/api/staticmap?sensor=true' +
+         '&center=${center}&zoom=${zoom}&size=${size}&maptype=${mapType}">' +
+         'Map data</a> - <a style="white-space: nowrap" target="_blank" ' +
+         'href="http://www.google.com/help/terms_maps.html">' +
+         'Terms of Use</a></span>',
+    
+    /**
+     * Property: mapTypes
+     * {Object} mapping of {google.maps.MapTypeId} to the t param of
+     * http://maps.google.com/maps? permalinks
+     */
+    mapTypes: {
+        "roadmap": "m",
+        "satellite": "k",
+        "hybrid": "h",
+        "terrain": "p"
+    },
+    
+    /**
+     * Property: mapObject
+     * {google.maps.Map} Shared GMaps instance - will be set on the prototype
+     * upon instantiation of the 1st GoogleNG layer
+     */
+    mapObject: null,
+
+    /**
+     * APIProperty: type
+     * {google.maps.MapTypeId} See
+     * http://code.google.com/apis/maps/documentation/javascript/reference.html#MapTypeId
+     */
+    type: null,
+
+    /**
+     * Constructor: OpenLayers.Layer.GoogleNG
+     * Create a new GoogleNG layer. Requires the GMaps v3 JavaScript API script
+     * included in the html document.
+     *
+     * Example:
+     * (code)
+     * var terrain = new OpenLayers.Layer.GoogleNG({
+     *     name: "Google Terrain",
+     *     type: google.maps.MapTypeId.TERRAIN
+     * });
+     * (end)
+     *
+     * Parameters:
+     * config - {Object} Configuration properties for the layer.
+     *
+     * Required configuration properties:
+     * type - {String} The layer identifier.  See
+     *     http://code.google.com/apis/maps/documentation/javascript/reference.html#MapTypeId
+     *     for valid types.
+     *
+     * Any other documented layer properties can be provided in the config object.
+     */
+    initialize: function(options) {
+        if (!this.mapObject) {
+            OpenLayers.Layer.GoogleNG.prototype.mapObject =
+                new google.maps.Map(document.createElement("div"));
+        }
+        google.maps.event.addListenerOnce(
+            this.mapObject, 
+            "idle", 
+            OpenLayers.Function.bind(this.initLayer, this)
+        );
+
+        options = OpenLayers.Util.applyDefaults({
+            sphericalMercator: true
+        }, options);
+
+        if (!options.type) {
+            options.type = google.maps.MapTypeId.ROADMAP;
+        }
+        var newArgs = [options.name, null, options];
+        OpenLayers.Layer.XYZ.prototype.initialize.apply(this, newArgs);
+    },
+
+    /**
+     * Method: initLayer
+     *
+     * Sets layer properties according to the metadata provided by the API
+     */
+    initLayer: function() {
+        var mapType = this.mapObject.mapTypes[this.type];
+        if (!this.name) {
+            this.setName("Google " + mapType.name);
+        }
+        
+        var numZoomLevels = mapType.maxZoom + 1;
+        if (this.numZoomLevels) {
+            numZoomLevels = Math.min(numZoomLevels, this.numZoomLevels);
+        }
+        var restrictedMinZoom = mapType.minZoom;
+        if (this.restrictedMinZoom) {
+            restrictedMinZoom = Math.max(
+                restrictedMinZoom, options.restrictedMinZoom
+            );
+        }
+        
+        this.addOptions({
+            restrictedMinZoom: mapType.minZoom,
+            numZoomLevels: numZoomLevels,
+            tileSize: new OpenLayers.Size(
+                mapType.tileSize.width, mapType.tileSize.height
+            )
+        });
+        // redraw to populate tiles with content
+        this.redraw();
+    },
+
+    /**
+     * APIMethod: addTile
+     * Create a tile, initialize it, and add it to the layer div. 
+     *
+     * Parameters
+     * bounds - {<OpenLayers.Bounds>}
+     * position - {<OpenLayers.Pixel>}
+     *
+     * Returns:
+     * {<OpenLayers.Tile.Goolge>} The added OpenLayers.Tile.Google
+     */
+    addTile:function(bounds, position) {
+        return new OpenLayers.Tile.Google(this, position, bounds, null, 
+                                         this.tileSize, this.tileOptions);
+    },
+    
+    /**
+     * Method: updateAttribution
+     * Updates the attribution using the <attributionTemplate>
+     */
+    updateAttribution: function() {
+        var center = this.map.getCenter();
+        center && center.transform(
+            this.map.getProjectionObject(),
+            new OpenLayers.Projection("EPSG:4326")
+        );
+        var size = this.map.getSize();
+        this.attribution = OpenLayers.String.format(this.attributionTemplate, {
+            center: center ? center.lat + "," + center.lon : "",
+            zoom: this.map.getZoom(),
+            size: size.w + "x" + size.h,
+            t: this.mapTypes[this.type],
+            mapType: this.type
+        });
+        this.map && this.map.events.triggerEvent("changelayer", {layer: this});
+    },
+
+    /**
+     * Method: setMap
+     */
+    setMap: function() {
+        OpenLayers.Layer.XYZ.prototype.setMap.apply(this, arguments);
+
+        this.updateAttribution();
+        this.map.events.register("moveend", this, this.updateAttribution);
+    },
+    
+    /**
+     * Method: removeMap
+     */
+    removeMap: function() {
+        OpenLayers.Layer.XYZ.prototype.removeMap.apply(this, arguments);
+        this.map.events.unregister("moveend", this, this.updateAttribution);
+    },
+        
+    /**
+     * APIMethod: clone
+     * 
+     * Parameters:
+     * obj - {Object}
+     * 
+     * Returns:
+     * {<OpenLayers.Layer.GoogleNG>} An exact clone of this
+     * <OpenLayers.Layer.GoogleNG>
+     */
+    clone: function(obj) {
+        if (obj == null) {
+            obj = new OpenLayers.Layer.GoogleNG(this.options);
+        }
+        //get all additions from superclasses
+        obj = OpenLayers.Layer.XYZ.prototype.clone.apply(this, [obj]);
+        // copy/set any non-init, non-simple values here
+        return obj;
+    },
+    
+    /**
+     * Method: destroy
+     */
+    destroy: function() {
+        this.map &&
+            this.map.events.unregister("moveend", this, this.updateAttribution);
+        OpenLayers.Layer.XYZ.prototype.destroy.apply(this, arguments);
+    },
+    
+    CLASS_NAME: "OpenLayers.Layer.GoogleNG"
+});
\ No newline at end of file

Added: sandbox/ahocevar/google-ng/lib/OpenLayers/Tile/Google.js
===================================================================
--- sandbox/ahocevar/google-ng/lib/OpenLayers/Tile/Google.js	                        (rev 0)
+++ sandbox/ahocevar/google-ng/lib/OpenLayers/Tile/Google.js	2011-05-22 20:14:29 UTC (rev 11988)
@@ -0,0 +1,141 @@
+/* Copyright (c) 2006-2011 by OpenLayers Contributors (see authors.txt for 
+ * full list of contributors). Published under the Clear BSD license.  
+ * See http://svn.openlayers.org/trunk/openlayers/license.txt for the
+ * full text of the license. */
+
+
+/*
+ * @requires OpenLayers/BaseTypes/Class.js
+ * @requires OpenLayers/Util.js
+ * @requires OpenLayers/Console.js
+ * @requires OpenLayers/Lang.js
+ */
+
+/*
+ * Class: OpenLayers.Tile.Google
+ * Instances of OpenLayers.Tile.Google are used to manage the tiles created
+ * by google.maps.MapType (see
+ * http://code.google.com/apis/maps/documentation/javascript/reference.html#MapType).
+ *
+ * Inherits from:
+ *  - <OpenLayers.Tile>
+ */
+OpenLayers.Tile.Google = OpenLayers.Class(OpenLayers.Tile, {
+    
+    /**
+     * Property: node
+     * {DOMElement} The tile node from the MapType's getTile method
+     */
+    node: false,
+        
+    /** TBD 3.0 -- remove 'url' from the list of parameters to the constructor.
+     *             there is no need for the base tile class to have a url.
+     * 
+     * Constructor: OpenLayers.Tile.Google
+     * Constructor for a new <OpenLayers.Tile.Google> instance.
+     * 
+     * Parameters:
+     * layer - {<OpenLayers.Layer>} layer that the tile will go in.
+     * position - {<OpenLayers.Pixel>}
+     * bounds - {<OpenLayers.Bounds>}
+     * url - {<String>}
+     * size - {<OpenLayers.Size>}
+     * options - {Object}
+     */   
+    initialize: function(layer, position, bounds, url, size, options) {
+        OpenLayers.Tile.prototype.initialize.apply(this, arguments);
+    },
+
+    /** 
+     * APIMethod: destroy
+     * Nullify references to prevent circular references and memory leaks.
+     */
+    destroy:function() {
+        this.node && this.clear();
+        OpenLayers.Tile.prototype.destroy.apply(this, arguments);
+    },
+    
+    /**
+     * Method: clone
+     *
+     * Parameters:
+     * obj - {<OpenLayers.Tile>} The tile to be cloned
+     *
+     * Returns:
+     * {<OpenLayers.Tile>} An exact clone of this <OpenLayers.Tile.Google>
+     */
+    clone: function (obj) {
+        if (obj == null) {
+            obj = new OpenLayers.Tile.Google(this.layer, 
+                                      this.position, 
+                                      this.bounds, 
+                                      this.url, 
+                                      this.size);
+        } 
+        
+        // catch any randomly tagged-on properties
+        OpenLayers.Util.applyDefaults(obj, this);
+        
+        return obj;
+    },
+
+    /**
+     * Method: draw
+     * Check that a tile should be drawn, and draw it.
+     * 
+     * Returns:
+     * {Boolean} Always returns true.
+     */
+    draw: function() {
+        var layerType = this.layer.mapObject.mapTypes[this.layer.type];
+        if (layerType && OpenLayers.Tile.prototype.draw.apply(this, arguments)) {
+            var xyz = this.layer.getXYZ(this.bounds);
+            var point = new google.maps.Point(xyz.x, xyz.y);
+
+            // The hybrid tile consists of two images. For some reason, we have
+            // to make sure that the satellite image loads first, otherwise we
+            // occasionally get blank tiles for one of the two images. This is
+            // done by requesting the tile for just the satellite mapType
+            // first, before requesting the hybrid one.
+            //TODO revisit this - it may be a temporary issue with GMaps
+            if (this.layer.type === google.maps.MapTypeId.HYBRID) {
+                layerType.getTile(point, xyz.z, document);
+            }
+            
+            this.node = layerType.getTile(point, xyz.z, document);
+            
+            this.isLoading = true;
+            this.events.triggerEvent("loadstart");
+
+            this.layer.div.appendChild(this.node);
+            OpenLayers.Util.modifyDOMElement(
+                this.node, null, this.position, null, "absolute"
+            );  
+            
+            // The images inside the node returned from getTile seem to be
+            // prelaoded already, so registering onload events on these images
+            // won't work. Instead, we trigger the loadend event immediately
+            // in the next cycle.
+            window.setTimeout(OpenLayers.Function.bind(function() {
+                this.isLoading = false;
+                this.events.triggerEvent("loadend"); 
+            }, this), 0);
+        }
+        return true;
+    },
+    
+    /** 
+     * Method: clear
+     * Clear the tile of any bounds/position-related data so that it can 
+     *     be reused in a new location. To be implemented by subclasses.
+     */
+    clear: function() {
+        if (this.node) {
+            this.node.parentNode &&
+                this.node.parentNode.removeChild(this.node);
+            this.layer.mapObject.mapTypes[this.layer.type].releaseTile(this.node);
+        }
+    },
+    
+    CLASS_NAME: "OpenLayers.Tile.Google"
+});

Modified: sandbox/ahocevar/google-ng/lib/OpenLayers.js
===================================================================
--- sandbox/ahocevar/google-ng/lib/OpenLayers.js	2011-05-22 20:12:12 UTC (rev 11987)
+++ sandbox/ahocevar/google-ng/lib/OpenLayers.js	2011-05-22 20:14:29 UTC (rev 11988)
@@ -120,6 +120,7 @@
                 "OpenLayers/Marker/Box.js",
                 "OpenLayers/Popup.js",
                 "OpenLayers/Tile.js",
+                "OpenLayers/Tile/Google.js",
                 "OpenLayers/Tile/Image.js",
                 "OpenLayers/Tile/Image/IFrame.js",
                 "OpenLayers/Tile/WFS.js",
@@ -152,6 +153,7 @@
                 "OpenLayers/Layer/Boxes.js",
                 "OpenLayers/Layer/XYZ.js",
                 "OpenLayers/Layer/Bing.js",
+                "OpenLayers/Layer/GoogleNG.js",
                 "OpenLayers/Layer/TMS.js",
                 "OpenLayers/Layer/TileCache.js",
                 "OpenLayers/Layer/Zoomify.js",

Modified: sandbox/ahocevar/google-ng/theme/default/style.css
===================================================================
--- sandbox/ahocevar/google-ng/theme/default/style.css	2011-05-22 20:12:12 UTC (rev 11987)
+++ sandbox/ahocevar/google-ng/theme/default/style.css	2011-05-22 20:14:29 UTC (rev 11988)
@@ -355,6 +355,19 @@
     color: #333;
 }
 
+.olGoogleAttribution.hybrid, .olGoogleAttribution.satellite {
+    color: #DDD;
+}
+.olGoogleAttribution {
+    color: #333;
+}
+span.olGoogleAttribution a {
+    color: #77C;
+}
+span.olGoogleAttribution.hybrid a, span.olGoogleAttribution.satellite a {
+    color: white;
+}
+
 /**
  * Editing and navigation icons.
  * (using the editing_tool_bar.png sprint image)



More information about the Commits mailing list