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.URL ; 7 import java.net.URLConnection ; 8 import java.net.URLEncoder ; 9 10 21 public class USAddressGeocoder implements Geocoder, Serializable { 22 23 String zipcode = null; 24 String state = null; 25 String city = null; 26 String address = null; 27 String country = null; 28 double longitude = 0.00d; 29 double latitude = 0.00d; 30 String precision = null; 31 String warning = null; 32 33 public void setAddress(String address) { 34 this.address = address; 35 } 36 public void setCity(String city) { 37 this.city = city; 38 } 39 public void setState(String state) { 40 this.state = state; 41 } 42 public void setCountry(String country) { 43 this.country = country; 44 } 45 public void setZip(String zip) { 46 this.zipcode = zip; 47 } 48 public boolean geocode() { 49 try { 50 String 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 con = new URL (path).openConnection(); 60 if (con != null) { 61 BufferedReader input = new BufferedReader (new InputStreamReader (con.getInputStream())); 62 String 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 value = line.substring(start, end).trim(); 68 try { 69 latitude = Double.parseDouble(value); 70 } 71 catch(Exception 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 value = line.substring(start, end).trim(); 80 try { 81 longitude = Double.parseDouble(value); 82 } 83 catch(Exception 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 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 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 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 getPrecision() { 118 return precision; 119 } 120 public String getWarning() { 121 return warning; 122 } 123 } 124 | Popular Tags |