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. See
23 * <a href="http://tools.ietf.org/html/draft-mayrhofer-geo-uri-00">
24 * http://tools.ietf.org/html/draft-mayrhofer-geo-uri-00</a>.
26 * @author srowen@google.com (Sean Owen)
28 public final class GeoParsedResult extends ParsedReaderResult {
30 private final float latitude;
31 private final float longitude;
32 private final float altitude;
34 private GeoParsedResult(float latitude, float longitude, float altitude) {
35 super(ParsedReaderResultType.GEO);
36 this.latitude = latitude;
37 this.longitude = longitude;
38 this.altitude = altitude;
41 public static GeoParsedResult parse(Result result) {
42 String rawText = result.getText();
43 if (!rawText.startsWith("geo:")) {
46 // Drop geo, query portion
47 int queryStart = rawText.indexOf('?', 4);
49 rawText = rawText.substring(4);
51 rawText = rawText.substring(4, queryStart);
53 int latitudeEnd = rawText.indexOf(',');
54 if (latitudeEnd < 0) {
57 float latitude = Float.parseFloat(rawText.substring(0, latitudeEnd));
58 int longitudeEnd = rawText.indexOf(',', latitudeEnd + 1);
61 if (longitudeEnd < 0) {
62 longitude = Float.parseFloat(rawText.substring(latitudeEnd + 1));
65 longitude = Float.parseFloat(rawText.substring(latitudeEnd + 1, longitudeEnd));
66 altitude = Float.parseFloat(rawText.substring(longitudeEnd + 1));
68 return new GeoParsedResult(latitude, longitude, altitude);
72 * @return latitude in degrees
74 public float getLatitude() {
79 * @return longitude in degrees
81 public float getLongitude() {
86 * @return altitude in meters. If not specified, in the geo URI, returns 0.0
88 public float getAltitude() {
92 public String getDisplayResult() {
93 StringBuffer result = new StringBuffer(50);
94 result.append(latitude);
95 result.append("deg N, ");
96 result.append(longitude);
97 result.append("deg E, ");
98 result.append(altitude);
100 return result.toString();