Add client support for geo URIs
authorsrowen <srowen@59b500cc-1b3d-0410-9834-0bbf25fbcc57>
Mon, 17 Mar 2008 15:16:59 +0000 (15:16 +0000)
committersrowen <srowen@59b500cc-1b3d-0410-9834-0bbf25fbcc57>
Mon, 17 Mar 2008 15:16:59 +0000 (15:16 +0000)
git-svn-id: http://zxing.googlecode.com/svn/trunk@285 59b500cc-1b3d-0410-9834-0bbf25fbcc57

android/src/com/google/zxing/client/android/ResultHandler.java
core/src/com/google/zxing/client/result/GeoParsedResult.java
javame/src/com/google/zxing/client/j2me/ZXingMIDlet.java

index 0950af1..b1bde8b 100755 (executable)
@@ -26,6 +26,7 @@ import com.google.zxing.client.result.AddressBookDoCoMoResult;
 import com.google.zxing.client.result.BookmarkDoCoMoResult;
 import com.google.zxing.client.result.EmailAddressResult;
 import com.google.zxing.client.result.EmailDoCoMoResult;
+import com.google.zxing.client.result.GeoParsedResult;
 import com.google.zxing.client.result.ParsedReaderResult;
 import com.google.zxing.client.result.ParsedReaderResultType;
 import com.google.zxing.client.result.UPCParsedResult;
@@ -103,6 +104,13 @@ final class ResultHandler extends Handler {
         } catch (URISyntaxException e) {
           return;
         }
+      } else if (type.equals(ParsedReaderResultType.GEO)) {
+        GeoParsedResult geoResult = (GeoParsedResult) result;
+        try {
+          intent = new Intent(Intent.SENDTO_ACTION, new ContentURI(geoResult.getGoogleMapsURI()));
+        } catch (URISyntaxException e) {
+          return;
+        }
       } else if (type.equals(ParsedReaderResultType.UPC)) {
         UPCParsedResult upcResult = (UPCParsedResult) result;
         try {
index 5162b81..1f68835 100644 (file)
@@ -19,7 +19,8 @@ package com.google.zxing.client.result;
 import com.google.zxing.Result;
 
 /**
- * Represents a "geo:" URI result. See
+ * Represents a "geo:" URI result, which specifices a location on the surface of
+ * the Earth as well as an optional altitude above the surface. See
  * <a href="http://tools.ietf.org/html/draft-mayrhofer-geo-uri-00">
  * http://tools.ietf.org/html/draft-mayrhofer-geo-uri-00</a>.
  *
@@ -57,7 +58,7 @@ public final class GeoParsedResult extends ParsedReaderResult {
     float latitude = Float.parseFloat(rawText.substring(0, latitudeEnd));
     int longitudeEnd = rawText.indexOf(',', latitudeEnd + 1);
     float longitude;
-    float altitude;
+    float altitude; // in meters
     if (longitudeEnd < 0) {
       longitude = Float.parseFloat(rawText.substring(latitudeEnd + 1));
       altitude = 0.0f;
@@ -94,9 +95,42 @@ public final class GeoParsedResult extends ParsedReaderResult {
     result.append(latitude);
     result.append("deg N, ");
     result.append(longitude);
-    result.append("deg E, ");
-    result.append(altitude);
-    result.append('m');
+    result.append("deg E");
+    if (altitude > 0.0f) {
+      result.append(", ");
+      result.append(altitude);
+      result.append('m');
+    }
+    return result.toString();
+  }
+
+  /**
+   * @return a URI link to Google Maps which display the point on the Earth described
+   *  by this instance, and sets the zoom level in a way that roughly reflects the
+   *  altitude, if specified
+   */
+  public String getGoogleMapsURI() {
+    StringBuffer result = new StringBuffer(50);
+    result.append("http://maps.google.com/?ll=");
+    result.append(latitude);
+    result.append(',');
+    result.append(longitude);
+    if (altitude > 0.0f) {
+      // Map altitude to zoom level, cleverly. Roughly, zoom level 19 is like a
+      // view from 1000ft, 18 is like 2000ft, 17 like 4000ft, and so on.
+      float altitudeInFeet = altitude * 3.28f;
+      int altitudeInKFeet = (int) (altitudeInFeet / 1000.0f);
+      // No Math.log() available here, so compute log base 2 the old fashioned way
+      // Here logBaseTwo will take on a value between 0 and 18 actually
+      int logBaseTwo = 0;
+      while (altitudeInKFeet > 1 && logBaseTwo < 18) {
+        altitudeInKFeet >>= 1;
+        logBaseTwo++;
+      }
+      int zoom = 19 - logBaseTwo;
+      result.append("&z=");
+      result.append(zoom);
+    }
     return result.toString();
   }
 
index fffe2ab..1073547 100644 (file)
@@ -20,6 +20,7 @@ import com.google.zxing.Result;
 import com.google.zxing.client.result.BookmarkDoCoMoResult;
 import com.google.zxing.client.result.EmailAddressResult;
 import com.google.zxing.client.result.EmailDoCoMoResult;
+import com.google.zxing.client.result.GeoParsedResult;
 import com.google.zxing.client.result.ParsedReaderResult;
 import com.google.zxing.client.result.ParsedReaderResultType;
 import com.google.zxing.client.result.UPCParsedResult;
@@ -207,6 +208,9 @@ public final class ZXingMIDlet extends MIDlet {
     } else if (type.equals(ParsedReaderResultType.EMAIL_ADDRESS)) {
       String email = ((EmailAddressResult) result).getEmailAddress();
       showOpenURL("Compose E-mail?", email, "mailto:" + email);
+    } else if (type.equals(ParsedReaderResultType.GEO)) {
+      GeoParsedResult geoResult = (GeoParsedResult) result;
+      showOpenURL("Open In Google Maps?", geoResult.getDisplayResult(), geoResult.getGoogleMapsURI());
     } else if (type.equals(ParsedReaderResultType.UPC)) {
       String upc = ((UPCParsedResult) result).getUPC();
       String uri = "http://www.upcdatabase.com/item.asp?upc=" + upc;