我注意到在使用Delphi的Google Map API创建标记时出现了一些奇怪的行为。我可以很容易地重现问题,但没有解释。在GMMap上创建标记失败
在下面的代码中,您将看到一个CreatePoint方法,我在TButton的OnClick事件中调用该方法。标记是按照它应该创建的。
但是,然后我在IdHTTPServer的OnCommandGet事件中使用相同的参数调用SAME createpoint方法。然后我用Curl触发事件。但是,然后标记不会创建,我得到的消息:“地址5548985C模块'mshtml.dll'中的访问冲突。阅读地址00000144”
我不明白为什么这可能会给出不同的结果。任何想法 ?我正在使用Delphi XE,因此我在运行时创建TWebBrowser(因为它不在XE的工具面板中)。
代码如下,示例项目可以下载here。
unit main;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, IdBaseComponent, IdComponent, IdCustomTCPServer, IdCustomHTTPServer,
IdHTTPServer, GMClasses, GMMap, GMMapVCL, ExtCtrls,
OleCtrls, SHDocVw, StdCtrls, GMLinkedComponents, GMMarker, GMMarkerVCL,
GMConstants, IdContext;
type
DeviceRange = 0..15;
TModWallyForm = class(TForm)
Panel1: TPanel;
GMMap1: TGMMap;
IdHTTPServer1: TIdHTTPServer;
GMMarker1: TGMMarker;
Button4: TButton;
procedure FormCreate(Sender: TObject);
procedure GMMap1AfterPageLoaded(Sender: TObject; First: Boolean);
procedure Button4Click(Sender: TObject);
procedure IdHTTPServer1CommandGet(AContext: TIdContext;
ARequestInfo: TIdHTTPRequestInfo; AResponseInfo: TIdHTTPResponseInfo);
private
{ Private declarations }
WebBrowser1 : TWebBrowser;
Marker : TGMMarker;
procedure CreatePoint(DeviceID : String; Longitude, Latitude : Real);
public
{ Public declarations }
end;
var
ModWallyForm: TModWallyForm;
implementation
{$R *.dfm}
procedure TModWallyForm.FormCreate(Sender: TObject);
var
i : Integer;
begin
WebBrowser1 := TWebBrowser.Create(Panel1);
TControl(WebBrowser1).Parent := Panel1;
WebBrowser1.Align := alClient;
GMMap1.WebBrowser := WebBrowser1;
// Instantiate Markers
Marker := TGMMarker.Create(nil);
Marker.Map := GMMap1;
IdHTTPServer1.Active := True;
end;
procedure TModWallyForm.GMMap1AfterPageLoaded(Sender: TObject; First: Boolean);
begin
if First then
begin
GMMap1.DoMap;
end;
end;
procedure TModWallyForm.Button4Click(Sender: TObject);
begin
CreatePoint('15',4.77,50.55900);
end;
procedure TModWallyForm.IdHTTPServer1CommandGet(AContext: TIdContext;
ARequestInfo: TIdHTTPRequestInfo; AResponseInfo: TIdHTTPResponseInfo);
begin
CreatePoint('15',4.77,50.55900);
end;
procedure TModWallyForm.CreatePoint(DeviceID: string; Longitude: Real; Latitude: Real);
begin
with Marker.Add(Latitude, Longitude) do
begin
MarkerType := mtColored;
InfoWindow.HTMLContent := DeviceID;
ColoredMarker.Width := 48+(Index*20);
ColoredMarker.Height := 48;
ColoredMarker.PrimaryColor := clBlack;
ColoredMarker.StrokeColor := clBlack;
Title := ''; // Avoid showing the title when the mouse hovers over the marker
end;
GMMap1.RequiredProp.Center.Lat := Latitude;
GMMap1.RequiredProp.Center.Lng := Longitude;
GMMap1.RequiredProp.Zoom := 8;
end;
end.
如果未调用构造函数,您甚至不会看到地图或表单上的其他任何内容。 – TLama
地图显示正常。只要我使用TButton创建标记,应用程序就会工作。 – Soitjes