|
Example 11 - More Events
This little example just lets the user click around the map, generating a polyline from point to point.
Here's the code we used:
First the <googlemaps:map> tag with the event tag added:
<googlemaps:map id="map3" 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/example_11.jsp"/>
<googlemaps:message>
<img src="/GoogleMaps/images/loading.gif"/> Please wait...
</googlemaps:message>
</googlemaps:map>
Here's the bean mapping:
<jsp:useBean id="processor2" class="com.lamatek.beans.PolyLineBean">
<jsp:setProperty name="processor2" property="pageContext"
value="<%=pageContext%>"/>
</jsp:useBean>
And last is the actual code for the bean:
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);
}
}
}
To see the test in action, start clicking on the map. Starting with your second click, you'll see a polyline follow
you around.
In Example 12 we'll experiment with asynchronous events.
|