Preserve query in geo URI
[zxing.git] / core / src / com / google / zxing / client / result / GeoParsedResult.java
1 /*
2  * Copyright 2008 ZXing authors
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package com.google.zxing.client.result;
18
19 /**
20  * @author Sean Owen
21  */
22 public final class GeoParsedResult extends ParsedResult {
23
24   private final double latitude;
25   private final double longitude;
26   private final double altitude;
27   private final String query;
28
29   GeoParsedResult(double latitude, double longitude, double altitude, String query) {
30     super(ParsedResultType.GEO);
31     this.latitude = latitude;
32     this.longitude = longitude;
33     this.altitude = altitude;
34     this.query = query;
35   }
36
37   public String getGeoURI() {
38     StringBuffer result = new StringBuffer();
39     result.append("geo:");
40     result.append(latitude);
41     result.append(',');
42     result.append(longitude);
43     if (altitude > 0) {
44       result.append(',');
45       result.append(altitude);
46     }
47     if (query != null) {
48       result.append('?');
49       result.append(query);
50     }
51     return result.toString();
52   }
53
54   /**
55    * @return latitude in degrees
56    */
57   public double getLatitude() {
58     return latitude;
59   }
60
61   /**
62    * @return longitude in degrees
63    */
64   public double getLongitude() {
65     return longitude;
66   }
67
68   /**
69    * @return altitude in meters. If not specified, in the geo URI, returns 0.0
70    */
71   public double getAltitude() {
72     return altitude;
73   }
74
75   /**
76    * @return query string associated with geo URI or null if none exists
77    */
78   public String getQuery() {
79     return query;
80   }
81
82   public String getDisplayResult() {
83     StringBuffer result = new StringBuffer(20);
84     result.append(latitude);
85     result.append(", ");
86     result.append(longitude);
87     if (altitude > 0.0) {
88       result.append(", ");
89       result.append(altitude);
90       result.append('m');
91     }
92     if (query != null) {
93       result.append(" (");
94       result.append(query);
95       result.append(')');
96     }
97     return result.toString();
98   }
99
100   /**
101    * @return a URI link to Google Maps which display the point on the Earth described
102    *  by this instance, and sets the zoom level in a way that roughly reflects the
103    *  altitude, if specified
104    */
105   /*
106   public String getGoogleMapsURI() {
107     StringBuffer result = new StringBuffer(50);
108     result.append("http://maps.google.com/?ll=");
109     result.append(latitude);
110     result.append(',');
111     result.append(longitude);
112     if (altitude > 0.0f) {
113       // Map altitude to zoom level, cleverly. Roughly, zoom level 19 is like a
114       // view from 1000ft, 18 is like 2000ft, 17 like 4000ft, and so on.
115       double altitudeInFeet = altitude * 3.28;
116       int altitudeInKFeet = (int) (altitudeInFeet / 1000.0);
117       // No Math.log() available here, so compute log base 2 the old fashioned way
118       // Here logBaseTwo will take on a value between 0 and 18 actually
119       int logBaseTwo = 0;
120       while (altitudeInKFeet > 1 && logBaseTwo < 18) {
121         altitudeInKFeet >>= 1;
122         logBaseTwo++;
123       }
124       int zoom = 19 - logBaseTwo;
125       result.append("&z=");
126       result.append(zoom);
127     }
128     return result.toString();
129   }
130    */
131
132 }