Summary
Instructions provided describe how to implement geocoding with the StreetMap USA data with Delphi 6.
Note:
Contact the local ESRI regional office about the keycode for using StreetMap data.
Procedure
Follow the steps below.
- Open Delphi IDE.
1. Navigate to Component > Install ActiveX Control...
2. Select ESRI MapObjects 2.2 (Version 2.0) and click Install...
3. Accept all default value in the upcoming dialog boxes.
- Add the MapObjects ActiveX Map Control to Delphi.
- Drag and Drop the MapObjects Control to the form.
Add the following:
- Two labels
- Two edit boxes
- One combo box
- One button
A. Set one label's caption to 'Street Number:'
B. Set the other to 'Zip Code:'
C. Set the button's caption to 'match'.
![[O-Image] Layout](https://webapps-cdn.esri.com/CDN/support-site/technical-articles-images/000006616/00N39000003LL2C-0EM39000000wcna.png)
- Replace the existing code in 'unit1' using the following process.
In Object Inspector, double-click Form1's onShow event handler to let it be automatically set to FormShow. Perform the same process to Button1's onClick, ComboBox1's onClick, Map1's onBeforeLayerDraw and onMouseDown.
Note:
The auto format wraps the lines in the code improperly. Unwrap them to run the code.
unit unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, OleCtrls, MapObjects2_TLB, ComObj, StdCtrls;
type
TForm1 = class(TForm)
Map1: TMap;
Label1: TLabel;
Edit1: TEdit;
Label2: TLabel;
Edit2: TEdit;
Button1: TButton;
ComboBox1: TComboBox;
procedure FormShow(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure Map1BeforeLayerDraw(Sender: TObject; index: Smallint;
hDC: Cardinal);
procedure ComboBox1Click(Sender: TObject);
procedure Map1MouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
MOGeoCoder : IMoGeocoder;
MOStandard : IMoStandardizer;
function AddDataLayers : IMoGeoDataset;
procedure FreshMap;
implementation
{$R *.dfm}
function AddDataLayers : IMoGeoDataset;
var
DConn : IMoDataConnection;
GeoDS : IMoGeoDataset;
MapLayer : IMoMapLayer;
MapLayer1 : IMoMapLayer;
begin
DConn := IMoDataConnection(CreateOleObject('MapObjects2.DataConnection'));
DConn.Database := 'D:\Data\states';
if not DConn.Connect then
begin
ShowMessage('Cannot connect to D:\Data\states');
exit;
end;
GeoDS := DConn.FindGeoDataset('States');
if VarIsEmpty(GeoDS) then
begin
ShowMessage('Cannot find dataset States');
exit;
end;
MapLayer := IMoMapLayer(CreateOleObject('MapObjects2.MapLayer'));
MapLayer.GeoDataset := GeoDS;
MapLayer.Visible := true;
Form1.Map1.Layers.Add(MapLayer);
DConn.Disconnect;
DConn.Database := '[StreetMap]D:\Data\Streets';
if not DConn.Connect then
begin
ShowMessage('Cannot find dataset streets');
exit;
end;
GeoDS := DConn.FindGeoDataset('usa');
if VarIsEmpty(GeoDS) then
begin
ShowMessage('Cannot find dataset usa');
exit;
end;
MapLayer1 := IMoMapLayer(CreateOleObject('MapObjects2.MapLayer'));
MapLayer1.GeoDataset := GeoDS;
AddDataLayers := GeoDS;
MapLayer1.Visible := false;
form1.Map1.Layers.Add(MapLayer1);
end;
procedure TForm1.FormShow(Sender: TObject);
var
GDSStreets : IMoGeoDataset;
begin
map1.EnableStreetMap('Your Keycode here');
GDSStreets := AddDataLayers;
if VarIsEmpty(GDSStreets) then
begin
ShowMessage('Cannot execute function AddDataLayers');
exit;
end;
MOStandard := IMoStandardizer(CreateOleObject('MapObjects2.Standardizer'));
MOStandard.StandardizingRules := 'C:\Program Files\ESRI\MapObjects2\Georules\stmap.stn';
if not MOStandard.Valid then
begin
ShowMessage('Cannot create IMOStandardizer object.');
exit;
end;
MOGeoCoder := IMoGeocoder(CreateOleObject('MapObjects2.Geocoder'));
MOGeoCoder.Standardizer := MOStandard;
MOGeoCoder.StreetTable := GDSStreets;
MOGeoCoder.MatchRules := 'C:\Program Files\ESRI\MapObjects2\Georules\stmap.mat';
if not MOGeoCoder.Valid then
begin
ShowMessage('Cannot create IMoGeocoder object');
exit;
end;
Map1.TrackingLayer.SymbolCount := 1;
map1.TrackingLayer.Symbol[0].SymbolType := moPointSymbol;
map1.TrackingLayer.Symbol[0].Style := moCircleMarker;
map1.TrackingLayer.Symbol[0].Color := moBlue;
edit1.Text := '380 New York Street';
edit2.Text := '92373';
ComboBox1.Clear;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
i : Integer;
Count : Integer;
begin
ComboBox1.Clear;
if Edit1.Text <> '' then
begin
MOStandard.StandardizeAddress(Edit1.Text);
MOStandard.FieldValue['ZP'] := Edit2.Text;
MOGeoCoder.GenerateCandidates;
if MOGeoCoder.CandidateCount = 0 then
begin
ComboBox1.Items.Add('<No candidates>');
end
else
begin
Count := MOGeoCoder.CandidateCount - 1;
for i := 0 to Count do
begin
ComboBox1.Items.Add(MOGeoCoder.Candidate[i]);
end;
end;
ComboBox1.ItemIndex := 0;
FreshMap;
end;
end;
procedure TForm1.Map1BeforeLayerDraw(Sender: TObject; index: Smallint;
hDC: Cardinal);
var
lyr : IMoMaplayer;
layer : IMoMaplayer;
visibility : wordbool;
begin
if index = 0 then
begin
lyr := IMoMapLayer(Map1.Layers.Item(index));
if Map1.Extent.Width < 0.5 then
lyr.Visible := True
else
lyr.Visible := False;
layer := IMoMapLayer(Map1.Layers.Item(index));
visibility := layer.Visible;
end;
end;
procedure TForm1.ComboBox1Click(Sender: TObject);
begin
FreshMap;
end;
procedure FreshMap;
var
Location : IMoAddressLocation;
begin
if Form1.ComboBox1.ItemIndex > -1 then
begin
Location := MOGeoCoder.LocateCandidate(Form1.ComboBox1.ItemIndex);
if not VarIsEmpty(Location) then
begin
Form1.Map1.FlashShape(Location.location, 3);
Form1.Map1.TrackingLayer.AddEvent(Location.location, 0);
Form1.map1.Refresh;
end;
end;
end;
procedure TForm1.Map1MouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
if Button = mbLeft then
Map1.Extent := Map1.TrackRectangle
else
map1.Pan;
end;
end.