KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > lamatek > tags > google > GoogleMapPointTag


1 package com.lamatek.tags.google;
2
3 import java.io.Serializable JavaDoc;
4
5 import javax.servlet.jsp.tagext.Tag JavaDoc;
6 import javax.servlet.jsp.tagext.TagSupport JavaDoc;
7
8 import com.lamatek.tags.google.beans.GeocoderBean;
9 import com.lamatek.tags.google.beans.Ip2GeoCoder;
10
11 /**
12  * GoogleMapPointTag
13  *
14  * This class represents a <googlemaps;point> tag. Developers should not subclass or override this
15  * class or it's methods.
16  *
17  * @author Tom Cole
18  * @version 0.40
19  */

20 public class GoogleMapPointTag extends TagSupport JavaDoc implements Serializable JavaDoc {
21     
22     String JavaDoc id = null;
23     double latitude = -181.00d;
24     double longitude = -181.00d;
25     String JavaDoc ip = null;
26     String JavaDoc address = null;
27     String JavaDoc city = null;
28     String JavaDoc state = null;
29     String JavaDoc zipcode = null;
30     String JavaDoc country = null;
31     /**
32      * Overrides doEndTag() from TagSupport. Developers should not override this method.
33      */

34     public int doEndTag() {
35         if (longitude < -180 || latitude < -90 || longitude > 180 || latitude > 90) {
36             if (geocode())
37                 addTag();
38         }
39         else {
40             addTag();
41         }
42         return EVAL_PAGE;
43     }
44     /**
45      * Starts the geocoding process if the longitude and latitude either:
46      * <ol>
47      * <li>Have not been previously geocoded, or
48      * <li>Have not been set.
49      * </ol>
50      * Automatically determines if this is an IP address or street address geocode,
51      * and forwards to the appropriate geocoder.
52      *
53      * @return True if geocoding resolved a longitude/latitude pair. False is not.
54      */

55     public boolean geocode() {
56         if (longitude < -180 || latitude < -90 || longitude > 180 || latitude > 90) {
57             if (ip == null) {
58                 GeocoderBean coder = new GeocoderBean();
59                 coder.setAddress(address);
60                 coder.setCity(city);
61                 coder.setState(state);
62                 coder.setCountry(country);
63                 coder.setZip(zipcode);
64                 if (coder.geocode()) {
65                     longitude = coder.getLongitude();
66                     latitude = coder.getLatitude();
67                     return true;
68                 }
69                 else {
70                     return false;
71                 }
72             }
73             else {
74                 Ip2GeoCoder coder = new Ip2GeoCoder();
75                 coder.setIp(ip);
76                 if (coder.geocode()) {
77                     longitude = coder.getLongitude();
78                     latitude = coder.getLatitude();
79                     return true;
80                 }
81                 else {
82                     return false;
83                 }
84             }
85         }
86         else {
87             return true;
88         }
89     }
90     /**
91      * Overrides doStartTag() from TagSupport. Developers should not override this method.
92      */

93     public int doStartTag(){
94         return SKIP_BODY;
95     }
96     /**
97      * Adds this tag to the parent GoogleMapTag.
98      */

99     private void addTag() {
100         Tag JavaDoc tag = this;
101         while (tag.getParent() != null) {
102             if (tag.getParent() instanceof GoogleMapTag) {
103                 ((GoogleMapTag) tag.getParent()).addPoint(this);
104                 return;
105             }
106             tag = tag.getParent();
107         }
108     }
109     /**
110      * Sets this point as the initial centerpoint of the entire map.
111      *
112      * @param center True if the map should be centered around this point. False if not.
113      */

114     public void setCenter(boolean center) {
115         if (center) {
116             Tag JavaDoc tag = this;
117             while (tag.getParent() != null)
118                 if (tag.getParent() instanceof GoogleMapTag) {
119                     ((GoogleMapTag) tag.getParent()).setCenterLatitude(latitude);
120                     ((GoogleMapTag) tag.getParent()).setCenterLongitude(longitude);
121                     return;
122                 }
123         }
124     }
125     /**
126      * Sets the latitude (in decimal form) for this point. If using IP or street addresses,
127      * this is called automatically by the resulting Geocoder.
128      *
129      * @param latitude The latitude (in decimal form)
130      */

131     public void setLatitude(double latitude) {
132         this.latitude = latitude;
133     }
134     /**
135      * Sets the longitude (in decimal form) for this point. If using IP or street addresses,
136      * this is called automatically by the resulting Geocoder.
137      *
138      * @param longitude The longitude (in decimal form)
139      */

140     public void setLongitude(double longitude) {
141         this.longitude = longitude;
142     }
143     /**
144      * Attempts to set the latitude using a String, by converting it to a double.
145      *
146      * @param value A valid latitude, specified as a String.
147      */

148     public void setLatitudeAsString(String JavaDoc value) {
149         try {
150            latitude = Double.parseDouble(value);
151         }
152         catch(NumberFormatException JavaDoc nfe) {
153            nfe.printStackTrace(System.err);
154         }
155     }
156     /**
157      * Attempts to set the longitude using a String, by converting it to a double.
158      *
159      * @param value A valid longitude, specified as a String.
160      */

161     public void setLongitudeAsString(String JavaDoc value) {
162         try {
163            longitude = Double.parseDouble(value);
164         }
165         catch(NumberFormatException JavaDoc nfe) {
166            nfe.printStackTrace(System.err);
167         }
168     }
169     /**
170      * Returns the address used when creating this marker, or null if this point was
171      * created using an IP or longitude/latitude pair.
172      *
173      * @return Address string or null.
174      */

175     public String JavaDoc getAddress() {
176         return address;
177     }
178     /**
179      * Sets the street address used to geocode the longitude/latitude.
180      *
181      * @param address A valid street address (i.e. 74 Connor Lane).
182      */

183     public void setAddress(String JavaDoc address) {
184         this.address = address;
185     }
186     /**
187      * Returns the city used when creating this marker, or null if this point was
188      * created using an IP or longitude/latitude pair.
189      *
190      * @return Us or Intl. city string or null.
191      */

192     public String JavaDoc getCity() {
193         return city;
194     }
195     /**
196      * Sets the city name used to geocode the longitude/latitude. This can
197      * be US or international.
198      *
199      * @param city A valid city name.
200      */

201     public void setCity(String JavaDoc city) {
202         this.city = city;
203     }
204     /**
205      * Returns the ISO compliant 2-digit country code used when creating this marker, or null if this point was
206      * created using an IP or longitude/latitude pair.
207      *
208      * @return Country code as a string or null.
209      */

210     public String JavaDoc getCountry() {
211         return country;
212     }
213     /**
214      * Sets the ISO compliant 2-character country code used to geocode the longitude/latitude.
215      *
216      * @param country A valid 2-character country code.
217      */

218     public void setCountry(String JavaDoc country) {
219         this.country = country;
220     }
221     /**
222      * Returns the IP address used when creating this marker, or null if this point was
223      * created using a street address or longitude/latitude pair.
224      *
225      * @return The IPv4 or IPv6 address as a string or null.
226      */

227     public String JavaDoc getIp() {
228         return ip;
229     }
230     /**
231      * Sets the IP address used to geocode the longitude/latitude.
232      *
233      * @param ip A valid ip address (i.e. 216.113.235.52).
234      */

235     public void setIp(String JavaDoc ip) {
236         this.ip = ip;
237     }
238     /**
239      * Returns the state or province used when creating this marker, or null if this point was
240      * created using an IP or longitude/latitude pair.
241      *
242      * @return State or province string or null.
243      */

244     public String JavaDoc getState() {
245         return state;
246     }
247     /**
248      * Sets the state or province used to geocode the longitude/latitude.
249      *
250      * @param state A valid state or province.
251      */

252     public void setState(String JavaDoc state) {
253         this.state = state;
254     }
255     /**
256      * Returns the zip or postal code used when creating this marker, or null if this point was
257      * created using an IP or longitude/latitude pair.
258      *
259      * @return Zipcode or postal code as a string or null.
260      */

261     public String JavaDoc getZipcode() {
262         return zipcode;
263     }
264     /**
265      * Sets the zip or postal used to geocode the longitude/latitude.
266      *
267      * @param zipcode A valid US ZIP or international postal code.
268      */

269     public void setZipcode(String JavaDoc zipcode) {
270         this.zipcode = zipcode;
271     }
272     /**
273      * Returns the latitude (in decimal form) for this point. This may have been
274      * set programmatically, or if using addresses, may have been geocoded after
275      * calling the geocode() method. If using addresses, developers should be sure to
276      * call geocode() before calling this method.
277      *
278      * @return The latitude of this point or -181 if non-codable.
279      */

280     public double getLatitude() {
281         return latitude;
282     }
283     /**
284      * Returns the longitude (in decimal form) for this point. This may have been
285      * set programmatically, or if using addresses, may have been geocoded after
286      * calling the geocode() method. If using addresses, developers should be sure to
287      * call geocode() before calling this method.
288      *
289      * @return The longitude of this point or -181 if non-codable.
290      */

291     public double getLongitude() {
292         return longitude;
293     }
294     /**
295      * Returns the unique id for this point.
296      *
297      * @return The points id.
298      */

299     public String JavaDoc getId() {
300         return id;
301     }
302     /**
303      * Sets the unique id for this point. Other overlays will use this id
304      * to reference this point in their construction.
305      *
306      * @param id A unique id for this point.
307      */

308     public void setId(String JavaDoc id) {
309         this.id = id;
310     }
311 }
312
Popular Tags