1. 程式人生 > >百度地圖 API 呼叫 例子

百度地圖 API 呼叫 例子

http://developer.baidu.com/map/jsdemo.htm#i7_1

地址是呼叫方法的例子.

unit Unit1;



interface


uses
  System.SysUtils, System.Types, System.UITypes, System.Classes,
  System.Variants,
  FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs, FMX.WebBrowser,
  FMX.Controls.Presentation, FMX.Edit, FMX.StdCtrls;


type
  TForm1 = class(TForm)
    addr: TEdit;
    WebBrowser1: TWebBrowser;
    CornerButton1: TCornerButton;
    city: TEdit;
    procedure CornerButton1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }


  end;


var
  Form1: TForm1;
  Scity: string;
  Saddr: string;
  s1: string;


const
  s = ' <!DOCTYPE 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" />' +
    ' <style type="text/css"> ' +
    ' body, html,#allmap {width: 100%;height: 100%;overflow: hidden;margin:0;font-family:"微軟雅黑";}'
    + ' </style> ' +
    ' <script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=74c27c8c6c623cfbe0bc80aaYOU KEY "></script>'
    + ' <title>地址解析</title> ' + ' </head>  ' + ' <body> ' +
    ' <div id="allmap"></div> ' + ' </body> ' + ' </html> ';


implementation


{$R *.fmx}
{$R *.LgXhdpiPh.fmx ANDROID}


procedure TForm1.CornerButton1Click(Sender: TObject);
begin
  s1 := 'var map = new BMap.Map("allmap");' +
        'var point = new BMap.Point(116.331398,39.897445);' +
        'map.centerAndZoom(point,15);' + 'map.enableScrollWheelZoom();' +
        'var myGeo = new BMap.Geocoder(); ' +
        'myGeo.getPoint("%s", function(point){ ' + ' if (point) {' +
        'map.centerAndZoom(point, 16); ' +
        'map.addOverlay(new BMap.Marker(point)); ' + '}else{  ' +
        'alert("您選擇地址沒有解析到結果!"); ' + ' } ' + ' }, "%s"); ';
  s1 := format(s1, [addr.text, city.text]);
  WebBrowser1.EvaluateJavaScript(s1);
end;


procedure TForm1.FormCreate(Sender: TObject);
begin
  WebBrowser1.LoadFromStrings(s, 'http://api.map.baidu.com'); // 調入網頁
end;


end.