原文地址:Example of using JavaScript for Google maps in the Delphi XE6
XE6的TWebBrowser新增了EvaluateJavaScript方法,这个方法,允许我们与TWebBrowser已经加载的的网页进行交互。这太方便了!作者用一个具体的实例,演示如何调用Google map的API。
首先,建立一个网页:
const
{ Was used sample from }
{ https://developers.google.com/maps/documentation/javascript/examples/directions-panel }
cRoute =
''
+ ''
+ ' '
+ ' '
+ ' '
+ '
setPanel()
'+ ' '
+ ' '
+ '
+ '
+ ' '
+ ' '
+ '
'
+ ' Start:'
+ '
Chicago
St Louis
Joplin, MO
Oklahoma City
Amarillo
Gallup, NM
Flagstaff, AZ
Winona
Kingman
Barstow
San Bernardino
Los Angeles
'
Chicago
St Louis
Joplin, MO
Oklahoma City
Amarillo
Gallup, NM
Flagstaff, AZ
Winona
Kingman
Barstow
San Bernardino
Los Angeles
'
+ ' End:'
+ '
Chicago
St Louis
Joplin, MO
Oklahoma City
Amarillo
Gallup, NM
Flagstaff, AZ
Winona
Kingman
Barstow
San Bernardino
Los Angeles
'
Chicago
St Louis
Joplin, MO
Oklahoma City
Amarillo
Gallup, NM
Flagstaff, AZ
Winona
Kingman
Barstow
San Bernardino
Los Angeles
'
+ '
'
+ '
'
+ '
'
+ ' '
+ '';
然后,用一个TWebBrowser调入:
WebBrowser1.LoadFromStrings(cRoute, '');
再声明两个常量:
cChicagoWinona =
'document.getElementByIdx_x(''start'').value = "chicago, il";'
+ 'document.getElementByIdx_x(''end'').value = "winona, az";'
+ 'calcRoute();';
cStLouisGallupNM =
'document.getElementByIdx_x(''start'').value = "st louis, mo";'
+ 'document.getElementByIdx_x(''end'').value = "gallup, nm";'
+ 'calcRoute();';
用两个按钮调用:
procedure TForm1.Button1Click(Sender: TObject);
begin
WebBrowser1.EvaluateJavaScript(cChicagoWinona);
end;
http://blog.sina.com.cn/s/blog_44fa172f0101rhfz.html