KickJava   Java API By Example, From Geeks To Geeks.

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


1 package com.lamatek.tags.google;
2
3 import java.io.InputStream JavaDoc;
4 import java.io.Serializable JavaDoc;
5 import java.net.URL JavaDoc;
6 import java.net.URLConnection JavaDoc;
7 import java.util.Date JavaDoc;
8 import java.util.Vector JavaDoc;
9
10 import javax.servlet.jsp.tagext.BodyTagSupport JavaDoc;
11 import javax.servlet.jsp.tagext.Tag JavaDoc;
12 import javax.xml.parsers.DocumentBuilder JavaDoc;
13 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
14
15 import org.w3c.dom.Document JavaDoc;
16 import org.w3c.dom.NamedNodeMap JavaDoc;
17 import org.w3c.dom.Node JavaDoc;
18 import org.w3c.dom.NodeList JavaDoc;
19
20 import com.lamatek.tags.google.beans.TrafficEventBean;
21
22 /**
23  * GoogleMapTrafficTag
24  *
25  * This class represents a <googlemaps:traffic> tag. Developers should not subclass nor
26  * override it's methods.
27  *
28  * @author Tom Cole
29  * @version 0.75
30  */

31 public class GoogleMapTrafficTag extends BodyTagSupport JavaDoc implements Serializable JavaDoc {
32     
33     String JavaDoc id = null;
34     String JavaDoc point = null;
35     double radius = 0.00d;
36     Vector JavaDoc events = null;
37     int severity = 1;
38     String JavaDoc type = "both"; //incident | construction | both
39
GoogleMapIconTag icon = null;
40     
41     /**
42      * This method geocodes all the traffic events within the specified radius of the
43      * longitude and latitude provided, and populates them as TrafficEventBeans. These
44      * TrafficEventBeans can be retrieved using the getEvent() method.
45      *
46      * @param longitude The longitude of the centerpoint from which the radius is derived.
47      * @param latitude The latitude of the centerpoint from which the radius is derived.
48      */

49     public boolean geocode(double longitude, double latitude) {
50         boolean coded = true;
51         String JavaDoc url = "http://api.local.yahoo.com/MapsService/V1/trafficData?appid=google-jsp-taglib";
52         url += "&longitude=" + longitude + "&latitude=" + latitude;
53         url += "&severity=" + severity + "&radius=" + radius;
54         url += "&include_map=0";
55         try {
56             URLConnection JavaDoc con = new URL JavaDoc(url).openConnection();
57             if (con != null) {
58                 InputStream JavaDoc input = con.getInputStream();
59                 DocumentBuilder JavaDoc parser = DocumentBuilderFactory.newInstance().newDocumentBuilder();
60                 Document JavaDoc elements = parser.parse(input);
61                 NodeList JavaDoc results = elements.getElementsByTagName("Result");
62                 for (int i = 0; i < results.getLength(); i++) {
63                     Node JavaDoc result = results.item(i);
64                     NamedNodeMap JavaDoc attributes = result.getAttributes();
65                     String JavaDoc eventType = attributes.getNamedItem("type").getNodeValue();
66                     if (type.equals("both") || type.equals(eventType)) {
67                         if (events == null)
68                             events = new Vector JavaDoc();
69                         TrafficEventBean event = new TrafficEventBean();
70                         event.setType(eventType);
71                         NodeList JavaDoc children = result.getChildNodes();
72                         for (int j = 0; j < children.getLength(); j++) {
73                             Node JavaDoc child = children.item(j);
74                             Node JavaDoc text = null;
75                             NodeList JavaDoc sub = child.getChildNodes();
76                             for (int c = 0; c < sub.getLength(); c++) {
77                                 if (sub.item(c).getNodeName().equals("#text")) {
78                                     text = sub.item(c);
79                                 }
80                             }
81                             if (child.getNodeName().equals("Title")) {
82                                 if (text != null)
83                                   event.setTitle(text.getNodeValue());
84                             }
85                             else if (child.getNodeName().equals("Description")) {
86                                 if (text != null)
87                                       event.setDescription(text.getNodeValue());
88                             }
89                             else if (child.getNodeName().equals("Longitude")) {
90                                 if (text != null)
91                                       event.setLongitude(Double.parseDouble(text.getNodeValue()));
92                             }
93                             else if (child.getNodeName().equals("Latitude")) {
94                                 if (text != null)
95                                       event.setLatitude(Double.parseDouble(text.getNodeValue()));
96                             }
97                             else if (child.getNodeName().equals("Severity")) {
98                                 if (text != null)
99                                       event.setSeverity(Integer.parseInt(text.getNodeValue()));
100                             }
101                             else if (child.getNodeName().equals("Direction")) {
102                                 if (text != null)
103                                       event.setDirection(text.getNodeValue());
104                             }
105                             else if (child.getNodeName().equals("ReportDate")) {
106                                 if (text != null)
107                                       event.setReported(new Date JavaDoc(Long.parseLong(text.getNodeValue())));
108                             }
109                             else if (child.getNodeName().equals("UpdateDate")) {
110                                 if (text != null)
111                                       event.setUpdated(new Date JavaDoc(Long.parseLong(text.getNodeValue())));
112                             }
113                             else if (child.getNodeName().equals("EndDate")) {
114                                 if (text != null)
115                                       event.setEnding(new Date JavaDoc(Long.parseLong(text.getNodeValue())));
116                             }
117                         }
118                         events.addElement(event);
119                     }
120                 }
121                 input.close();
122             }
123         }
124         catch(Exception JavaDoc ex) {
125             ex.printStackTrace(System.err);
126             coded = false;
127         }
128         return coded;
129     }
130     /**
131      * Overrides doStartTag() from BodyTagSupport. Developers should not override
132      * this method.
133      */

134     public int doStartTag() {
135         return EVAL_BODY_INCLUDE;
136     }
137     /**
138      * Returns the number of traffic events found within the mile radius of the
139      * provided centerpoint.
140      *
141      * @return The number of events found.
142      */

143     public int eventCount() {
144         if (events == null) {
145             return 0;
146         }
147         else {
148             return events.size();
149         }
150     }
151     /**
152      * Returns the traffic event at the given index.
153      *
154      * @return A traffic event as a TrafficEventBean
155      */

156     public TrafficEventBean getEvent(int i) {
157         if (events == null) {
158             return null;
159         }
160         else {
161             return (TrafficEventBean) events.elementAt(i);
162         }
163     }
164     /**
165      * Overrides doEndTag() from BodyTagSupport. Developers should not override
166      * this method.
167      */

168     public int doEndTag() {
169         addTag();
170         return EVAL_PAGE;
171     }
172     /**
173      * Adds this tag to the parent GoogleMapTag.
174      */

175     private void addTag() {
176         Tag JavaDoc tag = this;
177         while (tag.getParent() != null) {
178             if (tag.getParent() instanceof GoogleMapTag) {
179                 ((GoogleMapTag) tag.getParent()).addTraffic_tag(this);
180                 return;
181             }
182             tag = tag.getParent();
183         }
184     }
185     /**
186      * Returns the map unique id for this traffic event.
187      *
188      * @return The id of the traffic event. This value is generated.
189      */

190     public String JavaDoc getId() {
191         return id;
192     }
193     /**
194      * Sets the map unique id for this traffic event.
195      *
196      * @param id A unique id.
197      */

198     public void setId(String JavaDoc id) {
199         this.id = id;
200     }
201     /**
202      * Returns the id of the point used as the centerpoint for scanning events.
203      *
204      * @return A GoogleMapPointTag id.
205      */

206     public String JavaDoc getPoint() {
207         return point;
208     }
209     /**
210      * Sets the id of the GoogleMapPointTag used as the centerpoint for scanning
211      * for events.
212      *
213      * @param point The id of the GoogleMapsPointTag used as the centerpoint.
214      */

215     public void setPoint(String JavaDoc point) {
216         this.point = point;
217     }
218     /**
219      * Returns the radius (in miles) from the centerpoint that will be scanned
220      * for traffic events.
221      *
222      * @return The radius (in miles) as a double.
223      */

224     public double getRadius() {
225         return radius;
226     }
227     /**
228      * Sets the radius (in miles) from the centerpoint that will be scanned for
229      * traffic events.
230      *
231      * @param radius The radius to scan (in miles)
232      */

233     public void setRadius(double radius) {
234         this.radius = radius;
235     }
236     /**
237      * Returns the minimum severity of traffic events to scan for. This value should
238      * be between 1 (least severe) to 5 (most severe).
239      *
240      * @return The minimum severity of events to look for.
241      */

242     public int getSeverity() {
243         return severity;
244     }
245     /**
246      * Sets the type of events to look for. Valid values are 'incident' for non-construction
247      * related delays like accidents, 'construction' for construction related events or
248      * 'both' for both construction and non-construction related events.
249      *
250      * @param type The type of events to look for.
251      */

252     public void setType(String JavaDoc type) {
253         this.type = type;
254     }
255     /**
256      * Retrns the type of events that will be scanned for. Valid values are 'incident' for non-construction
257      * related delays like accidents, 'construction' for construction related events or
258      * 'both' for both construction and non-construction related events.
259      *
260      * @return The type of events being looked for.
261      */

262     public String JavaDoc getType() {
263         return type;
264     }
265     /**
266      * Sets the minimum severity levels of events to look for. This number must be between
267      * 1 (least severe) and 5 (most severe).
268      *
269      * @return The current minimum severity level of events to look for.
270      */

271     public void setSeverity(int severity) {
272         this.severity = severity;
273     }
274     /**
275      * Sets a GoogleMapIconTag for this group of events, overriding the standard Google Maps marker icon.
276      *
277      * @param icon An initialized GoogleMapIconTag
278      */

279     public void setIcon(GoogleMapIconTag icon) {
280         this.icon = icon;
281     }
282     /**
283      * Returns the GoogleMapIconTag that renders this marker, or null if the default
284      * markers are used.
285      *
286      * @return A GoogleMapIconTag or null.
287      */

288     public GoogleMapIconTag getIcon() {
289         return icon;
290     }
291 }
292
Popular Tags