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

📄 labelclass.java

📁 CreateMyFirstMap
💻 JAVA
字号:
package myfirstmap;
import com.mapinfo.mapj.MapJ;
import com.mapinfo.unit.LinearUnit;
import com.mapinfo.util.DoublePoint;
import com.mapinfo.util.DoubleRect;
import com.mapinfo.mapj.Layers;
import com.mapinfo.mapj.DataLayer;
import com.mapinfo.mapj.FeatureLayer;
import com.mapinfo.mapdefcontainer.MapDefContainer;
import  com.mapinfo.mapdefcontainer.FileMapDefContainer;
import com.mapinfo.mapxtreme.client.MapXtremeImageRenderer;
import com.mapinfo.xmlprot.mxtj.ImageRequestComposer;
import java.awt.Color;
import com.mapinfo.mapj.LabelProperties;
import com.mapinfo.graphics.Rendition;
import com.mapinfo.graphics.RenditionImpl;
import com.mapinfo.labeltheme.OverrideLabelTheme;
import com.mapinfo.mapj.BaseLabelProperties;
import com.mapinfo.unit.Distance;
public class LabelClass
{
  private String mdfFilePath="e:\\MapInfo\\study\\mapmdf";
  private  String mdfFileName="states.mdf";
  private String mapxtremeURL="http://901-32:8083/mapxtreme47/mapxtreme";
  private MapDefContainer mapDefContainer;
  public LabelClass()
  {
  }
public void studyLabelProperty()
{
  MapJ mapj=new MapJ();
  DoublePoint centerPoint=new DoublePoint(-108.8,42.5);
  LinearUnit unit=LinearUnit.kilometer ;
  mapj.setDistanceUnits(unit) ;
  mapj.setDeviceBounds(new DoubleRect(0,0,800,600)) ;
  Layers layers=null;
  DataLayer dataLayer=null;
  DoubleRect rect;
  FeatureLayer featureLayer=null;
  String layerName;
  LabelProperties labelProperties;
  //DoubleRect bound=new DoubleRect(centerPoint,
  try
  {
    mapj.setCenter(centerPoint);
//    mapDefContainer=new FileMapDefContainer(mdfFilePath);
//    mapj.loadMapDefinition(mapDefContainer,mdfFileName);
    mapj.loadMapDefinition(mdfFilePath+"\\"+mdfFileName) ;
    layers=mapj.getLayers() ;
    dataLayer=(DataLayer)layers.get(0) ;
    rect=dataLayer.getBounds() ;
    double width=(rect.xmax -rect.xmin )/8;
    double height=(rect.ymax -rect.ymin )/8;
    mapj.setBounds(new DoubleRect(centerPoint,width,height)) ;
    //
  featureLayer=(FeatureLayer)mapj.getLayers() .get(0) ;
    layerName=featureLayer.getName() ;
   // labelProperties=new LabelProperties();
   labelProperties=featureLayer.getLabelProperties() ;
    //set the label offset and alignments to their default position
    labelProperties.setOffset(new DoublePoint(1d,1d) ) ;//不用(0,0)可以让标签在区域里面,当然最好的办法是
    //求出每个区域的中心,然后用它作为偏移值,不过再做此之前还应该多一道判断手续,那就是该几何体是不是区域
    labelProperties.setHorizontalAlignment(LabelProperties.HORIZ_ALIGN_DEFAULT ) ;
    labelProperties.setVerticalAlignment(LabelProperties.VERT_ALIGN_DEFAULT ) ;
    // Turn the multiline option on and set the columns to be used for labeling.
    labelProperties.setMultiLineTextMode(LabelProperties.MULTILINE_TEXT_ON ) ;
    //labelProperties.setLabelExpression("\"NAME:[\"+State_Name+\"]\\nABBREV:[\"+State+\"]\"");//("\"name:[\"+State_Name+\"]\nabbrev:[\"+State+\"]") ;
    labelProperties.setLabelExpression("\"name:[\"+State+\"]") ;
    //use geometory compute mode
    labelProperties.setGeometryCalculationMode(LabelProperties.GEOMETRY_COMPUTED );//这条语句在这儿设置没有用,我不知道为什么
    //// Set LabelFollowingPath to true to enable label splining
    boolean bFollowPath=true;
//    labelProperties.setLabelFollowingPath(bFollowPath);//我不知道这条语句有什么用
    boolean bfollow=labelProperties.isLabelFollowingPath() ;
    // Set a Rendition for the Labels and create an OverrideLabelTheme in
   // order to display the options set.
    Rendition rend=new RenditionImpl();
    rend.setValue(Rendition.FONT_FAMILY ,"Arial");
    rend.setValue(Rendition.FONT_WEIGHT ,1);
    Distance distance=new Distance(60,    LinearUnit.kilometer );
    rend.setValue(Rendition.FONT_SIZE ,distance);
    rend.setValue(Rendition.FONT_STYLE ,Rendition.FontStyle.ITALIC  );
    rend.setValue(Rendition.SYMBOL_FOREGROUND ,Color.red );
   rend.setValue(Rendition.FILTER_EFFECTS ,Rendition.FilterEffects .HALO );

/*
    Rendition rend=new RenditionImpl();
    rend.setValue(Rendition.SYMBOL_MODE ,Rendition.SymbolMode .FONT );
rend.setValue(Rendition.FONT_FAMILY ,"Mapinfo Cartographic") ;
rend.setValue(Rendition.FONT_SIZE ,16);
rend.setValue(Rendition.SYMBOL_STRING ,String.valueOf((char)33)) ;
*/
    labelProperties.setRendition(rend) ;
//    OverrideLabelTheme overLabelTheme =new OverrideLabelTheme(labelProperties,"Theme Name");//1
//    featureLayer.getLabelThemeList() .add(overLabelTheme) ;//2
   // Get the States Layer, set its LabelProperties object, and turn labeling on.
   //我觉得这下面的两条语句没有必要要,因为labelProperties已经添加到LabelThemeList中去了,我试验了一下
  //用它们其中一个就可以了,即要么将labelProperties添加到LabelThemeList中去(用1,2),要么用featureLayer来直接设置
  //该标签属性(用3,4)
    BaseLabelProperties baseLabel=new BaseLabelProperties(labelProperties);//3
    Distance maxDist=new Distance(100,LinearUnit.kilometer );
    Distance minDist=new Distance(70,LinearUnit.kilometer );
    baseLabel.setZoomMax(maxDist) ;
    baseLabel.setZoomMin(minDist) ;
 //  featureLayer.setLabelProperties(baseLabel) ;//4
    //下面的语句是另外一种标签设置方式,它首先通过getLabelProperties()方法得到该层的标签属性的引用(不是拷贝)
    //这样后面的设置就直接反映到标签属性里去了,没有必要再用 featureLayer.setLabelProperties(baseLabel) 这样的语句了
/*    LabelProperties labelProperties2=featureLayer.getLabelProperties() ;
    Rendition rend2=new RenditionImpl();
    rend2.setValue(Rendition.FONT_STYLE ,Rendition.FontStyle.NORMAL  );
    rend2.setValue(Rendition.FONT_FAMILY ,"Arial");
    rend2.setValue(Rendition.FONT_WEIGHT ,1);
    rend2.setValue(Rendition.FONT_SIZE ,30);
    labelProperties2.setRendition(rend2) ;*/
    //
    featureLayer.setAutoLabel(true) ;//这条语句必须要
    //
    MapXtremeImageRenderer render=new MapXtremeImageRenderer(mapxtremeURL);
    ImageRequestComposer imageRC=ImageRequestComposer.create(mapj,256,Color.white,"image/png");
    render.render(imageRC) ;
    render.toFile("E:\\MapInfo\\study\\png\\labelProperty4.png") ;
    render.dispose() ;
  }catch(Exception e)
  {
    System.out.println("get a "+e.getClass() +"\nwith message "+e.getMessage() ) ;
  }

}
}

⌨️ 快捷键说明

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