📄 mapperservlet.java
字号:
package com.oyc.mapxtreme.servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import com.mapinfo.mapj.MapJ;
import com.mapinfo.unit.LinearUnit;
import com.mapinfo.util.DoublePoint;
import com.mapinfo.util.DoubleRect;
import com.oyc.mapxtreme.util.MapConfig;
import com.oyc.mapxtreme.util.RenderMap;
/**
* servlet:根据客户端Applet的请求生成相应的地图,包括放大处理,缩小处理等
* @author 三峡大学理学院 欧阳超
*
*/
public class MapperServlet extends BaseHttpServlet {
//地图属性对象
private MapConfig mapCfg = MapConfig.getInstance();
//地图的原始视野值
private double oldZoom;
/**
* 全图显示
*
*/
public void fullMap(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try {
//取出MapJ对象,如果不存在,则创建一个
HttpSession session = request.getSession(true);
MapJ myMap = (MapJ) session.getAttribute("mapj");
if (myMap == null) { //等于null,说明是第一次访问
//加载地图配置文件
this.mapCfg.config();
//初始化地图
myMap = initMapJ();
} else {
myMap.setZoomAndCenter(oldZoom, new DoublePoint(0, 0));
}
//渲染MapJ,输出
RenderMap render = new RenderMap();
render.renderMap(myMap, response);
//将MapJ对象存入Session
session.setAttribute("mapj", myMap);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 用放大或缩小按钮单击或拖动Applet中的地图时,缩放地图
*
*/
public void zoom(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try {
//存储Applet传过来的参数
double ptx = Double.parseDouble(request.getParameter("ptx"));
double pty = Double.parseDouble(request.getParameter("pty"));
double zoom = Double.parseDouble(request.getParameter("zoom"));
//取出MapJ对象
HttpSession session = request.getSession();
MapJ myMap=(MapJ) session.getAttribute("mapj");
//创建地图中心坐标
DoublePoint point=new DoublePoint(ptx, pty);
DoublePoint newpoint=myMap.transformScreenToNumeric(point);
//重置地图视野和中心
myMap.setZoomAndCenter(zoom, newpoint);
//渲染MapJ,输出
RenderMap render = new RenderMap();
render.renderMap(myMap, response);
//将MapJ对象存入Session
session.setAttribute("mapj", myMap);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 平移地图
*
*/
public void pan(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try {
//存储Applet传过来的参数
double ptx=Double.parseDouble(request.getParameter("ptx"));
double pty=Double.parseDouble(request.getParameter("pty"));
//取出MapJ对象
HttpSession session = request.getSession();
MapJ myMap=(MapJ) session.getAttribute("mapj");
//创建地图中心坐标
DoublePoint point=new DoublePoint(ptx, pty);
DoublePoint newpoint=myMap.transformScreenToNumeric(point);
//重置地图中心
myMap.setCenter(newpoint);
//渲染MapJ,输出
RenderMap render = new RenderMap();
render.renderMap(myMap, response);
//将MapJ对象存入Session
session.setAttribute("mapj", myMap);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 初始化MapJ
*
*/
private MapJ initMapJ() throws Exception {
MapJ myMap = new MapJ();
myMap.setDeviceBounds(new DoubleRect(0, 0, this.mapCfg.getMapwidth(), this.mapCfg.getMapheight()));
//加载地图
try {
if (this.mapCfg.getFiletoload().endsWith(".gst")) {
myMap.loadGeoset(this.mapCfg.getFiletoload(), this.mapCfg.getMappath(), null);
} else {
myMap.loadMapDefinition(this.mapCfg.getFiletoload());
}
} catch (Exception e) {
System.out.println("Can't load geoset: " + this.mapCfg.getFiletoload() + "\n");
throw e;
}
myMap.setDistanceUnits(LinearUnit.kilometer);
//记下地图的初始zoom值
this.oldZoom = myMap.getZoom();
System.out.println("map old zoom: " + this.oldZoom);
return myMap;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -