PageRenderTime 39ms CodeModel.GetById 18ms app.highlight 15ms RepoModel.GetById 1ms app.codeStats 0ms

/walkytalky/src/com/googlecode/eyesfree/walkytalky/Address.java

http://eyes-free.googlecode.com/
Java | 103 lines | 65 code | 16 blank | 22 comment | 8 complexity | 6b9ae9db294fd24bf265103717bc2966 MD5 | raw file
  1/*
  2 * Copyright (C) 2008 Google Inc.
  3 * 
  4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5 * use this file except in compliance with the License. You may obtain a copy of
  6 * 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, WITHOUT
 12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 13 * License for the specific language governing permissions and limitations under
 14 * the License.
 15 */
 16
 17package com.googlecode.eyesfree.walkytalky;
 18
 19import org.json.JSONArray;
 20import org.json.JSONException;
 21import org.json.JSONObject;
 22
 23/**
 24 * Class for representing a street address. For details, see:
 25 * http://code.google.com/apis/maps/documentation/geocoding/
 26 * 
 27 * @author clchen@google.com (Charles L. Chen)
 28 */
 29
 30public class Address {
 31
 32    private String streetNumber = "";
 33
 34    private String route = "";
 35
 36    private String city = "";
 37
 38    private String postalCode = "";
 39
 40    private boolean isValid = false;
 41
 42    public Address(String mapsJsonResponse) {
 43        try {
 44            JSONObject jsonObj = new JSONObject(mapsJsonResponse);
 45            String status = jsonObj.getString("status");
 46            if (status.equals("OK")) {
 47                JSONArray addressComponents = jsonObj.getJSONArray("results").getJSONObject(0)
 48                        .getJSONArray("address_components");
 49                for (int i = 0; i < addressComponents.length(); i++) {
 50                    JSONObject obj = addressComponents.getJSONObject(i);
 51                    JSONArray types = obj.getJSONArray("types");
 52                    for (int j = 0; j < types.length(); j++) {
 53                        String typeStr = types.getString(j);
 54                        if (typeStr.equals("street_number")) {
 55                            streetNumber = obj.getString("long_name");
 56                            break;
 57                        }
 58                        if (typeStr.equals("route")) {
 59                            route = obj.getString("long_name");
 60                            if (route.length() > 0){
 61                                route = ReverseGeocoder.extendShorts(route);
 62                            }
 63                            break;
 64                        }
 65                        if (typeStr.equals("locality")) {
 66                            city = obj.getString("long_name");
 67                            break;
 68                        }
 69                        if (typeStr.equals("postal_code")) {
 70                            postalCode = obj.getString("long_name");
 71                            break;
 72                        }
 73                    }
 74                }
 75                isValid = true;
 76            }
 77        } catch (JSONException e) {
 78            // TODO Auto-generated catch block
 79            e.printStackTrace();
 80        }
 81    }
 82
 83    public String getStreetNumber() {
 84        return streetNumber;
 85    }
 86
 87    public String getRoute() {
 88        return route;
 89    }
 90
 91    public String getCity() {
 92        return city;
 93    }
 94
 95    public String getPostalCode() {
 96        return postalCode;
 97    }
 98
 99    public boolean isValid() {
100        return isValid;
101    }
102
103}