From 10d55bd72ae94c44a3021308fafc62262b21de78 Mon Sep 17 00:00:00 2001 From: euzuro Date: Fri, 7 Jul 2006 12:55:39 +0000 Subject: [PATCH] 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 --- build/library.cfg | 1 - lib/OpenLayers.js | 2 +- lib/OpenLayers/Layer/Google.js | 127 +++++++++++++++++++-------------- 3 files changed, 74 insertions(+), 56 deletions(-) diff --git a/build/library.cfg b/build/library.cfg index 20b2a21470..b78030cb2c 100644 --- a/build/library.cfg +++ b/build/library.cfg @@ -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 diff --git a/lib/OpenLayers.js b/lib/OpenLayers.js index 440660b320..8ccc8839f0 100644 --- a/lib/OpenLayers.js +++ b/lib/OpenLayers.js @@ -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", diff --git a/lib/OpenLayers/Layer/Google.js b/lib/OpenLayers/Layer/Google.js index da013bec31..023b8011f2 100644 --- a/lib/OpenLayers/Layer/Google.js +++ b/lib/OpenLayers/Layer/Google.js @@ -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(""); - /** * @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Ž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Ž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; }, /********************************************************/