⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 maphandle.java

📁 一个很有特点的地图平台,可以以动画方试展现电子地图,有拉近,拉远,滑动功能,最主要的是它是一个地图维护台,处理地图到数据库的数据导入
💻 JAVA
字号:
package mapcenter.weihu;

import java.io.*;
import java.util.*;
import java.awt.*;
import java.awt.image.*;
import com.mapinfo.mapj.*;
import com.mapinfo.util.*;
import com.mapinfo.xmlprot.mxtj.*;
import com.mapinfo.coordsys.*;
import com.mapinfo.dp.annotation.*;
import com.mapinfo.dp.util.*;
import com.mapinfo.dp.*;
import com.mapinfo.graphics.*;
import mapcenter.service.*;

public class MapHandle {
  public static com.mapinfo.unit.LinearUnit Unit_Meter = com.mapinfo.unit.LinearUnit.meter;
  public static com.mapinfo.unit.LinearUnit Unit_Kilometer = com.mapinfo.unit.LinearUnit.kilometer;
  private static final Log log = LogFactory.getLog("WebGIS");
  private ImageRequestComposer imageRC;
  private LocalRenderer renderer;
  private BufferedImage bufferImage;
  private double SearchZoom, containWidth, containHeight;
  private ArrayList rulerKeys = new ArrayList();
  private PrimaryKey tongJiKey, SearchKey;
  public FeatureFactory featureFactory;
  private File MapFile = null;
  private MapJ myMap;

  public void init(Component contain)throws Exception{
    //============== 读取配置参数 ==========================
    String MapPath = MapContext.getInitParameter("MapPath");
    SearchZoom = Double.valueOf(MapContext.getInitParameter("SearchZoom")).doubleValue();
    containWidth = contain.getWidth();
    containHeight = contain.getHeight();
    if(MapPath != null)this.loadMap(MapPath);
  }

  public void loadMap(String MapPath) throws Exception {
    //=============== 加载地图文件 ===========================
    this.myMap = new MapJ();
    this.MapFile = new File(MapPath);
    String extension = ExtensionFilter.getExtension(MapFile);
    if(extension != null){
      if (extension.equals("gst")) {
        myMap.loadGeoset(MapFile.getPath(), MapFile.getParent(), null);
      }
      else if (extension.equals("mdf")) {
        myMap.loadMapDefinition(MapFile.getPath());
      }
    }
    myMap.setZoom(myMap.getZoom() * 3);
    featureFactory = myMap.getFeatureFactory();
    //============== 创建图像请求器,渲染器 ================================
    myMap.setDeviceBounds(new DoubleRect(0, 0, containWidth * 3, containHeight * 3));
    bufferImage = new BufferedImage((int)containWidth * 3, (int)containHeight * 3, BufferedImage.TYPE_INT_RGB);
    renderer = new LocalRenderer(bufferImage);
    imageRC = ImageRequestComposer.create(myMap, 256, Color.white, "image/gif");
    //============== 创建动态图层 ==================
    crateAnnotLayer();
  }

  //==============================================
  public MapJ getMapJ() throws Exception {
    if (myMap == null)throw new Exception("MapJ没有初始化");
    return myMap;
  }

  public String getMapPath(){
    return MapFile.getPath();
  }
  public void crateAnnotLayer() throws Exception {
    //============== 创建临时标注图层 ==============================
    AnnotationTableDescHelper annTableDesc = new AnnotationTableDescHelper("临时标注图层");
    AnnotationDataProviderHelper dpHelper = new AnnotationDataProviderHelper();
    LocalDataProviderRef dpRef = new LocalDataProviderRef(dpHelper);
    FeatureLayer annotLayer = myMap.getLayers().insert(dpRef, annTableDesc, 0, "临时标注图层");
    //================= 标注样式设置 =============================
    LabelProperties labelProp = annotLayer.getLabelProperties();
    com.mapinfo.graphics.Rendition labelRend = labelProp.getRendition();
    labelRend.setValue(labelRend.FONT_WEIGHT, 1f);
    labelRend.setValue(labelRend.SYMBOL_FOREGROUND, Color.black);
    //labelRend.setValue(labelRend.FONT_STYLE,labelRend.FontStyle.NORMAL);
    labelProp.setRendition(labelRend);
    labelProp.setHorizontalAlignment(labelProp.HORIZ_ALIGN_CENTER);
    labelProp.setDuplicationAllowed(true);
    labelProp.setOverridePriority(true);
    labelProp.setLabelFollowingPath(true);
    labelProp.setLabelColumn(1);
    BaseLabelProperties baseLabel = new BaseLabelProperties(labelProp);
    annotLayer.setLabelProperties(baseLabel);
    annotLayer.setAutoLabel(true);
  }

  //================== 平移地图 =================
  public void panMap(double screenx, double screeny) throws Exception {
    //DoublePoint screenpoint = new DoublePoint(screenx, screeny);
    //DoublePoint numberpoint = myMap.transformScreenToNumeric(screenpoint);
    //myMap.setCenter(numberpoint);
    myMap.offset(screenx, screeny);
  }

  //================== 放大缩小操作 ==================
  public void zoomMap(double screenx, double screeny, double zoomPen) throws Exception {
    double zoomValue;
    DoublePoint screenpoint = new DoublePoint(screenx, screeny);
    DoublePoint numberpoint = myMap.transformScreenToNumeric(screenpoint);
    if (zoomPen == 0) zoomPen = 0.5;
    zoomValue = myMap.getZoom() * zoomPen;
    myMap.setZoomAndCenter(zoomValue, numberpoint);
  }

  public void zoomtoMap(double screenX1, double screenY1, double screenX2,double screenY2) throws Exception {
    DoublePoint ScreenLeftpoint = new DoublePoint(screenX1, screenY1);
    DoublePoint ScreenRightpoint = new DoublePoint(screenX2, screenY2);
    DoublePoint numberLeftpoint = myMap.transformScreenToNumeric(ScreenLeftpoint);
    DoublePoint numberRightpoint = myMap.transformScreenToNumeric(ScreenRightpoint);
    DoubleRect bounds = new DoubleRect(numberLeftpoint, numberRightpoint);
    myMap.setBounds(bounds);
  }

  //================== 全图显示 ====================
  public void fullExtend() throws Exception {
    DoubleRect maxBound = new DoubleRect(0, 0, 0, 0);
    DoubleRect currentRect = null;
    Layers layers = myMap.getLayers();
    int layerCount = layers.size();
    for (int i = 0; i < layerCount; i++) {
      try {currentRect = layers.elementAt(i).getBounds();}catch(Exception e) {}
      if (currentRect == null)continue;
      if (currentRect.width() > maxBound.width()) {
        maxBound = currentRect;
      }
    }
    if(maxBound.equals(new DoubleRect(0,0,0,0)))return;
    myMap.setBounds(maxBound);
    myMap.setZoom(myMap.getZoom()*3);
  }

  //======== 返回渲染地图文件名 =======
  public Image render() throws Exception {
    if (renderer == null) log.debug("renderer is null");
    if (imageRC == null) log.debug("imageRC is null");
    renderer.render(imageRC);
    return (Image) bufferImage;
  }

  public Image getImg() {
    return (Image) bufferImage;
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -