📄 htmlembeddedmapservlet.java
字号:
// Read in the form fields the user submitted, and store them
// in a Hashtable. For the hashtable keys, use the string
// constants that we have defined for use as Form Field names.
// If there is no appropriate FF_ constant, we define a constant
// specifically for use with this hashtable, beginning with HT_ prefix.
private Hashtable getFormFieldsHT(HttpServletRequest req, MapToolkit toolkit) {
// Create a Hashtable with 17 entries by default, because
// performance is best if you have a prime number of entries,
// with more entries than you will need, so that the VM does
// not attempt to expand the Hashtable automatically.
Hashtable ht = new Hashtable(17);
// Get the selected map tool (radio button)
String param = req.getParameter(MapToolkit.FF_MAPTOOLS);
if (param != null) {
ht.put(MapToolkit.FF_MAPTOOLS, param);
}
// Handle x/y coords (in pixels) of where the user clicked on the map
param = req.getParameter(FF_MAP_IMAGE_X);
if (param != null) {
// An x-coordinate was supplied, so assume that the user clicked the map.
Double dx = new Double(param);
param = req.getParameter(FF_MAP_IMAGE_Y);
if (param != null) {
// A y-coordinate was also supplied....
Double dy = new Double(param);
DoublePoint screenPoint =
new DoublePoint(dx.doubleValue(), dy.doubleValue() );
// Store the point representing where the user clicked (in pixels).
ht.put(HT_SCREEN_LOCATION, screenPoint);
}
}
// Handle x/y coords (in map coordinates) that were stored on the
// map page, in hidden form fields.
param = req.getParameter(FF_OLD_X);
if (param != null) {
// An x-coordinate was supplied, so assume the hidden form fields are present.
Double dx = new Double(param);
param = req.getParameter(FF_OLD_Y);
if (param != null) {
// A y-coordinate was also supplied....
Double dy = new Double(param);
DoublePoint mapPoint =
new DoublePoint(dx.doubleValue(), dy.doubleValue() );
// Store the point representing map's center-point location.
ht.put(HT_OLD_MAP_LOCATION, mapPoint);
}
}
// Handle the map zoom level that was stored on the
// map page, in a hidden form field.
param = req.getParameter(FF_OLD_ZOOM);
if (param != null) {
try {
Double d = new Double(param);
// Store the zoom value the user entered, but only if
// the number is greater than zero. (The map cannot
// be zoomed to zero miles.)
if (d.doubleValue() > 0) {
ht.put(FF_OLD_ZOOM, d);
}
} catch (Exception e) {
// The contents of the text field could not be converted
// into a number, so we will not store any zoom number.
}
}
// Process the hidden form field that represents whether
// the user was viewing the map in its large size.
param = req.getParameter(FF_OLD_SIZE_LARGE);
if (param != null) {
ht.put(FF_OLD_SIZE_LARGE, "true");
}
// Handle case where user clicked on the Apply Zoom button
if (req.getParameter(MapToolkit.FF_ZOOM_BUTTON) != null) {
ht.put(HT_SUBMIT_BUTTON, MapToolkit.FF_ZOOM_BUTTON);
}
// Handle case where user clicked on the Apply button or
// the Cancel button on the Layer Control form.
if (req.getParameter(FF_LS_APPLY) != null) {
ht.put(HT_SUBMIT_BUTTON, FF_LS_APPLY);
}
if (req.getParameter(FF_LS_CANCEL) != null) {
ht.put(HT_SUBMIT_BUTTON, FF_LS_CANCEL);
}
// TODO: If you add more submit buttons to the form,
// add a test here to test for whether the user clicked
// the button; if the user did click the button, call
// ht.put(HT_SUBMIT_BUTTON, name);
// Get the value of the Zoom text field. Note that the user
// can type in a Zoom Width and press Enter instead of
// clicking the Zoom submit button.
param = req.getParameter(MapToolkit.FF_ZOOM_FIELD);
if (param != null) {
try {
double zoom = toolkit.parseFormattedNumber(param).doubleValue();
// Store the zoom value the user entered, but only if
// the number is greater than zero. (The map cannot
// be zoomed to zero miles.)
if (zoom > 0) {
ht.put(MapToolkit.FF_ZOOM_FIELD, new Double(zoom));
}
} catch (Exception e) {
// The contents of the text field could not be converted
// into a number, so we will not store any zoom number.
}
}
// Make note of whether the user clicked a request for
// a special page type, such as Layer Control
param = req.getParameter(FF_REQUEST_TYPE);
if (param != null) {
ht.put(FF_REQUEST_TYPE, param);
}
return ht;
}
/**
* Adjust the map's center point and zoom level, if necessary
* (i.e. if the Hashtable of form field values indicates that
* the user wants the map to be panned or zoomed). If there
* is no need to pan or zoom the map (e.g. if the user simply
* clicked Cancel on the Layer Control page), then do nothing.
*
* @param myMap the MapJ object to update
* @param ht the Hashtable containing information about the user's request
* @param session the HttpSession session used to save the MapJ object
*
*/
private void panAndZoomMap(MapJ myMap, Hashtable ht, HttpSession session)
throws Exception
{
DoublePoint newPoint = getClickLocation(myMap, ht);
Double newZoom = getRequestedZoom(myMap.getZoom(), ht, session);
if (newZoom != null) {
// The user requested a change to the zoom width
myMap.setZoom(newZoom.doubleValue() );
}
if (newPoint != null) {
// The user requested a change to the center
myMap.setCenter(newPoint);
}
}
/*
* Return a DoublePoint object indicating the location (in map
* coordinates) where the user clicked on the map image.
*/
private DoublePoint getClickLocation(MapJ myMap, Hashtable ht)
throws Exception
{
// Retrieve x/y location (on image) of where the user clicked:
DoublePoint screenPoint = (DoublePoint)ht.get(HT_SCREEN_LOCATION);
if (screenPoint != null) {
return myMap.transformScreenToNumeric(screenPoint);
}
return null;
}
/** Return a double indicating the zoom width that the user
* has attempted to apply. The user can apply a zoom width
* by clicking on the map with the zoom in or zoom out tools
* selected; or by clicking the Submit button next to the
* zoom width field; or by pressing ENTER on the zoom width field.
*
* Once we determine which navigation tool the user selected,
* save that tool number using session.putValue.
*
* @return the desired zoom width, OR return null if no valid
* zoom width could be determined.
*/
private Double getRequestedZoom(
double currentZoom, Hashtable ht, HttpSession session) {
Double newZoom = null;
boolean bUserClickedMap = false;
String strSubmitButton = null;
int toolNumber;
// See whether we received x- and y-coordinates from clicking the image map.
if (ht.get(HT_SCREEN_LOCATION) != null) {
// The user did click on the map to submit the form.
bUserClickedMap = true;
// If the selected tool is either Zoom In or Zoom Out,
// then the requested zoom will be a multiple of the current zoom.
String toolName = (String)ht.get(MapToolkit.FF_MAPTOOLS);
if (toolName != null) {
toolNumber = MapToolkit.getToolNumber(toolName);
if (toolNumber == MapToolkit.MAPTOOL_ZOOMIN) {
newZoom = new Double(currentZoom / 2);
}
else if (toolNumber == MapToolkit.MAPTOOL_ZOOMOUT) {
newZoom = new Double(currentZoom * 2);
}
// Now that we've determined which tool the user selected,
// save that information.
session.setAttribute("mapinfo.toolnumber", new Integer(toolNumber));
}
}
else {
// The user did not click on the map; see whether the user
// clicked the Submit button next to the Zoom box.
strSubmitButton = (String)ht.get(HT_SUBMIT_BUTTON);
if (strSubmitButton != null &&
strSubmitButton.equals(MapToolkit.FF_ZOOM_BUTTON)) {
newZoom = (Double)ht.get(MapToolkit.FF_ZOOM_FIELD);
}
}
if (!bUserClickedMap && strSubmitButton == null) {
// The user did not click the map or click on a submit button.
// We will assume that the user typed in a zoom width in the
// text field, and pressed ENTER to submit the form, in which
// case we will read the text field and use it to set the zoom.
newZoom = (Double)ht.get(MapToolkit.FF_ZOOM_FIELD);
}
return newZoom;
}
/**
* Generate an image of the map.
* @param myMap MapJ object representing the map
* @param session HttpSession object representing the session
*
*/
private void renderMap(MapJ myMap, HttpSession session) throws Exception {
// Determine the image file name to use.
// If this is not the first request from this user in
// this session, then re-use the filename they used
// for their last map request.
String filename = (String) session.getAttribute("mapinfo.imageName");
if (filename == null) {
filename = getImageName();
session.setAttribute("mapinfo.imageName", filename);
}
// Add a listener for when this session becomes invalid.
// We will want to delete the image file at this time
CleanUp cln = null; // Clean up the image file
cln = (CleanUp) session.getAttribute("bindings.listener");
if (cln == null) {
// The first time through for this user session:
cln = new CleanUp(m_mapImagePath + filename + m_imageExtension);
session.setAttribute("bindings.listener",cln);
}
// Set up the renderer for this MapJ
String sMIME = getMIMEType();
MapXtremeImageRenderer rr = new MapXtremeImageRenderer(m_mxtURL);
rr.render(
ImageRequestComposer.create(myMap, NUM_OF_COLORS, BACKGROUND_COLOR, sMIME));
rr.toFile(m_mapImagePath + filename + m_imageExtension);
}
// Return a String of HTML that defines a Map page
private String getMapPage(
MapJ myMap, boolean bSmallMap, MapToolkit toolkit,
HttpSession session, HttpServletResponse res) {
StringBuffer sb = new StringBuffer();
int toolNumber;
Random rnd = new Random();
// Output Page header
sb.append("<HTML><HEAD><TITLE>HTMLEmbeddedMapServlet</TITLE></HEAD>");
// Specify white background, black foreground text, etc.
sb.append("<BODY BGCOLOR=WHITE TEXT=BLACK LINK=BLUE VLINK=PURPLE ALINK=RED>");
sb.append("<h2><font FACE=\"Verdana,Geneva,Arial,Helvetica\">HTMLEmbeddedMapServlet</font></h2>");
//Put out MapControls - note encodeURL is for session mngt.
sb.append("<P>\n");
// Output the FORM tag. Note: if using Servlet API 2.0, you must use
// the encodeUrl method. For later APIs, use encodeURL instead.
sb.append("<form method=\"POST\" action=\"" +
res.encodeURL(m_thisServletURL) + "\">");
// Display the mapping elements in a large, 1-row x 2-column table.
sb.append("<table border=\"0\"><tr><td>");
// First cell in the table: display the map.
sb.append("<p><input type=\"image\" name=\"" + FF_MAP_IMAGE + "\" ");
if (!m_bGenerateImageFile) {
// When m_bGenerateImageFile is false, we include a tag on the page
// which creates a second call to the servlet, such as:
// <input type="image" name="foo"
// src="http://localhost/servlets/HTMLEmbeddedMapServlet?reqtype=IMG" ...
// The second call causes the servlet to stream an image down directly.
sb.append(" src=\"" + res.encodeURL(m_thisServletURL +
"?" + FF_REQUEST_TYPE + "=IMG&refresh=" + rnd.nextLong()) + "\"");
}
else {
// Instead of streaming an image directly down,
// we will assume that an image file was created on the server,
// and build an URL that loads the image from the server.
// Note that our tag looks like "file.gif?refresh=<random #>".
// The 'refresh' portion of the tag is not actually used; it
// simply prevents the browser from displaying a cached image.
String filename = (String) session.getAttribute("mapinfo.imageName");
if (filename == null) {
filename = getImageName();
session.setAttribute("mapinfo.imageName", filename);
}
sb.append(" src=\"" + m_mapImageURL +
filename + m_imageExtension + "?refresh=" + rnd.nextLong() + "\"");
}
int iMapWidth;
int iMapHeight;
if (bSmallMap) {
iMapWidth = MAP_WIDTH_SMALL;
iMapHeight = MAP_HEIGHT_SMALL;
} else {
iMapWidth = MAP_WIDTH_LARGE;
iMapHeight = MAP_HEIGHT_LARGE;
}
sb.append(" align=\"bottom\" border=\"0\" WIDTH=\"" + iMapWidth
+ "\"" + " HEIGHT=\"" + iMapHeight + "\"></p>");
// End the cell containing the map; begin the cell that
// will contain the zoom control and other page elements.
sb.append("</td><td VALIGN=\"TOP\">");
// Display the radio buttons that act as map navigation tools.
// But first, determine which radio button was selected previously,
// so that we can automatically re-select that radio button.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -