From b9fc7693dbb12a44721988226a0363b27fe294e8 Mon Sep 17 00:00:00 2001 From: "Michael J. Rubinsky" Date: Thu, 17 Jun 2010 01:42:59 -0400 Subject: [PATCH] Tweak map zoom level handling. Save the current zoom level when saving event so we can return to it when viewing/editing later. Also, base the zoom level after geocoding on the precision of the geocoding results if we have have the information (only google geocoder returns this). --- kronolith/js/kronolith.js | 32 ++++++++++++++++------ kronolith/lib/Geo/Mysql.php | 12 +++++--- kronolith/lib/Geo/Sql.php | 15 ++++++---- kronolith/scripts/upgrades/2010-06-16_add_zoom.sql | 1 + kronolith/templates/index/edit.inc | 1 + 5 files changed, 43 insertions(+), 18 deletions(-) create mode 100644 kronolith/scripts/upgrades/2010-06-16_add_zoom.sql diff --git a/kronolith/js/kronolith.js b/kronolith/js/kronolith.js index 2c3af37ce..77c11f0fb 100644 --- a/kronolith/js/kronolith.js +++ b/kronolith/js/kronolith.js @@ -4760,6 +4760,9 @@ KronolithCore = { this.eventTagAc.shutdown(); $('kronolithEventSave').disable(); $('kronolithEventSaveAsNew').disable(); + if (this.mapInitialized) { + $('kronolithEventMapZoom').value = this.map.getZoom(); + } this.startLoading(target, start + end); this.doAction('saveEvent', $H($('kronolithEventForm').serialize({ hash: true })) @@ -4984,6 +4987,7 @@ KronolithCore = { if (ev.gl) { $('kronolithEventLocationLat').value = ev.gl.lat; $('kronolithEventLocationLon').value = ev.gl.lon; + $('kronolithEventMapZoom').value = ev.gl.zoom; } if (!ev.pe) { @@ -5465,9 +5469,9 @@ KronolithCore = { if ($('kronolithEventLocationLat').value) { var ll = { lat:$('kronolithEventLocationLat').value, lon: $('kronolithEventLocationLon').value }; - - // @TODO: a default/configurable default zoom level? - this.placeMapMarker(ll, true, 8); + // Note that we need to cast the value of zoom to an integer here, + // otherwise the map display breaks. + this.placeMapMarker(ll, true, $('kronolithEventMapZoom').value - 0); } //@TODO: check for Location field - and if present, but no lat/lon value, attempt to // geocode it. @@ -5480,6 +5484,7 @@ KronolithCore = { this.mapInitialized = false; $('kronolithEventLocationLat').value = null; $('kronolithEventLocationLon').value = null; + $('kronolithEventMapZoom').value = null; if (this.mapMarker) { this.map.removeMarker(this.mapMarker); this.mapMarker = null; @@ -5527,13 +5532,16 @@ KronolithCore = { /** * Callback for geocoding calls. - * @TODO: Figure out the proper zoom level based on the detail of the - * provided address. */ onGeocode: function(r) { r = r.shift(); - this.placeMapMarker({ lat: r.lat, lon: r.lon }, true); + if (r.precision) { + zoom = r.precision * 2; + } else { + zoom = null; + } + this.placeMapMarker({ lat: r.lat, lon: r.lon }, true, zoom); }, geocode: function(a) { @@ -5545,8 +5553,10 @@ KronolithCore = { }, /** - * Place the event marker on the map, ensuring it exists. - * See note in onGeocode about zoomlevel + * Place the event marker on the map, at point ll, ensuring it exists. + * Optionally center the map on the marker and zoom. Zoom only honored if + * center is set, and if center is set, but zoom is null, we zoomToFit(). + * */ placeMapMarker: function(ll, center, zoom) { @@ -5565,6 +5575,9 @@ KronolithCore = { $('kronolithEventLocationLat').value = ll.lat; if (center) { this.map.setCenter(ll, zoom); + if (!zoom) { + this.map.zoomToFit(); + } } }, @@ -5583,6 +5596,9 @@ KronolithCore = { this.mapMarker = false; }, + /** + * Ensures the map tab is visible and sets UI elements accordingly. + */ ensureMap: function() { if (!this.mapInitialized) { diff --git a/kronolith/lib/Geo/Mysql.php b/kronolith/lib/Geo/Mysql.php index 92cd4867e..e0c20fe9e 100644 --- a/kronolith/lib/Geo/Mysql.php +++ b/kronolith/lib/Geo/Mysql.php @@ -44,15 +44,19 @@ class Kronolith_Geo_Mysql extends Kronolith_Geo_Sql return; } + if (empty($point['zoom'])) { + $point['zoom'] = 0; + } + /* INSERT or UPDATE */ if ($count) { - $sql = sprintf('UPDATE kronolith_events_geo SET event_coordinates = GeomFromText(\'POINT(%F %F)\') WHERE event_id = ?', $point['lat'], $point['lon']); + $sql = sprintf('UPDATE kronolith_events_geo SET event_coordinates = GeomFromText(\'POINT(%F %F)\'), event_zoom = ? WHERE event_id = ?', $point['lat'], $point['lon']); } else { - $sql = sprintf('INSERT into kronolith_events_geo (event_id, event_coordinates) VALUES(?, GeomFromText(\'POINT(%F %F)\'))', $point['lat'], $point['lon']); + $sql = sprintf('INSERT into kronolith_events_geo (event_id, event_coordinates, event_zoom) VALUES(?, GeomFromText(\'POINT(%F %F)\'), ?)', $point['lat'], $point['lon']); } Horde::logMessage(sprintf('Kronolith_Geo_Mysql::setLocation(): user = "%s"; query = "%s"; values = "%s"', $GLOBALS['registry']->getAuth(), $sql, $event_id), 'DEBUG'); - $result = $this->_write_db->query($sql, array($event_id)); + $result = $this->_write_db->query($sql, array($event_id, $point['zoom'])); if ($result instanceof PEAR_Error) { Horde::logMessage($result, 'ERR'); throw new Horde_Exception($result); @@ -69,7 +73,7 @@ class Kronolith_Geo_Mysql extends Kronolith_Geo_Sql */ public function getLocation($event_id) { - $sql = 'SELECT x(event_coordinates) as lat, y(event_coordinates) as lon FROM kronolith_events_geo WHERE event_id = ?'; + $sql = 'SELECT x(event_coordinates) as lat, y(event_coordinates) as lon, event_zoom as zoom FROM kronolith_events_geo WHERE event_id = ?'; Horde::logMessage(sprintf('Kronolith_Geo_Mysql::getLocation(): user = "%s"; query = "%s"; values = "%s"', $GLOBALS['registry']->getAuth(), $sql, $event_id), 'DEBUG'); $result = $this->_db->getRow($sql, array($event_id), DB_FETCHMODE_ASSOC); diff --git a/kronolith/lib/Geo/Sql.php b/kronolith/lib/Geo/Sql.php index a3bf94462..5b12b92f0 100644 --- a/kronolith/lib/Geo/Sql.php +++ b/kronolith/lib/Geo/Sql.php @@ -96,8 +96,7 @@ class Kronolith_Geo_Sql extends Kronolith_Geo } /* Do we actually have data? If not, see if we are deleting an - * existing entry. - */ + * existing entry. */ if ((empty($point['lat']) || empty($point['lon'])) && $count) { // Delete the record. $this->deleteLocation($event_id); @@ -106,12 +105,16 @@ class Kronolith_Geo_Sql extends Kronolith_Geo return; } + if (empty($point['zoom'])) { + $point['zoom'] = 0; + } + /* INSERT or UPDATE */ - $params = array($point['lat'], $point['lon'], $event_id); + $params = array($point['lat'], $point['lon'], $point['zoom'], $event_id); if ($count) { - $sql = 'UPDATE kronolith_events_geo SET event_lat = ?, event_lon = ? WHERE event_id = ?'; + $sql = 'UPDATE kronolith_events_geo SET event_lat = ?, event_lon = ?, event_zoom = ? WHERE event_id = ?'; } else { - $sql = 'INSERT into kronolith_events_geo (event_lat, event_lon, event_id) VALUES(?, ?, ?)'; + $sql = 'INSERT into kronolith_events_geo (event_lat, event_lon, event_zoom, event_id) VALUES(?, ?, ?, ?)'; } Horde::logMessage(sprintf('Kronolith_Geo_Sql::setLocation(): user = "%s"; query = "%s"; values = "%s"', $GLOBALS['registry']->getAuth(), $sql, print_r($params, true)), 'DEBUG'); @@ -132,7 +135,7 @@ class Kronolith_Geo_Sql extends Kronolith_Geo */ public function getLocation($event_id) { - $sql = 'SELECT event_lat as lat, event_lon as lon FROM kronolith_events_geo WHERE event_id = ?'; + $sql = 'SELECT event_lat as lat, event_lon as lon, event_zoom as zoom FROM kronolith_events_geo WHERE event_id = ?'; Horde::logMessage(sprintf('Kronolith_Geo_Sql::getLocation(): user = "%s"; query = "%s"; values = "%s"', $GLOBALS['registry']->getAuth(), $sql, $event_id), 'DEBUG'); $result = $this->_db->getRow($sql, array($event_id), DB_FETCHMODE_ASSOC); diff --git a/kronolith/scripts/upgrades/2010-06-16_add_zoom.sql b/kronolith/scripts/upgrades/2010-06-16_add_zoom.sql new file mode 100644 index 000000000..716183b1b --- /dev/null +++ b/kronolith/scripts/upgrades/2010-06-16_add_zoom.sql @@ -0,0 +1 @@ +ALTER TABLE kronolith_events_geo ADD COLUMN event_zoom INTEGER DEFAULT 0 NOT NULL; diff --git a/kronolith/templates/index/edit.inc b/kronolith/templates/index/edit.inc index 3f559ac93..b94826597 100644 --- a/kronolith/templates/index/edit.inc +++ b/kronolith/templates/index/edit.inc @@ -4,6 +4,7 @@ +