2013-11-26 56 views
1

我注意到在使用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. 

回答

0

确保你的构造(TModWallyForm.FormCreate)调用之前OnCommandGet否则你的“标记”是零

+0

如果未调用构造函数,您甚至不会看到地图或表单上的其他任何内容。 – TLama

+0

地图显示正常。只要我使用TButton创建标记,应用程序就会工作。 – Soitjes

0

我做了一些测试,我看到,当尝试执行的AV升高加载HTML页面的JavaScript函数。组件的代码是这样

function TGMMap.ExecuteScript(NameFunct, Params: string): Boolean; 
var 
    Doc2: IHTMLDocument2; 
    Win2: IHTMLWindow2; 
begin 
    Result := Check; 

    if not (FWebBrowser is TWebBrowser) then Exit; 

    if not Result then Exit; 

    Doc2 := TWebBrowser(FWebBrowser).Document as IHTMLDocument2; 
    Win2 := Doc2.parentWindow; // <==== FAIL ON THIS LINE 

    Win2.execScript(NameFunct + '(' + Params + ')', 'JavaScript'); 

    if MapIsNull then 
    raise Exception.Create(GetTranslateText('El mapa todavía no ha sido creado',  Language)); 
end; 

我确实做了一个试验,没有任何GMLib成分,只有TWebBrowser(是的,它在XE存在;-)),一个按钮“做地图”和IdHTTPServer创建该标记具有相同的结果。

为此,我推断问题不在GMLib组件中。您可以从here下载演示。

+0

有效!它仍然让我不知道为什么发生这种情况,以及是否有解决方法。 TWebBrowser中可能存在一个错误? – Soitjes

+0

至于工具选项板中的TWebBrowser:我必须纠正我的陈述,因为我使用的是Delphi XE Starter,它在工具选项板中没有TWebBrowser。 – Soitjes

+0

我想我必须发表一个新的问题来重述这个问题?因为它与GMLib组件无关。 – Soitjes