Delphi+MapX5+Access数据绑定实例如果图片看不到, 可以到我的百度空间看:http://hi.baidu.com/382943850/blog/item/7411e4cb8b7e1ef553664f1d.html从网上搜一下,Delphi+MapX+Access数据绑定的例子不少,但大多没法运行.本人也是初学,经过试验实践,终于成功,总结如下. 之所以公布出来,是希望可以让象我一样的初学者少走点弯路.
(学MapX这些天以来,在几个QQ群上发现一个通病:似乎学MapX的前辈们都很保守, 抱着自己的一点点技术象看家宝贝? 初学者提点问题,大家都不愿回答. 太小气,太以为自己了不起了吧.)
*本文部分代码修改自互联网,部分注释中注明了网上原源码为什么不能运行
关键点:1、表的结构要与TAB的结构相同,或者至少要有一个相对应的索引字段。2、要先创建一个新图层,然后再把DataSet绑定到该图层。3、绑定的方法就是Tmap.Datasets.Add
实例步骤:1、创建MDB文件:2、创建一个TAB图层文件
3、创建ADO链接和查询的代码:procedure TForm1.FormCreate(Sender: TObject);beginwith Self.ADOConnection1 do begin    if Connected then Close;    LoginPrompt := False;    ConnectionString := 'Provider=Microsoft.Jet.OLEDB.4.0;Data Source='      +SysUtils.ExtractFilePath(ParamStr(0))      +'db1.mdb;Persist Security Info=False;Jet OLEDB:Database Password=';end;Self.ADOQuery1.Connection := Self.ADOConnection1;Self.ADOQuery1.SQL.Text := 'select ID, Name, X, Y from MyTab';Self.ADOQuery1.Open;end;
4、绑定的代码:
procedure TForm1.Button1Click(Sender: TObject);varoBLayer : BindLayer;     SearchLayer : Layer;ds : Dataset;beginMap1.Layers.RemoveAll;//先删除a图层。事实上,后来我用一个没有a图层的GST也测试成功,当然,就没必要写RemoveAll这句了oBLayer := coBindLayer.Create;oBLayer.LayerName := 'a';oBLayer.LayerType := miBindLayerTypeXY;//必须使用这个参数才能绑定XY坐标oBLayer.RefColumn1 := 'X';//第一个参数必须指定为横坐标oBLayer.RefColumn2 := 'Y';//纵坐标//添加数据集ds := map1.Datasets.Add(12,//数据集类型,这是miDataSetADO,即ADO专用的                        Self.ADOQuery1.Recordset,//使用这个方法获得ADO中的_Recordset类型                             'MyTab',//数据集名称                             'ID',//传入的是Xunit表中的字段ID的名称                             EmptyParam,                             oBLayer,//BindLayer                             EmptyParam,EmptyParam);//下边将设置新图层的各项属性searchLayer := map1.Layers._Item('a');//LihuaSoft注:应该是_Item而不是Item//字体颜色searchLayer.LabelProperties.Style.TextFontColor := miColorPurple;searchLayer.LabelProperties.Style.TextFontHalo := true;searchLayer.LabelProperties.Style.TextFontBackColor := miColorWhite;//设置图元显示的标签searchLayer.LabelProperties.Dataset := ds;searchLayer.LabelProperties.DataField := ds.Fields._Item('Name'); //长尾兔注:应是_ItemsearchLayer.LabelProperties.LabelZoom := true;{ //设置图层缩放比例范围searchLayer.ZoomMin := 0;searchLayer.ZoomMax := 200;searchLayer.ZoomLayer := true;//设置标签缩放比例范围searchLayer.LabelProperties.LabelZoomMin := 0;searchLayer.LabelProperties.LabelZoomMax := 200;searchLayer.LabelProperties.LabelZoom := true;//自动标记图元searchLayer.AutoLabel := true;}end;
附:
以下是用默认的北美地图做试验:相应的把座标改了一下,位置在墨西哥西北部:以下是整个单元。注意,我为什么把某些语句注掉了
unit Unit1;interfaceuses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, OleCtrls, MapXLib_TLB, DB, ADODB, StdCtrls, ComObj, OleServer;type
  TForm1 = class(TForm)
    Button1: TButton;
    DataSource1: TDataSource;
    ADOConnection1: TADOConnection;
    ADOQuery1: TADOQuery;
    Button2: TButton;
    Button3: TButton;
    Button4: TButton;
    Button5: TButton;
    Button6: TButton;
    Map1: TMap;
    procedure FormCreate(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure Button4Click(Sender: TObject);
    procedure Button5Click(Sender: TObject);
    procedure Button6Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;var
  Form1: TForm1;implementation{$R *.dfm}procedure TForm1.FormCreate(Sender: TObject);
begin
  with Self.ADOConnection1 do begin
    if Connected then Close;
    LoginPrompt := False;
    ConnectionString := 'Provider=Microsoft.Jet.OLEDB.4.0;Data Source='
      +SysUtils.ExtractFilePath(ParamStr(0))
      +'db1.mdb;Persist Security Info=False;Jet OLEDB:Database Password=';
  end;
  Self.ADOQuery1.Connection := Self.ADOConnection1;
  Self.ADOQuery1.SQL.Text := 'select ID, Name, X, Y from MyTab';
  Self.ADOQuery1.Open;
end;procedure TForm1.Button1Click(Sender: TObject);
var
  oBLayer : BindLayer;
  SearchLayer : Layer;
  ds : Dataset;
begin
  //Map1.Layers.RemoveAll;
  oBLayer := coBindLayer.Create;
  oBLayer.LayerName := 'a';
  oBLayer.LayerType := miBindLayerTypeXY;//必须使用这个参数才能绑定XY坐标
  oBLayer.RefColumn1 := 'X';//第一个参数必须指定为横坐标
  oBLayer.RefColumn2 := 'Y';//纵坐标
  //添加数据集
  ds := map1.Datasets.Add(12,//数据集类型,这是miDataSetADO,即ADO专用的
                        Self.ADOQuery1.Recordset,//使用这个方法获得ADO中的_Recordset类型
                             'MyTab',//数据集名称
                             'ID',//传入的是Xunit表中的字段ID的名称
                             EmptyParam,
                             oBLayer,//BindLayer
                             EmptyParam,
EmptyParam);
  //下边将设置新图层的各项属性
  searchLayer := map1.Layers._Item('a');//李华注:是否应该是_Item而不是Item
  //字体颜色
  searchLayer.LabelProperties.Style.TextFontColor := miColorPurple;
  searchLayer.LabelProperties.Style.TextFontHalo := true;
  searchLayer.LabelProperties.Style.TextFontBackColor := miColorWhite;
  //设置图元显示的标签
  searchLayer.LabelProperties.Dataset := ds;
  searchLayer.LabelProperties.DataField := ds.Fields._Item('Name'); //李华注:是否应是_Item
  searchLayer.LabelProperties.LabelZoom := true;
  {//设置图层缩放比例范围
  searchLayer.ZoomMin := 0;
  searchLayer.ZoomMax := 200;
  searchLayer.ZoomLayer := true;}
  //设置标签缩放比例范围
  searchLayer.LabelProperties.LabelZoomMin := 0;
  searchLayer.LabelProperties.LabelZoomMax := 200;
  searchLayer.LabelProperties.LabelZoom := true;
  //自动标记图元
  searchLayer.AutoLabel := true;
end;procedure TForm1.Button2Click(Sender: TObject);
begin
  Map1.CurrentTool := miZoomInTool;
end;procedure TForm1.Button3Click(Sender: TObject);
begin
Map1.CurrentTool := miZoomOutTool;
end;procedure TForm1.Button4Click(Sender: TObject);
begin
Map1.CurrentTool := miPanTool;
end;procedure TForm1.Button5Click(Sender: TObject);
begin
  Map1.CurrentTool := miSelectTool;
end;procedure TForm1.Button6Click(Sender: TObject);
begin
 
end;end.