First, the method of invoking JavaScript
Two kinds:
The first type: The method of using the self-coding function
function Executejavascript (webbrowser:twebbrowser; code:string): Variant;
var//Send Script
Document:ihtmldocument2;
Window:ihtmlwindow2;
Begin
Execute JavaScript in WebBrowser
Document:=webbrowser.document as IHTMLDocument2;
If not Assigned (Document) then Exit;
Window:=document.parentwindow;
If not Assigned (Window) then Exit;
Try
Result:=window.execscript (Code, ' JavaScript ');
Except
On e:exception does raise Exception.create (' Javascript error ' +e.message+ ' in: ' #13 #10+code);
End
End
The second type: Call WebBrowser built-in methods directly:
WebBrowser1.OleObject.document.parentWindow. Method name ();
Second, the Call of Baidu API
Make an HTML file
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/>
<meta name= "viewport" content= "initial-scale=1.0, User-scalable=no"/>
<script type= "Text/javascript" src= "http://api.map.baidu.com/api?v=2.0&ak= your secret key" ></script>
<title> Baidu Map Test </title>
<style type= "Text/css" >
Body, HTML, #allmap {width:100%;height:100%;overflow:hidden;margin:0;}
</style>
<body>
<div id= "Allmap" > </div>
</body>
<script type= "Text/javascript" >
function SetCenter (areaname) {//Set new map Center
Map.centerandzoom (areaname,10);
}
var map = new Bmap.map ("Allmap"); JS Main program, Tune up Baidu map
var point = new Bmap.point (116.404, 39.915);
Map.centerandzoom (point,5);
Map.enablescrollwheelzoom ();
</script>
Third, the implementation of DELPHI method:
WebBrowser1.Navigate (Extractfilepath (application.exename) + ' bmap.html ');
Try
Executejavascript (WebBrowser1, ' SetCenter ("' +centercityname+ '");
Except
On E:exception do ShowMessage (e.message);
End
{
Or:
WebBrowser1.OleObject.document.parentWindow.setcenter (Centercityname);
}
Delphi Call Baidu Map API