WebGIS小结之九(新增图层)

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();

    }

  }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值