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

View File

@@ -7,7 +7,6 @@ OpenLayers.js
[last]
[exclude]
OpenLayers/Layer/Google.js
OpenLayers/Layer/Yahoo.js
OpenLayers/Layer/VirtualEarth.js
OpenLayers/Layer/WMS/Untiled.js

View File

@@ -64,7 +64,7 @@ if (typeof(_OPENLAYERS_SFL_) == "undefined") {
"OpenLayers/Feature/WFS.js",
"OpenLayers/Tile/Image.js",
"OpenLayers/Tile/WFS.js",
// "OpenLayers/Layer/Google.js",
"OpenLayers/Layer/Google.js",
// "OpenLayers/Layer/VirtualEarth.js",
// "OpenLayers/Layer/Yahoo.js",
"OpenLayers/Layer/HTTPRequest.js",

View File

@@ -3,10 +3,6 @@
* text of the license. */
// @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
*/
@@ -58,7 +54,7 @@ OpenLayers.Layer.Google.prototype = Object.extend( new OpenLayers.Layer(), {
* @type Boolean
*/
isBaseLayer: function() {
return true;
return (this.gmap != null);
},
/**
@@ -95,34 +91,38 @@ OpenLayers.Layer.Google.prototype = Object.extend( new OpenLayers.Layer(), {
*/
loadGMap:function() {
// create GMap, hide nav controls
this.gmap = new GMap2(this.div);
// this causes the GMap to set itself to Map's center/zoom
this.moveTo();
// catch pans and zooms from GMap
GEvent.addListener(this.gmap,
"moveend",
this.catchPanZoom.bindAsEventListener(this));
// attach to the drag start and end and we<77>ll set a flag so that
// we dont get recursivity. this is because when we call setCenter(),
// it calls moveTo() on all layers
GEvent.addListener(this.gmap,
"dragstart",
this.dragStart.bindAsEventListener(this));
GEvent.addListener(this.gmap,
"dragend",
this.dragEnd.bindAsEventListener(this));
// catch pans and zooms from GMap
GEvent.addListener(this.gmap,
"drag",
this.catchPanZoom.bindAsEventListener(this));
//test if the gmaps library has been loaded
var gmapsLoaded = (typeof GMap2) != "undefined";
if (gmapsLoaded) {
// create GMap, hide nav controls
this.gmap = new GMap2(this.div);
// this causes the GMap to set itself to Map's center/zoom
this.moveTo();
// catch pans and zooms from GMap
GEvent.addListener(this.gmap,
"moveend",
this.catchPanZoom.bindAsEventListener(this));
// attach to the drag start and end and we<77>ll set a flag so that
// we dont get recursivity. this is because when we call setCenter(),
// it calls moveTo() on all layers
GEvent.addListener(this.gmap,
"dragstart",
this.dragStart.bindAsEventListener(this));
GEvent.addListener(this.gmap,
"dragend",
this.dragEnd.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
* OpenLayers.Pixel, translated into lon/lat by GMAPS
* If gmap is not loaded, returns null.
* @type OpenLayers.LonLat
*/
getLonLatFromViewPortPx: function (viewPortPx) {
var gPoint = this.getGPointFromOLPixel(viewPortPx);
var gLatLng = this.gmap.fromContainerPixelToLatLng(gPoint)
return this.getOLLonLatFromGLatLng(gLatLng);
var lonlat = null;
if (this.gmap != null) {
var gPoint = this.getGPointFromOLPixel(viewPortPx);
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,
* translated into view port pixels BY GMAPS
* If gmap is not loaded, returns null.
* @type OpenLayers.Pixel
*/
getViewPortPxFromLonLat: function (lonlat) {
var gLatLng = this.getGLatLngFromOLLonLat(lonlat);
// note we use special hacked function here,
// because GMaps doesnt give it to us
var gPoint = this.fromLatLngToContainerPixel(gLatLng);
return this.getOLPixelFromGPoint(gPoint);
getViewPortPxFromLonLat: function (lonlat) {
var viewPortPx = null;
if (this.gmap != null) {
var gLatLng = this.getGLatLngFromOLLonLat(lonlat);
// note we use special hacked function here,
// because GMaps doesnt give it to us
var gPoint = this.fromLatLngToContainerPixel(gLatLng);
viewPortPx = this.getOLPixelFromGPoint(gPoint);
}
return viewPortPx;
},
/** 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
*
* @return {int}
* @returns Corresponding zoom level for a specified Bounds.
* If gmap is not loaded, returns null.
* @type int
*/
getZoomForExtent: function (bounds) {
var gBounds = this.getGLatLngBoundsFromOLBounds(bounds);
var gZoom = this.gmap.getBoundsZoomLevel(gBounds);
return this.getOLZoomFromGZoom(gZoom);
var zoom = null;
if (this.gmap != null) {
var gBounds = this.getGLatLngBoundsFromOLBounds(bounds);
var gZoom = this.gmap.getBoundsZoomLevel(gBounds);
zoom = this.getOLZoomFromGZoom(gZoom);
}
return zoom;
},
/**
* @returns A Bounds object which represents the lon/lat bounds of the
* current viewPort.
* If gmap is not loaded, returns null
* @type OpenLayers.Bounds
*/
getExtent: function () {
if (this.gmap.getCenter() == null) {
this.moveTo();
var extent = null;
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 this.getOLBoundsFromGLatLngBounds(gLatLngBounds);
return extent;
},
/********************************************************/