1 package com.lamatek.tags.google.beans; 2 3 import java.io.BufferedReader ; 4 import java.io.InputStreamReader ; 5 import java.io.Serializable ; 6 import java.net.HttpURLConnection ; 7 import java.net.URL ; 8 import java.net.URLEncoder ; 9 import java.util.StringTokenizer ; 10 11 22 public class IntlAddressGeocoder implements Geocoder, Serializable { 23 24 String zipcode = ""; 25 String state = ""; 26 String city = null; 27 String address = ""; 28 String country = null; 29 double longitude = 0.00d; 30 double latitude = 0.00d; 31 32 public void setAddress(String address) { 33 this.address = address; 34 } 35 public void setCity(String city) { 36 this.city = city; 37 } 38 public void setState(String state) { 39 this.state = state; 40 } 41 public void setCountry(String country) { 42 this.country = country; 43 } 44 public void setZip(String 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 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 url = new URL (path); 59 HttpURLConnection con = (HttpURLConnection ) url.openConnection(); 60 BufferedReader input = new BufferedReader (new InputStreamReader (con.getInputStream())); 61 String 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 coords = line.substring(start, end); 70 StringTokenizer t = new StringTokenizer (coords, ","); 71 try { 72 latitude = Double.parseDouble(t.nextToken().trim()); 73 longitude = Double.parseDouble(t.nextToken().trim()); 74 } 75 catch(Exception e) { 76 e.printStackTrace(System.err); 77 return false; 78 } 79 } 80 } 81 input.close(); 82 return true; 83 } 84 catch(Exception 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 getPrecision() { 96 return null; 97 } 98 public String getWarning() { 99 return null; 100 } 101 } 102 | Popular Tags |