2 * Copyright 2008 Google Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package com.google.zxing.client.result;
19 import com.google.zxing.Result;
22 * Represents a "geo:" URI result, which specifices a location on the surface of
23 * the Earth as well as an optional altitude above the surface. See
24 * <a href="http://tools.ietf.org/html/draft-mayrhofer-geo-uri-00">
25 * http://tools.ietf.org/html/draft-mayrhofer-geo-uri-00</a>.
27 * @author srowen@google.com (Sean Owen)
29 public final class GeoParsedResult extends ParsedReaderResult {
31 private final String geoURI;
32 private final float latitude;
33 private final float longitude;
34 private final float altitude;
36 private GeoParsedResult(String geoURI, float latitude, float longitude, float altitude) {
37 super(ParsedReaderResultType.GEO);
39 this.latitude = latitude;
40 this.longitude = longitude;
41 this.altitude = altitude;
44 public static GeoParsedResult parse(Result result) {
45 String rawText = result.getText();
46 if (rawText == null || !rawText.startsWith("geo:")) {
49 // Drop geo, query portion
50 int queryStart = rawText.indexOf('?', 4);
52 rawText = rawText.substring(4);
54 rawText = rawText.substring(4, queryStart);
56 int latitudeEnd = rawText.indexOf(',');
57 if (latitudeEnd < 0) {
60 float latitude = Float.parseFloat(rawText.substring(0, latitudeEnd));
61 int longitudeEnd = rawText.indexOf(',', latitudeEnd + 1);
63 float altitude; // in meters
64 if (longitudeEnd < 0) {
65 longitude = Float.parseFloat(rawText.substring(latitudeEnd + 1));
68 longitude = Float.parseFloat(rawText.substring(latitudeEnd + 1, longitudeEnd));
69 altitude = Float.parseFloat(rawText.substring(longitudeEnd + 1));
71 return new GeoParsedResult(rawText, latitude, longitude, altitude);
74 public String getGeoURI() {
79 * @return latitude in degrees
81 public float getLatitude() {
86 * @return longitude in degrees
88 public float getLongitude() {
93 * @return altitude in meters. If not specified, in the geo URI, returns 0.0
95 public float getAltitude() {
99 public String getDisplayResult() {
100 StringBuffer result = new StringBuffer(50);
101 result.append(latitude);
102 result.append(" deg N, ");
103 result.append(longitude);
104 result.append(" deg E");
105 if (altitude > 0.0f) {
107 result.append(altitude);
110 return result.toString();
114 * @return a URI link to Google Maps which display the point on the Earth described
115 * by this instance, and sets the zoom level in a way that roughly reflects the
116 * altitude, if specified
119 public String getGoogleMapsURI() {
120 StringBuffer result = new StringBuffer(50);
121 result.append("http://maps.google.com/?ll=");
122 result.append(latitude);
124 result.append(longitude);
125 if (altitude > 0.0f) {
126 // Map altitude to zoom level, cleverly. Roughly, zoom level 19 is like a
127 // view from 1000ft, 18 is like 2000ft, 17 like 4000ft, and so on.
128 float altitudeInFeet = altitude * 3.28f;
129 int altitudeInKFeet = (int) (altitudeInFeet / 1000.0f);
130 // No Math.log() available here, so compute log base 2 the old fashioned way
131 // Here logBaseTwo will take on a value between 0 and 18 actually
133 while (altitudeInKFeet > 1 && logBaseTwo < 18) {
134 altitudeInKFeet >>= 1;
137 int zoom = 19 - logBaseTwo;
138 result.append("&z=");
141 return result.toString();