add support for GMaps v3. Thanks tschaub for the big improvements to my original patch. p=tschaub,me, r=tschaub (closes #2493)

git-svn-id: http://svn.openlayers.org/trunk/openlayers@10474 dc9f47b5-9b13-0410-9fdd-eb0c1a62fdaf
This commit is contained in:
ahocevar
2010-07-09 12:01:11 +00:00
parent 4d3a1eebf7
commit 64842fd711
10 changed files with 2078 additions and 357 deletions

View File

@@ -7,7 +7,7 @@
* @requires OpenLayers/Layer/SphericalMercator.js
* @requires OpenLayers/Layer/EventPane.js
* @requires OpenLayers/Layer/FixedZoomLevels.js
* @requires OpenLayers/Console.js
* @requires OpenLayers/Layer/Google/v2.js
*/
/**
@@ -80,23 +80,10 @@ OpenLayers.Layer.Google = OpenLayers.Class(
sphericalMercator: false,
/**
* Property: dragObject
* {GDraggableObject} Since 2.93, Google has exposed the ability to get
* the maps GDraggableObject. We can now use this for smooth panning
* Property: version
* {Number} The version of the Google Maps API
*/
dragObject: null,
/**
* Property: termsOfUse
* {DOMElement} Div for Google's copyright and terms of use link
*/
termsOfUse: null,
/**
* Property: poweredBy
* {DOMElement} Div for Google's powered by logo and link
*/
poweredBy: null,
version: null,
/**
* Constructor: OpenLayers.Layer.Google
@@ -107,16 +94,34 @@ OpenLayers.Layer.Google = OpenLayers.Class(
* on the layer.
*/
initialize: function(name, options) {
OpenLayers.Layer.EventPane.prototype.initialize.apply(this, arguments);
options = options || {};
if(!options.version) {
options.version = typeof GMap2 === "function" ? "2" : "3";
}
var mixin = OpenLayers.Layer.Google["v" +
options.version.replace(/\./g, "_")];
if (mixin) {
OpenLayers.Util.applyDefaults(options, mixin);
} else {
throw "Unsupported Google Maps API version: " + options.version;
}
OpenLayers.Util.applyDefaults(options, mixin.DEFAULTS);
if (options.maxExtent) {
options.maxExtent = options.maxExtent.clone();
}
OpenLayers.Layer.EventPane.prototype.initialize.apply(this,
[name, options]);
OpenLayers.Layer.FixedZoomLevels.prototype.initialize.apply(this,
arguments);
this.addContainerPxFunction();
[name, options]);
if (this.sphericalMercator) {
OpenLayers.Util.extend(this, OpenLayers.Layer.SphericalMercator);
this.initMercatorParameters();
}
},
/**
* Method: clone
* Create a clone of this layer
@@ -135,116 +140,6 @@ OpenLayers.Layer.Google = OpenLayers.Class(
this.name, this.getOptions()
);
},
/**
* Method: loadMapObject
* Load the GMap and register appropriate event listeners. If we can't
* load GMap2, then display a warning message.
*/
loadMapObject:function() {
if (!this.type) {
this.type = G_NORMAL_MAP;
}
var mapObject, termsOfUse, poweredBy;
var cache = OpenLayers.Layer.Google.cache[this.map.id];
if (cache) {
// there are already Google layers added to this map
mapObject = cache.mapObject;
termsOfUse = cache.termsOfUse;
poweredBy = cache.poweredBy;
// increment the layer count
++cache.count;
} else {
// this is the first Google layer for this map
var container = this.map.viewPortDiv;
var div = document.createElement("div");
div.id = this.map.id + "_GMap2Container";
div.style.position = "absolute";
div.style.width = "100%";
div.style.height = "100%";
container.appendChild(div);
// create GMap and shuffle elements
try {
mapObject = new GMap2(div);
// move the ToS and branding stuff up to the container div
termsOfUse = div.lastChild;
container.appendChild(termsOfUse);
termsOfUse.style.zIndex = "1100";
termsOfUse.style.right = "";
termsOfUse.style.bottom = "";
termsOfUse.className = "olLayerGoogleCopyright";
poweredBy = div.lastChild;
container.appendChild(poweredBy);
poweredBy.style.zIndex = "1100";
poweredBy.style.right = "";
poweredBy.style.bottom = "";
poweredBy.className = "olLayerGooglePoweredBy gmnoprint";
} catch (e) {
OpenLayers.Console.error(e);
return;
}
// cache elements for use by any other google layers added to
// this same map
OpenLayers.Layer.Google.cache[this.map.id] = {
mapObject: mapObject,
termsOfUse: termsOfUse,
poweredBy: poweredBy,
count: 1
};
}
this.mapObject = mapObject;
this.termsOfUse = termsOfUse;
this.poweredBy = poweredBy;
// ensure this layer type is one of the mapObject types
if (OpenLayers.Util.indexOf(this.mapObject.getMapTypes(),
this.type) === -1) {
this.mapObject.addMapType(this.type);
}
//since v 2.93 getDragObject is now available.
if(typeof mapObject.getDragObject == "function") {
this.dragObject = mapObject.getDragObject();
} else {
this.dragPanMapObject = null;
}
if(this.isBaseLayer === false) {
this.setGMapVisibility(this.div.style.display !== "none");
}
},
/**
* APIMethod: onMapResize
*/
onMapResize: function() {
// workaround for resizing of invisible or not yet fully loaded layers
// where GMap2.checkResize() does not work. We need to load the GMap
// for the old div size, then checkResize(), and then call
// layer.moveTo() to trigger GMap.setCenter() (which will finish
// the GMap initialization).
if(this.visibility && this.mapObject.isLoaded()) {
this.mapObject.checkResize();
} else {
if(!this._resized) {
var layer = this;
var handle = GEvent.addListener(this.mapObject, "load", function() {
GEvent.removeListener(handle);
delete layer._resized;
layer.mapObject.checkResize();
layer.moveTo(layer.map.getCenter(), layer.map.getZoom());
});
}
this._resized = true;
}
},
/**
* APIMethod: setVisibility
@@ -269,43 +164,6 @@ OpenLayers.Layer.Google = OpenLayers.Class(
this.setOpacity(opacity);
},
/**
* Method: setGMapVisibility
* Display the GMap container and associated elements.
*
* Parameters:
* visible - {Boolean} Display the GMap elements.
*/
setGMapVisibility: function(visible) {
var cache = OpenLayers.Layer.Google.cache[this.map.id];
if (cache) {
var container = this.mapObject.getContainer();
if (visible === true) {
this.mapObject.setMapType(this.type);
container.style.display = "";
this.termsOfUse.style.left = "";
this.termsOfUse.style.display = "";
this.poweredBy.style.display = "";
cache.displayed = this.id;
} else {
if (cache.displayed === this.id) {
delete cache.displayed;
}
if (!cache.displayed) {
container.style.display = "none";
this.termsOfUse.style.display = "none";
// move ToU far to the left in addition to setting display
// to "none", because at the end of the GMap2 load
// sequence, display: none will be unset and ToU would be
// visible after loading a map with a google layer that is
// initially hidden.
this.termsOfUse.style.left = "-9999px";
this.poweredBy.style.display = "none";
}
}
}
},
/**
* APIMethod: setOpacity
* Sets the opacity for the entire layer (all images)
@@ -326,7 +184,7 @@ OpenLayers.Layer.Google = OpenLayers.Class(
// Though this layer's opacity may not change, we're sharing a container
// and need to update the opacity for the entire container.
if (this.getVisibility()) {
var container = this.mapObject.getContainer();
var container = this.getMapContainer();
OpenLayers.Util.modifyDOMElement(
container, null, null, null, null, null, null, opacity
);
@@ -347,7 +205,7 @@ OpenLayers.Layer.Google = OpenLayers.Class(
this.setGMapVisibility(false);
var cache = OpenLayers.Layer.Google.cache[this.map.id];
if (cache && cache.count <= 1) {
this.removeGMapElements(false);
this.removeGMapElements();
}
}
OpenLayers.Layer.EventPane.prototype.destroy.apply(this, arguments);
@@ -362,7 +220,7 @@ OpenLayers.Layer.Google = OpenLayers.Class(
var cache = OpenLayers.Layer.Google.cache[this.map.id];
if (cache) {
// remove shared elements from dom
var container = this.mapObject && this.mapObject.getContainer();
var container = this.mapObject && this.getMapContainer();
if (container && container.parentNode) {
container.parentNode.removeChild(container);
}
@@ -408,33 +266,6 @@ OpenLayers.Layer.Google = OpenLayers.Class(
OpenLayers.Layer.EventPane.prototype.removeMap.apply(this, arguments);
},
/**
* APIMethod: getZoomForExtent
*
* Parameters:
* bounds - {<OpenLayers.Bounds>}
*
* Returns:
* {Integer} Corresponding zoom level for a specified Bounds.
* If mapObject is not loaded or not centered, returns null
*
getZoomForExtent: function (bounds) {
var zoom = null;
if (this.mapObject != null) {
var moBounds = this.getMapObjectBoundsFromOLBounds(bounds);
var moZoom = this.getMapObjectZoomFromMapObjectBounds(moBounds);
//make sure zoom is within bounds
var moZoom = Math.min(Math.max(moZoom, this.minZoomLevel),
this.maxZoomLevel);
zoom = this.getOLZoomFromMapObjectZoom(moZoom);
}
return zoom;
},
*/
//
// TRANSLATION: MapObject Bounds <-> OpenLayers.Bounds
//
@@ -470,62 +301,6 @@ OpenLayers.Layer.Google = OpenLayers.Class(
return olBounds;
},
/**
* APIMethod: getMapObjectBoundsFromOLBounds
*
* Parameters:
* olBounds - {<OpenLayers.Bounds>}
*
* Returns:
* {Object} A MapObject Bounds, translated from olBounds
* Returns null if null value is passed in
*/
getMapObjectBoundsFromOLBounds: function(olBounds) {
var moBounds = null;
if (olBounds != null) {
var sw = this.sphericalMercator ?
this.inverseMercator(olBounds.bottom, olBounds.left) :
new OpenLayers.LonLat(olBounds.bottom, olBounds.left);
var ne = this.sphericalMercator ?
this.inverseMercator(olBounds.top, olBounds.right) :
new OpenLayers.LonLat(olBounds.top, olBounds.right);
moBounds = new GLatLngBounds(new GLatLng(sw.lat, sw.lon),
new GLatLng(ne.lat, ne.lon));
}
return moBounds;
},
/**
* Method: addContainerPxFunction
* Hack-on function because GMAPS does not give it to us
*
* Parameters:
* gLatLng - {GLatLng}
*
* Returns:
* {GPoint} A GPoint specifying gLatLng translated into "Container" coords
*/
addContainerPxFunction: function() {
if ( (typeof GMap2 != "undefined") &&
!GMap2.prototype.fromLatLngToContainerPixel) {
GMap2.prototype.fromLatLngToContainerPixel = function(gLatLng) {
// first we translate into "DivPixel"
var gPoint = this.fromLatLngToDivPixel(gLatLng);
// locate the sliding "Div" div
var div = this.getContainer().firstChild.firstChild;
// adjust by the offset of "Div" and voila!
gPoint.x += div.offsetLeft;
gPoint.y += div.offsetTop;
return gPoint;
};
}
},
/**
* APIMethod: getWarningHTML
*
@@ -547,29 +322,6 @@ OpenLayers.Layer.Google = OpenLayers.Class(
// Get&Set Center, Zoom
/**
* APIMethod: setMapObjectCenter
* Set the mapObject to the specified center and zoom
*
* Parameters:
* center - {Object} MapObject LonLat format
* zoom - {int} MapObject zoom format
*/
setMapObjectCenter: function(center, zoom) {
this.mapObject.setCenter(center, zoom);
},
/**
* APIMethod: dragPanMapObject
*
* Parameters:
* dX - {Integer}
* dY - {Integer}
*/
dragPanMapObject: function(dX, dY) {
this.dragObject.moveBy(new GSize(-dX, dY));
},
/**
* APIMethod: getMapObjectCenter
*
@@ -590,51 +342,7 @@ OpenLayers.Layer.Google = OpenLayers.Class(
return this.mapObject.getZoom();
},
// LonLat - Pixel Translation
/**
* APIMethod: getMapObjectLonLatFromMapObjectPixel
*
* Parameters:
* moPixel - {Object} MapObject Pixel format
*
* Returns:
* {Object} MapObject LonLat translated from MapObject Pixel
*/
getMapObjectLonLatFromMapObjectPixel: function(moPixel) {
return this.mapObject.fromContainerPixelToLatLng(moPixel);
},
/**
* APIMethod: getMapObjectPixelFromMapObjectLonLat
*
* Parameters:
* moLonLat - {Object} MapObject LonLat format
*
* Returns:
* {Object} MapObject Pixel transtlated from MapObject LonLat
*/
getMapObjectPixelFromMapObjectLonLat: function(moLonLat) {
return this.mapObject.fromLatLngToContainerPixel(moLonLat);
},
// Bounds
/**
* APIMethod: getMapObjectZoomFromMapObjectBounds
*
* Parameters:
* moBounds - {Object} MapObject Bounds format
*
* Returns:
* {Object} MapObject Zoom for specified MapObject Bounds
*/
getMapObjectZoomFromMapObjectBounds: function(moBounds) {
return this.mapObject.getBoundsZoomLevel(moBounds);
},
/************************************
* *
* MapObject Primitives *
@@ -675,27 +383,6 @@ OpenLayers.Layer.Google = OpenLayers.Class(
return lat;
},
/**
* APIMethod: getMapObjectLonLatFromLonLat
*
* Parameters:
* lon - {Float}
* lat - {Float}
*
* Returns:
* {Object} MapObject LonLat built from lon and lat params
*/
getMapObjectLonLatFromLonLat: function(lon, lat) {
var gLatLng;
if(this.sphericalMercator) {
var lonlat = this.inverseMercator(lon, lat);
gLatLng = new GLatLng(lonlat.lat, lonlat.lon);
} else {
gLatLng = new GLatLng(lat, lon);
}
return gLatLng;
},
// Pixel
/**
@@ -723,21 +410,7 @@ OpenLayers.Layer.Google = OpenLayers.Class(
getYFromMapObjectPixel: function(moPixel) {
return moPixel.y;
},
/**
* APIMethod: getMapObjectPixelFromXY
*
* Parameters:
* x - {Integer}
* y - {Integer}
*
* Returns:
* {Object} MapObject Pixel from x and y parameters
*/
getMapObjectPixelFromXY: function(x, y) {
return new GPoint(x, y);
},
CLASS_NAME: "OpenLayers.Layer.Google"
});