addLayer方法参照mapinfo官方公布的代码,做了修正。其目的是增加一个注释图层,显示某一个tab图层上相关图元的辅助信息,该信息存放在其他数据表中。方法的功能是在Annotation layer上创建一个点,并用自己的信息做为label显示。
private void addLayer()
{
try{
Layer newLayer = null;
//String Array that will contain Column Names
String columns[] = {"Name", "Num"};
//int Array that contains the column types
int types[] = { TableInfo.COLUMN_TYPE_STRING,
TableInfo.COLUMN_TYPE_STRING};
int keys[] = {1};
//Get a CoordSys object for use in SimpleTableInfo
CoordSys coord = myMap.getDisplayCoordSys();
//Create a TableInfo Object for the AnnotationDataProviderHelper
TableInfoImpl ti = new TableInfoImpl("MyNewTable", coord, columns, types, 2, keys, false);
//Create the Annotation dp Helper and AnnotationTableDescHelper
AnnotationDataProviderHelper dph = null;
try
{
dph = new AnnotationDataProviderHelper(ti);
}
catch (Exception e)
{
System.out.println("AnnotationDPHelper produced exception:/n" + e);
return;
}
AnnotationTableDescHelper tdh = null;
try
{
tdh = new AnnotationTableDescHelper("MyNewTable");
}
catch (Exception e)
{
return;
}
//Create a MapXtremeDataProviderRef, passing it the AnnoattionDataProviderHelper,
//the Host and Port of the MapServer
LocalDataProviderRef dpr = null;
try
{
dpr = new LocalDataProviderRef(dph);
}
catch (Exception e)
{
System.out.println("LocalDataProviderRef produced exception:/n" + e);
return;
}
//Add this layer to the MapJ Layers collection. Insert() is used versus
//add() because the add method always places the table at the bottom
//of the Collection
try
{
newLayer = myMap.getLayers().insert(dpr, tdh, 0, "Annotation");
}
catch(Exception e)
{
e.printStackTrace();
}
//Add a new feature to the layer
//Create a rendition for the new point
int penWidth = 1;
int penPattern = 2;
Color penColor = Color.blue;
int brushPattern = 68;
Color brushForeground = Color.red;
Color brushBackground = null;
boolean bTransparentBackground = true;
LabelProperties labelProps = new LabelProperties();
labelProps.setLabelExpression("Name"); //SmartLabel being a column name
Rendition labelRend = RenditionImpl.getDefaultRendition();
labelRend.setvalue(Rendition.FONT_FAMILY,"Arial");
labelProps.setRendition(labelRend);
BaseLabelProperties base_label = new BaseLabelProperties(labelProps);
newLayer.setLabelProperties(base_label);
newLayer.setAutoLabel(true);
Rendition rend = new RenditionImpl();
rend = TABStyleFactory.createRegionStyle(
penWidth,
penPattern,
penColor,
brushPattern,
brushForeground,
brushBackground,
bTransparentBackground);
//Rendition rend = new RenditionImpl();
/*
//Set some rendition properties
rend.setvalue(Rendition.FONT_FAMILY, "arial");
//rend.setvalue(Rendition.SYMBOL_MODE, Rendition.SymbolMode.FONT);
rend.setvalue(Rendition.SYMBOL_FOREGROUND, Color.getHSBColor(0, 0, 0));
rend.setvalue(Rendition.FONT_SIZE, new Float(24));
rend.setvalue(Rendition.FONT_STYLE, "arial");
*/
//Create attributes to associate with the new point
Attribute attrib[] = new Attribute[2];
attrib[0] = new Attribute("mbj");
attrib[1] = new Attribute("101");
DoublePoint dPoint = myMap.getCenter();
com.mapinfo.dp.PrimaryKey pk = null;
try
{
pk = new com.mapinfo.dp.PrimaryKey(attrib[1]);
}
catch (Exception e)
{
System.out.println("PrimaryKey encountered exception:/n" + e);
}
//Create the DoublePoint object to be passed to the FeatureFactory
//Now create the Point Feature
Feature newPoint = null;
try
{
newPoint = myMap.getFeatureFactory().createPoint(dPoint, rend, attrib, pk);
System.out.println(newPoint.getAttribute(0));
System.out.println(newPoint.getAttribute(1));
}
catch(Exception e)
{
e.printStackTrace();
}
//After getting a new Feature, add it to the new Layer
try
{
newLayer.addFeature(newPoint);
newLayer.setEnabled(true);
newLayer.setSelectable(true);
newLayer.setAutoLabel(true);
}
catch(Exception e)
{
e.printStackTrace();
}
}catch(Exception e){
e.printStackTrace();
}
}