KickJava   Java API By Example, From Geeks To Geeks.

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


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.HttpURLConnection JavaDoc;
7 import java.net.URL JavaDoc;
8 import java.net.URLEncoder JavaDoc;
9 import java.util.StringTokenizer JavaDoc;
10
11 /**
12  * IntlAddressGeocoder
13  * @author Tom Cole
14  * @version 0.40
15  *
16  * This class takes an international address and attempts to geocode it to
17  * longitude/latitude using Maporama.com.
18  *
19  * Developers should not override this class, but rather should override
20  * the Geocoder.geocode() method to call the geocoder of their choice.
21  */

22 public class IntlAddressGeocoder implements Geocoder, Serializable JavaDoc {
23     
24     String JavaDoc zipcode = "";
25     String JavaDoc state = "";
26     String JavaDoc city = null;
27     String JavaDoc address = "";
28     String JavaDoc country = null;
29     double longitude = 0.00d;
30     double latitude = 0.00d;
31
32     public void setAddress(String JavaDoc address) {
33         this.address = address;
34     }
35     public void setCity(String JavaDoc city) {
36         this.city = city;
37     }
38     public void setState(String JavaDoc state) {
39         this.state = state;
40     }
41     public void setCountry(String JavaDoc country) {
42        this.country = country;
43     }
44     public void setZip(String JavaDoc zip) {
45         this.zipcode = zip;
46     }
47     public boolean geocode() {
48         if (country == null || city == null) {
49             return false;
50         }
51         else try {
52             String JavaDoc path = "http://www.maporama.com/share/Map.asp?";
53             path += "COUNTRYCODE=" + country;
54             path += "&_XgoGCAddress=" + URLEncoder.encode(address, "UTF-8");
55             path += "&_XgoGCTownName=" + URLEncoder.encode(city.trim(), "UTF-8");
56             path += "&State=" + URLEncoder.encode(state, "UTF-8");
57             path += "&Zip=" + URLEncoder.encode(zipcode, "UTF-8");
58             URL JavaDoc url = new URL JavaDoc(path);
59             HttpURLConnection JavaDoc con = (HttpURLConnection JavaDoc) url.openConnection();
60             BufferedReader JavaDoc input = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(con.getInputStream()));
61             String JavaDoc line = "";
62             int count = 0;
63             while ((line = input.readLine()) != null) {
64                 if (line.indexOf("Lat-Long") > 0) {
65                     int start = line.indexOf("SearchMapFontText", line.indexOf("Lat-Long"));
66                     start = line.indexOf("SearchMapFontText", start + 1);
67                     start = line.indexOf(">", start) + 1;
68                     int end = line.indexOf("<", start);
69                     String JavaDoc coords = line.substring(start, end);
70                     StringTokenizer JavaDoc t = new StringTokenizer JavaDoc(coords, ",");
71                     try {
72                         latitude = Double.parseDouble(t.nextToken().trim());
73                         longitude = Double.parseDouble(t.nextToken().trim());
74                     }
75                     catch(Exception JavaDoc e) {
76                         e.printStackTrace(System.err);
77                         return false;
78                     }
79                 }
80             }
81             input.close();
82             return true;
83         }
84         catch(Exception JavaDoc ex) {
85             ex.printStackTrace(System.err);
86             return false;
87         }
88     }
89     public double getLongitude() {
90         return longitude;
91     }
92     public double getLatitude() {
93         return latitude;
94     }
95     public String JavaDoc getPrecision() {
96         return null;
97     }
98     public String JavaDoc getWarning() {
99         return null;
100     }
101 }
102
Popular Tags