KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > lamatek > tags > google > beans > USAddressGeocoder


1 package com.lamatek.tags.google.beans;
2
3 import java.io.BufferedReader JavaDoc;
4 import java.io.InputStreamReader JavaDoc;
5 import java.io.Serializable JavaDoc;
6 import java.net.URL JavaDoc;
7 import java.net.URLConnection JavaDoc;
8 import java.net.URLEncoder JavaDoc;
9
10 /**
11  * USAddressGeocoder
12  * @author Tom Cole
13  * @version 0.40
14  *
15  * This class takes a US address and attempts to geocode it to
16  * longitude/latitude using the Yahoo Geocoding API.
17  *
18  * Developers should not override this class, but rather should override
19  * the Geocoder.geocode() method to call the geocoder of their choice.
20  */

21 public class USAddressGeocoder implements Geocoder, Serializable JavaDoc {
22     
23     String JavaDoc zipcode = null;
24     String JavaDoc state = null;
25     String JavaDoc city = null;
26     String JavaDoc address = null;
27     String JavaDoc country = null;
28     double longitude = 0.00d;
29     double latitude = 0.00d;
30     String JavaDoc precision = null;
31     String JavaDoc warning = null;
32
33     public void setAddress(String JavaDoc address) {
34         this.address = address;
35     }
36     public void setCity(String JavaDoc city) {
37         this.city = city;
38     }
39     public void setState(String JavaDoc state) {
40         this.state = state;
41     }
42     public void setCountry(String JavaDoc country) {
43        this.country = country;
44     }
45     public void setZip(String JavaDoc zip) {
46         this.zipcode = zip;
47     }
48     public boolean geocode() {
49         try {
50             String JavaDoc path = "http://api.local.yahoo.com/MapsService/V1/geocode?appid=google-jsp-taglib";
51             if (address != null)
52                 path += "&street=" + URLEncoder.encode(address, "UTF-8");
53             if (city != null)
54                 path += "&city=" + URLEncoder.encode(city, "UTF-8");
55             if (state != null)
56                 path += "&state=" + URLEncoder.encode(state, "UTF-8");
57             if (zipcode != null)
58                 path += "&zip=" + URLEncoder.encode(zipcode, "UTF-8");
59             URLConnection JavaDoc con = new URL JavaDoc(path).openConnection();
60             if (con != null) {
61                 BufferedReader JavaDoc input = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(con.getInputStream()));
62                 String JavaDoc line = "";
63                 while ((line = input.readLine()) != null) {
64                     if (line.indexOf("<Latitude>") > 0) {
65                         int start = line.indexOf("<Latitude>") + 10;
66                         int end = line.indexOf("</Latitude>", start);
67                         String JavaDoc value = line.substring(start, end).trim();
68                         try {
69                             latitude = Double.parseDouble(value);
70                         }
71                         catch(Exception JavaDoc ex) {
72                             ex.printStackTrace(System.err);
73                             return false;
74                         }
75                     }
76                     if (line.indexOf("<Longitude>") > 0) {
77                         int start = line.indexOf("<Longitude>") + 11;
78                         int end = line.indexOf("</Longitude>", start);
79                         String JavaDoc value = line.substring(start, end).trim();
80                         try {
81                             longitude = Double.parseDouble(value);
82                         }
83                         catch(Exception JavaDoc ex) {
84                             ex.printStackTrace(System.err);
85                             return false;
86                         }
87                     }
88                     if (line.indexOf("<Result") > 0 && line.indexOf("precision") > 0) {
89                         int start = line.indexOf("precision=\"") + 11;
90                         int end = line.indexOf("\"", start);
91                         String JavaDoc precision = line.substring(start, end).trim();
92                     }
93                     if (line.indexOf("<Result") > 0 && line.indexOf("warning") > 0) {
94                         int start = line.indexOf("warning=\"") + 9;
95                         int end = line.indexOf("\"", start);
96                         String JavaDoc warning = line.substring(start, end).trim();
97                     }
98                 }
99                 input.close();
100                 return true;
101             }
102             else {
103                 return false;
104             }
105         }
106         catch(Exception JavaDoc ex) {
107             ex.printStackTrace();
108             return false;
109         }
110     }
111     public double getLongitude() {
112         return longitude;
113     }
114     public double getLatitude() {
115         return latitude;
116     }
117     public String JavaDoc getPrecision() {
118         return precision;
119     }
120     public String JavaDoc getWarning() {
121         return warning;
122     }
123 }
124
Popular Tags