|
Example 12 - Asynchronous Events
This example experiments with asynchronous events. An asynchronous event is an
http request that is sent without updating the page. Where a normal HTTP requests results
in either a new page being loaded, or the same page being reloaded, asynchronous events
do not result (necessarily) in any changes on the current page.
As far as this taglibrary is concerned, these types of events are useful when processing events
that you want to store or process, but don't actually effect the map, and therefore does not require a map
reloading.
This example works just like example 11 (the polyline example) except it stores the user's mouse clicks,
but doesn't update the page. While there's not a lot of
use for this, it demonstrates the function. To test it go ahead and click around the map.
When you're all done playing, click refresh. You'll see all your polylines display magically on
the page...
Here's the map tag we've setup. Notice that we've set the asynchronous="true" attribute
in the event tag:
<googlemaps:map id="map4" width="250" height="300" version="2" scope="site"
type="STREET" zoom="12">
<googlemaps:key domain="localhost" key="xxx"/>
<googlemaps:point id="point1" address="74 Connors Lane" city="Elkton"
state="MD" zipcode="21921" country="US"/>
<googlemaps:event action="click" url="/GoogleMaps/example_11.jsp"
asynchronous="true"/>
</googlemaps:map>
<jsp:useBean id="processor3" class="com.lamatek.beans.AsynchronousEventListener"/>
And here's the bean (it's the same setup as example 11):
<jsp:useBean id="processor2" class="com.lamatek.beans.PolyLineBean">
<jsp:setProperty name="processor2" property="pageContext"
value="<%=pageContext%>"/>
</jsp:useBean>
Last but not least, here's the bean we've used to process the events. You'll notice that it's the exact
same bean we used to handle the synchronous events in the previous example. All you need to do is declare the event as
asynchronous and you're off...
package com.lamatek.beans;
import java.util.Date;
import javax.servlet.ServletRequest;
import com.lamatek.beans.google.DefaultEventListenerBean;
import com.lamatek.tags.google.GoogleMapPointTag;
import com.lamatek.tags.google.GoogleMapPolylineTag;
import com.lamatek.tags.google.GoogleMapTag;
public class PolyLineBean extends DefaultEventListenerBean {
String lineId = "line_" + (new Date()).getTime();
public void processClickEvent(GoogleMapTag map, double longitude,
double latitude) {
super.processClickEvent(map, longitude, latitude);
try {
if (getPageContext().getSession().getAttribute("lineId") == null)
getPageContext().getSession().setAttribute("lineId", lineId);
else
lineId = (String) getPageContext().getSession().getAttribute("lineId");
GoogleMapPointTag point = new GoogleMapPointTag();
point.setLatitude(latitude);
point.setLongitude(longitude);
String pointID = "point_" + (new Date()).getTime();
point.setId(pointID);
map.addPoint(point);
GoogleMapPolylineTag line = map.getPolyline(lineId);
if (line == null) {
line = new GoogleMapPolylineTag();
line.setId(lineId);
line.setPointlist(pointID);
map.addPolyline(line);
}
else {
line.setPointlist(line.getPointlist() + "," + pointID);
}
}
catch(Exception ex) {
ex.printStackTrace(System.out);
}
}
}
That about does it for our examples section. We hope that you now have an idea what can
be accomplished using the GoogleMaps API Taglibrary with absolutely no javascript, AJAX or Google Maps
API programming required on your part.
We've included some Miscellaneous examples which include
our new <googlemaps:traffic> tag and using The Google Maps JSP Taglibrary within JSTL
for database driven maps. We've also added new XML capabilities which are displayed here.
So where are we going next? See our conclusion page.
|