Tweak map zoom level handling.
authorMichael J. Rubinsky <mrubinsk@horde.org>
Thu, 17 Jun 2010 05:42:59 +0000 (01:42 -0400)
committerMichael J. Rubinsky <mrubinsk@horde.org>
Thu, 17 Jun 2010 05:42:59 +0000 (01:42 -0400)
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
kronolith/lib/Geo/Mysql.php
kronolith/lib/Geo/Sql.php
kronolith/scripts/upgrades/2010-06-16_add_zoom.sql [new file with mode: 0644]
kronolith/templates/index/edit.inc

index 2c3af37..77c11f0 100644 (file)
@@ -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) {
index 92cd486..e0c20fe 100644 (file)
@@ -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);
index a3bf944..5b12b92 100644 (file)
@@ -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 (file)
index 0000000..716183b
--- /dev/null
@@ -0,0 +1 @@
+ALTER TABLE kronolith_events_geo ADD COLUMN event_zoom INTEGER DEFAULT 0 NOT NULL;
index 3f559ac..b948265 100644 (file)
@@ -4,6 +4,7 @@
 <input id="kronolithEventCalendar" type="hidden" name="cal" />
 <input id="kronolithEventLocationLon" type="hidden" name="lon" />
 <input id="kronolithEventLocationLat" type="hidden" name="lat" />
+<input id="kronolithEventMapZoom" type="hidden" name="zoom" />
 
 <div>
   <label for="kronolithEventTitle">