delphi 調用百度地圖api

1、調用javascript的方法javascript

     兩種:css

      第一種:採用自編函數的方法html

     

function ExecuteJavaScript(WebBrowser:TWebBrowser; Code: string):Variant;
var //發送腳本
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 do raise Exception.Create('Javascript error '+E.Message+' in: '#13#10+Code);
end;
end;java

 

第二種:直接調用WebBrowser內置方法:web

    WebBrowser1.OleObject.document.parentWindow.方法名();api

 

2、百度API的調用函數

     製做一個HTML文件測試

     

<html>
<head>
<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=你的祕鑰"></script>
<title>百度地圖測試</title>
<style type="text/css">
body, html,#allmap {width: 100%;height: 100%;overflow: hidden;margin:0;}
</style>
</head>
<body>
<div id="allmap"> </div>

</body>
</html>ui


<script type="text/javascript">spa

function setcenter(areaname){  //設置新的地圖中心

  map.centerAndZoom(areaname,10);
}

var map = new BMap.Map("allmap"); //js 主程序,調起百度地圖
var point = new BMap.Point(116.404, 39.915);
map.centerAndZoom(point,5);
map.enableScrollWheelZoom();

</script>

 

 

3、DELPHI 中的實現方法:

    

WebBrowser1.Navigate(ExtractFilePath(Application.ExeName)+'bmap.html');

try
ExecuteJavaScript(WebBrowser1,'setcenter("'+CenterCityname+'");');
except
on E:Exception do showmessage(E.Message);
end;

{

或者:

WebBrowser1.OleObject.document.parentWindow.setcenter(CenterCityname);

}

相關文章
相關標籤/搜索