[英]Displaying a location in TWebBrowser (Google Maps) without having to click a button
如何在Google Maps中顯示位置,而無需單擊Delphi / Windows應用程序中的按鈕? 當用戶單擊“客戶”表單上的“顯示位置”按鈕時,我試圖傳遞一個地址。 當前,如果用戶單擊“顯示位置”按鈕,則會傳遞地址,但是在打開ViewMaps表單時不會顯示該位置。 但是,一旦顯示了ViewMaps(TWebBrowser)表單,並在“備注”框中顯示了“ 1 Infinite Loop,Cupertino,CA 95014”地址,用戶可以單擊“ GoToAddressBtn”按鈕,然后將顯示位置。
那么,如何在用戶不必單擊ViewMaps表單上的“ GoToAddressBtn”按鈕的情況下,在Google地圖上顯示該位置呢?
implementation
Uses ShredMain, ActiveX;
{$R *.dfm}
const
HTMLStr: AnsiString =
'<html> '+
'<head> '+
'<meta name="viewport" content="initial-scale=1.0, user-scalable=yes" /> '+
//'<meta http-equiv="X-UA-Compatible" content="IE=edge" />'+
'<script type="text/javascript" src="http://maps.google.com/maps/api/js?v=3"></script> '+
'<script type="text/javascript"> '+
''+
''+
' var geocoder; '+
' var map; '+
//' var address; '+
' var trafficLayer;'+
' var bikeLayer;'+
' var markersArray = [];'+
''+
''+
' function initialize() { '+
' geocoder = new google.maps.Geocoder();'+
' var latlng = new google.maps.LatLng(40.714776,-74.019213); '+
//' var address = "San Diego, CA";'+
' var myOptions = { '+
' zoom: 11, '+
' center: latlng, '+
' mapTypeId: google.maps.MapTypeId.ROADMAP '+
' }; '+
' map = new google.maps.Map(document.getElementById("map_canvas"), myOptions); '+
' trafficLayer = new google.maps.TrafficLayer();'+
' bikeLayer = new google.maps.BicyclingLayer();'+
' map.set("streetViewControl", false);'+
' } '+
''+
''+
' function codeAddress(address) { '+
' if (geocoder) {'+
' geocoder.geocode( { address: address}, function(results, status) { '+
' if (status == google.maps.GeocoderStatus.OK) {'+
' map.setCenter(results[0].geometry.location);'+
' PutMarker(results[0].geometry.location.lat(), results[0].geometry.location.lng(), results[0].geometry.location.lat()+","+results[0].geometry.location.lng());'+
' } else {'+
' alert("Geocode was not successful for the following reason: " + status);'+
' }'+
' });'+
' }'+
' }'+
''+
''+
' function GotoLatLng(Lat, Lang) { '+
' var latlng = new google.maps.LatLng(Lat,Lang);'+
' map.setCenter(latlng);'+
' PutMarker(Lat, Lang, Lat+","+Lang);'+
' }'+
''+
''+
'function ClearMarkers() { '+
' if (markersArray) { '+
' for (i in markersArray) { '+
' markersArray[i].setMap(null); '+
' } '+
' } '+
'} '+
''+
' function PutMarker(Lat, Lang, Msg) { '+
' var latlng = new google.maps.LatLng(Lat,Lang);'+
' var marker = new google.maps.Marker({'+
' position: latlng, '+
' map: map,'+
' title: Msg+" ("+Lat+","+Lang+")"'+
' });'+
' markersArray.push(marker); '+
' }'+
''+
''+
' function TrafficOn() { trafficLayer.setMap(map); }'+
''+
' function TrafficOff() { trafficLayer.setMap(null); }'+
''+''+
' function BicyclingOn() { bikeLayer.setMap(map); }'+
''+
' function BicyclingOff(){ bikeLayer.setMap(null);}'+
''+
' function StreetViewOn() { map.set("streetViewControl", true); }'+
''+
' function StreetViewOff() { map.set("streetViewControl", false); }'+
''+
''+'</script> '+
'</head> '+
'<body onload="initialize()"> '+
' <div id="map_canvas" style="width:100%; height:100%"></div> '+
'</body> '+
'</html> ';
procedure TViewMaps.Timer1Timer(Sender: TObject);
var
aStream: TMemoryStream;
begin
Timer1.Enabled:= False; //Disable timer so it doesn't fire again
WebBrowser1.Navigate('about:blank');
if Assigned(WebBrowser1.Document) then
begin
aStream := TMemoryStream.Create;
try
aStream.WriteBuffer(Pointer(HTMLStr)^, Length(HTMLStr));
//aStream.Write(HTMLStr[1], Length(HTMLStr));
aStream.Seek(0, soFromBeginning);
MemoAddress.Lines.Clear;
MemoAddress.Lines.Add('1 Infinite Loop. Cupertino, CA 95014');
address := MemoAddress.Lines.Text;
address := StringReplace(StringReplace(Trim(address), #13, ' ', [rfReplaceAll]), #10, ' ', [rfReplaceAll]);
//HTMLWindow2.execScript(Format('codeAddress(%s)',[QuotedStr(address)]), 'JavaScript');
(WebBrowser1.Document as IPersistStreamInit).Load(TStreamAdapter.Create(aStream));
finally
aStream.Free;
end;
HTMLWindow2 := (WebBrowser1.Document as IHTMLDocument2).parentWindow;
end;
end;
procedure TViewMaps.GoToAddressBtnClick(Sender: TObject);
begin
address := MemoAddress.Lines.Text;
address := StringReplace(StringReplace(Trim(address), #13, ' ', [rfReplaceAll]), #10, ' ', [rfReplaceAll]);
HTMLWindow2.execScript(Format('codeAddress(%s)',[QuotedStr(address)]), 'JavaScript');
end;
如果要將地址傳遞給映射表單,則可以將其作為表單構造函數的參數傳遞。
創建一個名為fAddress的私有表單變量。
然后添加表單的公共構造函數重寫並傳遞地址:
Public
constructor create(AOwner: TComponent; AAddress: string); reintroduce;
implementation
constructor TForm1.create(AOwner: TComponent; AAddress: string);
begin
inherited create(AOwner);
fAddress := AAddress; // fAddress is now stored to form variable
end;
您的應用還需要確保首先加載Google API。 您無需使用TTimer,只需驗證TWebBrowser的READY_STATE即可確保Google API已完全加載。 在這里,我已將您的代碼移至表單的OnShow事件(已測試)。
procedure TForm1.FormShow(Sender: TObject);
var
aStream: TMemoryStream;
begin
WebBrowser1.Navigate('about:blank');
if Assigned(WebBrowser1.Document) then
begin
aStream := TMemoryStream.Create;
try
aStream.WriteBuffer(Pointer(HTMLStr)^, Length(HTMLStr));
aStream.Seek(0, soFromBeginning);
(WebBrowser1.Document as IPersistStreamInit).Load(TStreamAdapter.Create(aStream));
finally
aStream.Free;
end;
HTMLWindow2 := (WebBrowser1.Document as IHTMLDocument2).parentWindow;
end;
while WebBrowser1.ReadyState <> READYSTATE_COMPLETE do // wait for google
begin
sleep(0);
application.processmessages;
end;
fAddress := StringReplace(StringReplace(Trim(fAddress), #13, ' ', [rfReplaceAll]), #10, ' ', [rfReplaceAll]);
HTMLWindow2.execScript(Format('codeAddress(%s)',[QuotedStr(fAddress)]), 'JavaScript');
end;
要使用映射表實現和顯示地址,請使用調用表中的以下內容:
Form1 := TForm1.create(self, '15010 NE 36th St, Redmond, WA 98052');
try
Form1.showmodal;
finally
Form1.destroy;
end;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.