remove document.write() call that loaded the gmaps script in google.js. from now on, the user is responsible for adding that script tag to his/her page. Add a test to make sure gmaps code base is loaded before trying to initialize new gmap2. update isBaseLayer() to return true only if gmaps is correctly loaded. finally, protect all methods if this.gmap is null, do nothing. update OpenLayers.js to now include Google.js and library.cfg to not exclude it.

git-svn-id: http://svn.openlayers.org/trunk/openlayers@910 dc9f47b5-9b13-0410-9fdd-eb0c1a62fdaf
This commit is contained in:
euzuro
2006-07-07 12:55:39 +00:00
parent 3b1545eadc
commit 10d55bd72a
3 changed files with 74 additions and 56 deletions
-1
View File
@@ -7,7 +7,6 @@ OpenLayers.js
[last] [last]
[exclude] [exclude]
OpenLayers/Layer/Google.js
OpenLayers/Layer/Yahoo.js OpenLayers/Layer/Yahoo.js
OpenLayers/Layer/VirtualEarth.js OpenLayers/Layer/VirtualEarth.js
OpenLayers/Layer/WMS/Untiled.js OpenLayers/Layer/WMS/Untiled.js
+1 -1
View File
@@ -64,7 +64,7 @@ if (typeof(_OPENLAYERS_SFL_) == "undefined") {
"OpenLayers/Feature/WFS.js", "OpenLayers/Feature/WFS.js",
"OpenLayers/Tile/Image.js", "OpenLayers/Tile/Image.js",
"OpenLayers/Tile/WFS.js", "OpenLayers/Tile/WFS.js",
// "OpenLayers/Layer/Google.js", "OpenLayers/Layer/Google.js",
// "OpenLayers/Layer/VirtualEarth.js", // "OpenLayers/Layer/VirtualEarth.js",
// "OpenLayers/Layer/Yahoo.js", // "OpenLayers/Layer/Yahoo.js",
"OpenLayers/Layer/HTTPRequest.js", "OpenLayers/Layer/HTTPRequest.js",
+73 -54
View File
@@ -3,10 +3,6 @@
* text of the license. */ * text of the license. */
// @require: OpenLayers/Layer.js // @require: OpenLayers/Layer.js
// load Google map control script
// this key was generated for: http://openlayers.python-hosting.com/testing/euzuro/
document.write("<script src='http://maps.google.com/maps?file=api&amp;v=2&amp;key=ABQIAAAAmQ3udCHPQVB_9T_edFZ7YRRRlP-tOiFgaSzksg_0w1dphL9c5BTfdJMKT91b0UJGibNcWEM0Q5-O1w'></script>");
/** /**
* @class * @class
*/ */
@@ -58,7 +54,7 @@ OpenLayers.Layer.Google.prototype = Object.extend( new OpenLayers.Layer(), {
* @type Boolean * @type Boolean
*/ */
isBaseLayer: function() { isBaseLayer: function() {
return true; return (this.gmap != null);
}, },
/** /**
@@ -95,34 +91,38 @@ OpenLayers.Layer.Google.prototype = Object.extend( new OpenLayers.Layer(), {
*/ */
loadGMap:function() { loadGMap:function() {
// create GMap, hide nav controls //test if the gmaps library has been loaded
this.gmap = new GMap2(this.div); var gmapsLoaded = (typeof GMap2) != "undefined";
if (gmapsLoaded) {
// this causes the GMap to set itself to Map's center/zoom
this.moveTo(); // create GMap, hide nav controls
this.gmap = new GMap2(this.div);
// catch pans and zooms from GMap
GEvent.addListener(this.gmap, // this causes the GMap to set itself to Map's center/zoom
"moveend", this.moveTo();
this.catchPanZoom.bindAsEventListener(this));
// catch pans and zooms from GMap
GEvent.addListener(this.gmap,
// attach to the drag start and end and we´ll set a flag so that "moveend",
// we dont get recursivity. this is because when we call setCenter(), this.catchPanZoom.bindAsEventListener(this));
// it calls moveTo() on all layers
GEvent.addListener(this.gmap,
"dragstart", // attach to the drag start and end and we´ll set a flag so that
this.dragStart.bindAsEventListener(this)); // we dont get recursivity. this is because when we call setCenter(),
// it calls moveTo() on all layers
GEvent.addListener(this.gmap, GEvent.addListener(this.gmap,
"dragend", "dragstart",
this.dragEnd.bindAsEventListener(this)); this.dragStart.bindAsEventListener(this));
// catch pans and zooms from GMap GEvent.addListener(this.gmap,
GEvent.addListener(this.gmap, "dragend",
"drag", this.dragEnd.bindAsEventListener(this));
this.catchPanZoom.bindAsEventListener(this));
// catch pans and zooms from GMap
GEvent.addListener(this.gmap,
"drag",
this.catchPanZoom.bindAsEventListener(this));
}
}, },
/** /**
@@ -161,13 +161,17 @@ OpenLayers.Layer.Google.prototype = Object.extend( new OpenLayers.Layer(), {
* *
* @returns An OpenLayers.LonLat which is the passed-in view port * @returns An OpenLayers.LonLat which is the passed-in view port
* OpenLayers.Pixel, translated into lon/lat by GMAPS * OpenLayers.Pixel, translated into lon/lat by GMAPS
* If gmap is not loaded, returns null.
* @type OpenLayers.LonLat * @type OpenLayers.LonLat
*/ */
getLonLatFromViewPortPx: function (viewPortPx) { getLonLatFromViewPortPx: function (viewPortPx) {
var gPoint = this.getGPointFromOLPixel(viewPortPx); var lonlat = null;
var gLatLng = this.gmap.fromContainerPixelToLatLng(gPoint) if (this.gmap != null) {
var gPoint = this.getGPointFromOLPixel(viewPortPx);
return this.getOLLonLatFromGLatLng(gLatLng); var gLatLng = this.gmap.fromContainerPixelToLatLng(gPoint)
lonlat = this.getOLLonLatFromGLatLng(gLatLng);
}
return lonlat;
}, },
@@ -176,16 +180,21 @@ OpenLayers.Layer.Google.prototype = Object.extend( new OpenLayers.Layer(), {
* *
* @returns An OpenLayers.Pixel which is the passed-in OpenLayers.LonLat, * @returns An OpenLayers.Pixel which is the passed-in OpenLayers.LonLat,
* translated into view port pixels BY GMAPS * translated into view port pixels BY GMAPS
* If gmap is not loaded, returns null.
* @type OpenLayers.Pixel * @type OpenLayers.Pixel
*/ */
getViewPortPxFromLonLat: function (lonlat) { getViewPortPxFromLonLat: function (lonlat) {
var gLatLng = this.getGLatLngFromOLLonLat(lonlat); var viewPortPx = null;
if (this.gmap != null) {
// note we use special hacked function here, var gLatLng = this.getGLatLngFromOLLonLat(lonlat);
// because GMaps doesnt give it to us
var gPoint = this.fromLatLngToContainerPixel(gLatLng); // note we use special hacked function here,
// because GMaps doesnt give it to us
return this.getOLPixelFromGPoint(gPoint); var gPoint = this.fromLatLngToContainerPixel(gLatLng);
viewPortPx = this.getOLPixelFromGPoint(gPoint);
}
return viewPortPx;
}, },
/** Hacked function because GMAPS does not give us /** Hacked function because GMAPS does not give us
@@ -217,26 +226,36 @@ OpenLayers.Layer.Google.prototype = Object.extend( new OpenLayers.Layer(), {
/** /**
* @param {OpenLayers.Bounds} bounds * @param {OpenLayers.Bounds} bounds
* *
* @return {int} * @returns Corresponding zoom level for a specified Bounds.
* If gmap is not loaded, returns null.
* @type int
*/ */
getZoomForExtent: function (bounds) { getZoomForExtent: function (bounds) {
var gBounds = this.getGLatLngBoundsFromOLBounds(bounds); var zoom = null;
var gZoom = this.gmap.getBoundsZoomLevel(gBounds); if (this.gmap != null) {
var gBounds = this.getGLatLngBoundsFromOLBounds(bounds);
return this.getOLZoomFromGZoom(gZoom); var gZoom = this.gmap.getBoundsZoomLevel(gBounds);
zoom = this.getOLZoomFromGZoom(gZoom);
}
return zoom;
}, },
/** /**
* @returns A Bounds object which represents the lon/lat bounds of the * @returns A Bounds object which represents the lon/lat bounds of the
* current viewPort. * current viewPort.
* If gmap is not loaded, returns null
* @type OpenLayers.Bounds * @type OpenLayers.Bounds
*/ */
getExtent: function () { getExtent: function () {
if (this.gmap.getCenter() == null) { var extent = null;
this.moveTo(); if (this.gmap != null) {
if (this.gmap.getCenter() == null) {
this.moveTo();
}
var gLatLngBounds = this.gmap.getBounds();
extent = this.getOLBoundsFromGLatLngBounds(gLatLngBounds);
} }
var gLatLngBounds = this.gmap.getBounds(); return extent;
return this.getOLBoundsFromGLatLngBounds(gLatLngBounds);
}, },
/********************************************************/ /********************************************************/