|
Example 10 - Processing Events with Servlets
This example uses a <googlemaps:map> tag with a click event added. The click event is
set to call an event servlet anytime a click is heard. What we've done is set the url of the action
to point to the servlet designed to handle the request. The servlet's job is to add a <googlemaps:marker> tag to the
map, and to have the <googlemaps:marker> display an infowindow showing the longitude and latitude
clicked.
Notice that we've set the scope of the map to site. This allows us to load the map
in the servlet from the session. If we had not set the scope to site, the map would
be inaccesssible to the servlet.
Here's the code we used:
First the <googlemaps:map> tag with the event tag added:
<googlemaps:map id="map" width="250" height="300" version="2" type="STREET"
scope="site">
<googlemaps:key domain="localhost" key="xxxx"/>
<googlemaps:point id="point1" address="74 Connors Lane" city="Elkton"
state="MD" zipcode="21921" country="US"/>
<googlemaps:event action="click" url="/GoogleMaps/servlet/event.do"/>
<googlemaps:message>
<img src="/GoogleMaps/images/loading.gif"/> Please wait...
</googlemaps:message>
</googlemaps:map>
And last is the actual code for the servlet. Here you'll notice that we're accessing
the base classes:
package com.lamatek.servlets;
import java.io.IOException;
import java.util.Date;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.lamatek.servlets.google.DefaultEventListenerServlet;
import com.lamatek.tags.google.GoogleMapEventTag;
import com.lamatek.tags.google.GoogleMapInfoWindowTag;
import com.lamatek.tags.google.GoogleMapMarkerTag;
import com.lamatek.tags.google.GoogleMapPointTag;
import com.lamatek.tags.google.GoogleMapTag;
public class EventListenerServlet extends DefaultEventListenerServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
super.doGet(request, response);
response.sendRedirect("/GoogleMaps/example_10.jsp");
}
public void processClickEvent(GoogleMapTag map, double longitude, double latitude) {
super.processClickEvent(map, longitude, latitude);
String name = "marker" + (new Date()).getTime();
String pointName = "point" + (new Date()).getTime();
GoogleMapPointTag point = new GoogleMapPointTag();
point.setId(pointName);
point.setLatitude(latitude);
point.setLongitude(longitude);
map.addPoint(point);
GoogleMapMarkerTag marker = new GoogleMapMarkerTag();
marker.setPoint(pointName);
marker.setId(name);
GoogleMapInfoWindowTag info = new GoogleMapInfoWindowTag();
info.setDisplay(true);
info.setHtml(true);
info.setContent("<form action=\"\" method=\"POST\">
<table><tr><td>Title: </td><td>
<input type=\"text\" name=\"title\"/></td>
</tr><tr><td>URL: </td><td>
<input type=\"text\" name=\"url\"/>
</td></tr><tr><td></td><td>
<input type=\"submit\" value=\"Add\"/></td>
</tr></table></form>");
marker.setInfoWindow(info);
GoogleMapEventTag event = new GoogleMapEventTag();
event.setAction("dblclick");
event.setAsynchronous(false);
event.setUrl("/GoogleMaps/servlet/event.do");
marker.addEvent(event);
map.addMarker(marker);
}
public void processOverlayDoubleClickEvent(GoogleMapTag map, String component, String type) {
super.processOverlayDoubleClickEvent(map, component, type);
if (type.equals("marker")) {
GoogleMapMarkerTag m = map.getMarker(component);
map.removeMarker(m);
}
}
}
To see the test in action, click anywhere on the map. A marker will be added, displaying an
infowindow with the coordinates loaded.
Well that's the servlet event example. Next we'll experiment with a map
that lets users draw polylines.
|