delphi 调用百度地图api


本文整理自网络,侵删。

 

一、调用javascript的方法

     两种:

      第一种:采用自编函数的方法

     

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;

 

第二种:直接调用WebBrowser内置方法:

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

 

二、百度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>


<script type="text/javascript">

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>

 

 

三、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);

}

相关阅读 >>

Delphi ioutils 单元(6): tpath(结构体)路径的提取和处理

Delphi 隐藏,显示其他程序的托盘图标

Delphi自有的md5函��

Delphi gb2312 编码转义url字符串

Delphi 链接获取主站地址

Delphi下pos机控制钱箱,客显,打印机

Delphi firedac fdconnection 事务

Delphi 取得当前运行窗口的标题

Delphi设置dbgrid每列自动居中

Delphi fmx jpg 保存数据库 从数据库读取

更多相关阅读请进入《Delphi》频道 >>



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...

    暂无评论...