Ads 468x60px

Smaller time frame always follow the bigger time frame. It's better wait be patience to enter in position than risk with BIG SL. Having strong trading discipline and taking losses when necessary is a sign of serious trading approach

Wednesday, May 9, 2012

Using the Google Maps API V3 from Delphi

The Google Maps Javascript API Version 2 has been officially deprecated, so it’s time to update to the new version 3, this post shows how you can use the new Google maps V3 API from Delphi.
in this sample application you can use the traffic layer , Bicycling layer and the street View Control to activate the panorama view.
for additional info about the Google maps api v3 you can check these links.


Check the next full commented sample application written in Delphi 2007, the source code is available in this location
001unit fMain;
002
003interface
004
005uses
006  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
007  Dialogs, OleCtrls, SHDocVw, StdCtrls, ExtCtrls, XPMan, ComCtrls,MSHTML;
008
009type
010  TfrmMain = class(TForm)
011    WebBrowser1: TWebBrowser;
012    LabelAddress: TLabel;
013    PanelHeader: TPanel;
014    ButtonGotoLocation: TButton;
015    XPManifest1: TXPManifest;
016    MemoAddress: TMemo;
017    ButtonGotoAddress: TButton;
018    LabelLatitude: TLabel;
019    LabelLongitude: TLabel;
020    Longitude: TEdit;
021    Latitude: TEdit;
022    CheckBoxTraffic: TCheckBox;
023    CheckBoxBicycling: TCheckBox;
024    CheckBoxStreeView: TCheckBox;
025    procedure FormCreate(Sender: TObject);
026    procedure ButtonGotoAddressClick(Sender: TObject);
027    procedure ButtonGotoLocationClick(Sender: TObject);
028    procedure CheckBoxTrafficClick(Sender: TObject);
029    procedure CheckBoxBicyclingClick(Sender: TObject);
030    procedure CheckBoxStreeViewClick(Sender: TObject);
031  private
032    { Private declarations }
033    HTMLWindow2: IHTMLWindow2;
034  public
035    { Public declarations }
036  end;
037
038var
039  frmMain: TfrmMain;
040
041implementation
042
043uses
044   ActiveX;
045
046{$R *.dfm}
047
048const
049HTMLStr: String = //i put The code for the web page page wich load the google maps in a string const, you can use an external html file too or embed the page in a resource and then load in a stream
050' '+
051' '+
052' '+
053' '+
054' '+
118''+
119' '+
120
'+
121''+
122'';
123
124procedure TfrmMain.FormCreate(Sender: TObject);
125var
126  aStream     : TMemoryStream;
127begin
128   WebBrowser1.Navigate('about:blank'); //Set the location to an empty page
129    if Assigned(WebBrowser1.Document) then
130    begin
131      aStream := TMemoryStream.Create; //create a TStem to load the Page from the string
132      try
133         aStream.WriteBuffer(Pointer(HTMLStr)^, Length(HTMLStr)); //Copy the string to the stream
134         //aStream.Write(HTMLStr[1], Length(HTMLStr));
135         aStream.Seek(0, soFromBeginning);
136         (WebBrowser1.Document as IPersistStreamInit).Load(TStreamAdapter.Create(aStream));//Load the page from the stream
137      finally
138         aStream.Free;
139      end;
140      HTMLWindow2 := (WebBrowser1.Document as IHTMLDocument2).parentWindow; //Set the instance of the parentWindow to call the javascripts functions
141    end;
142end;
143
144procedure TfrmMain.ButtonGotoLocationClick(Sender: TObject);
145begin
146   HTMLWindow2.execScript(Format('GotoLatLng(%s,%s)',[Latitude.Text,Longitude.Text]), 'JavaScript');//Call the function GotoLatLng to go the coordinates
147end;
148
149procedure TfrmMain.ButtonGotoAddressClick(Sender: TObject);
150var
151   address    : string;
152begin
153   address := MemoAddress.Lines.Text;
154   address := StringReplace(StringReplace(Trim(address), #13, ' ', [rfReplaceAll]), #10, ' ', [rfReplaceAll]);
155   HTMLWindow2.execScript(Format('codeAddress(%s)',[QuotedStr(address)]), 'JavaScript');//Call the function codeAddress to go the address
156end;
157
158procedure TfrmMain.CheckBoxStreeViewClick(Sender: TObject);
159begin
160    if CheckBoxStreeView.Checked then
161     HTMLWindow2.execScript('StreetViewOn()', 'JavaScript') //Activate the Street View option
162    else
163     HTMLWindow2.execScript('StreetViewOff()', 'JavaScript');//Deactivate the Street View option
164
165end;
166
167procedure TfrmMain.CheckBoxBicyclingClick(Sender: TObject);
168begin
169    if CheckBoxBicycling.Checked then
170     HTMLWindow2.execScript('BicyclingOn()', 'JavaScript')//Activate the Bicycling View option
171    else
172     HTMLWindow2.execScript('BicyclingOff()', 'JavaScript');//Deactivate the Bicycling View option
173 end;
174
175procedure TfrmMain.CheckBoxTrafficClick(Sender: TObject);
176begin
177    if CheckBoxTraffic.Checked then
178     HTMLWindow2.execScript('TrafficOn()', 'JavaScript')//Activate the Traffic View option
179    else
180     HTMLWindow2.execScript('TrafficOff()', 'JavaScript');//Deactivate the Traffic View option
181 end;
182
183end.

Source


comments