最近在公司做WebGIS项目,感觉很爽快,将学习到的东西整理一下,供大家交流。
1.环境配置
Web服务器:Apache2048 Servlet:Tomcat4129 GIS开发平台:ArcIMS 9.0 Java编译环境:Eclipse2.上述环境设置好之后,进入ArcIMS开发阶段,主要的工作分三块:java类/jsp/javascript 用Struts 开发实质上将请求和处理完全隔离,jsp网页中只需要写与action对应的form,这些Action 通过struts-config.xml和jsp网页当中的form等对应起来. 下文主要按照功能对代码实现进行研究 初始化地图 InitMap Action: 需要用到的核心类:import com.esri.aims.mtier.io.ConnectionProxy和import com.esri.aims.mtier.model.map.Map *************代码********* ConnectionProxy conn = null; Map map = null; try { conn = new ConnectionProxy(); map = new Map(); conn.setHost(host);//ArcIMS服务器的名称或者IP conn.setConnectionType(connectionType); conn.setPort(port);//ArcIMS服务器的端口 conn.setService(service);//需要调用的ArcIMS服务器的服务名称 conn.setDisplayMessages(displayMessages); map.initMap(conn, 0, true, true, true, true);//初始化地图 //地图和图例的风格设置 map.setWidth(width); map.setHeight(height); map.getLegend().setFont("宋体"); map.getLegend().setAntialiasing(false); map.getLegend().setTitle("图例"); map.getLegend().setTitleFontSize(18); map.getLegend().setLayerFontSize(12); map.getLegend().setValueFontSize(10); map.getLegend().setAutoExtend(true); map.getLegend().setWidth(125); map.getLegend().setCellSpacing(7); //获取地图的全图范围和一些参数,并且传送给客户端 Envelope extent = map.getEnvelope(); double minx = extent.getMinX(); double miny = extent.getMinY(); double maxx = extent.getMaxX(); double maxy = extent.getMaxY(); double mapXDistance = maxx - minx; double mapYDistance = maxy - miny; double doubleWidth = Double.parseDouble(Long.toString(width)); double doubleHeight = Double.parseDouble(Long.toString(height)); double mapRatio = (maxx - minx) / (maxy-miny); double windowRatio = doubleWidth / doubleHeight; double mapHeight = (windowRatio/mapRatio) * doubleHeight; double upperHeight = (doubleHeight - mapHeight) / 2; double distancePerPixel = mapXDistance / doubleWidth; double mapMaxY = maxy + distancePerPixel * upperHeight; double mapMinY = miny - distancePerPixel * upperHeight; //将地图的全图范围传递到客户端 request.setAttribute("fullMinX", new Double(extent.getMinX())); request.setAttribute("fullMinY", new Double(mapMinY)); request.setAttribute("fullMaxX", new Double(extent.getMaxX())); request.setAttribute("fullMaxY", new Double(mapMaxY)); //将地图的当前范围传递到客户端 request.setAttribute("minX", new Double(extent.getMinX())); request.setAttribute("minY", new Double(mapMinY)); request.setAttribute("maxX", new Double(extent.getMaxX())); request.setAttribute("maxY", new Double(mapMaxY)); //告知客户端这是在初始化地图 request.setAttribute("initMap", "true"); //获取地图图片的 mapUrl和图例了legendurl request.setAttribute("mapUrl", map.getMapOutput().getURL()); request.setAttribute("legendUrl", map.getLegend().getLegendOutput() .getURL()); //将Map对象放入Session中,以后在这个对话中一直使用这个map对象来生成地图 request.getSession().setAttribute("map", map); request.getSession().setAttribute("fullExtent", extent); }catch(){}return mapping.findForward("ConetentFrame");//将网页重定向到ConetentFrame
ConetentFrame对应的content.jsp里面只需要写一个form,对应这个Action类InitMap
就可以初始化地图并获取相关的参数。在content.jsp中,获取地图的参数,并赋给客户端。
<script language="JavaScript" type="text/javascript"> var m = parent.mapFrame; //<%//初始化地图时,获得地图的初始化的全图范围if (initMap != null){ %> m.fullMinX = <%=(Double)request.getAttribute("fullMinX")%>; m.fullMinY = <%=(Double)request.getAttribute("fullMinY")%>; m.fullMaxX = <%=(Double)request.getAttribute("fullMaxX")%>; m.fullMaxY = <%=(Double)request.getAttribute("fullMaxY")%>; m.fullOVLeft = m.fullMinX; m.fullOVRight = m.fullMaxX; m.fullOVTop = m.fullMaxY; m.fullOVBottom = m.fullMinY; m.fullOVWidth = Math.abs(m.fullOVRight - m.fullOVLeft); m.fullOVHeight = Math.abs(m.fullOVTop - m.fullOVBottom);<%}