Einzelnen Beitrag anzeigen

MiniMax

Registriert seit: 15. Dez 2009
Ort: Bremen
193 Beiträge
 
Delphi 2009 Architect
 
#16

AW: Google Maps API

  Alt 26. Aug 2010, 21:05
das war ein guter tipp, aber ich hänge schon wieder:

Es kommt einfach kein wert raus (es soll die gecklickte koordinaten ausgegeben werden):

Delphi-Quellcode:
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, OleCtrls, SHDocVw, StdCtrls, ExtCtrls, XPMan, ComCtrls,MSHTML;

type
  TForm1 = class(TForm)
    XPManifest1: TXPManifest;
    CheckBoxTraffic: TCheckBox;
    WebBrowser1: TWebBrowser;
    Button1: TButton;
    procedure FormCreate(Sender: TObject);
    procedure CheckBoxTrafficClick(Sender: TObject);
    procedure WebBrowser1UpdatePageStatus(ASender: TObject;
      const pDisp: IDispatch; var nPage, fDone: OleVariant);
    procedure Button1Click(Sender: TObject);
  private
    { Private-Deklarationen }
     HTMLWindow2: IHTMLWindow2;

  public
    { Public-Deklarationen }
  end;

var
  Form1: TForm1;

implementation
uses
ActiveX;


{$R *.dfm}


const
HTMLStr : ansiString =
'<html> '+
'<head> '+
'<input type="hidden" id="result" value="" />'+
'<meta name="viewport" content="initial-scale=1.0, user-scalable=yes" /> '+
'<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=true"></script> '+
'<script type="text/javascript"> '+
''+
''+
' var geocoder; '+
' var map; '+
' var trafficLayer;'+
' var bikeLayer;'+
''+
''+




' function initialize() { '+
' geocoder = new google.maps.Geocoder();'+
' var latlng = new google.maps.LatLng(40.714776,-74.019213); '+
' var myOptions = { '+
' zoom: 13, '+
' center: latlng, '+
'mapTypeControl: true,'+
' mapTypeControlOptions: { '+
' style: google.maps.MapTypeControlStyle.DROPDOWN_MENU '+
' },'+
' navigationControl: true,'+
' navigationControlOptions: { '+
' style: google.maps.NavigationControlStyle.ZOOM_PAN'+
' },'+

' 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();'+
' google.maps.event.addListener(map, "click", function(event) {'+
' placeMarker(event.latLng);'+
' });'+

' } '+
''+
''+

'function placeMarker(location) { '+
' var clickedLocation = new google.maps.LatLng(location);'+
' var marker = new google.maps.Marker({ '+
' position: location,'+
' map: map'+
' });'+
''+
' map.setCenter(location);'+
'document.getElementById("result").value = location;'+
'}'+

''+
''+
' function codeAddress(address) { '+
' if (geocoder) {'+
' geocoder.geocode( { address: address}, function(results, status) { '+
' if (status == google.maps.GeocoderStatus.OK) {'+
' map.setCenter(results[0].geometry.location);'+
' var marker = new google.maps.Marker({'+
' map: map,'+
' position: results[0].geometry.location'+
' });'+
' } 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);'+
' var marker = new google.maps.Marker({'+
' position: latlng, '+
' map: map,'+
' title:Lat+","+Lang'+
' });'+
' }'+
''+
''+
' 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> ';


function GetElementIdValue(WebBrowser: TWebBrowser;
  TagName, TagId, TagAttrib: string):string;
var
  Document: IHTMLDocument2;
  Body: IHTMLElement2;
  Tags: IHTMLElementCollection;
  Tag: IHTMLElement;
  I: Integer;
begin
  Result:='';
  if not Supports(WebBrowser.Document, IHTMLDocument2, Document) then
    raise Exception.Create('Invalid HTML document');
  if not Supports(Document.body, IHTMLElement2, Body) then
    raise Exception.Create('Can''t find <body> element');
  Tags := Body.getElementsByTagName(UpperCase(TagName));
  for I := 0 to Pred(Tags.length) do begin
    Tag:=Tags.item(I, EmptyParam) as IHTMLElement;
    if Tag.id=TagId then Result := Tag.getAttribute(TagAttrib, 0);
  end;
end;



procedure TForm1.Button1Click(Sender: TObject);
begin
Showmessage(GetElementIdValue(WebBrowser1, 'input', 'result', 'value'));
end;

procedure TForm1.CheckBoxTrafficClick(Sender: TObject);
begin
    if CheckBoxTraffic.Checked then
     HTMLWindow2.execScript('TrafficOn()', 'JavaScript')
    else
     HTMLWindow2.execScript('TrafficOff()', 'JavaScript');
 end;

procedure Tform1.FormCreate(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.Write(HTMLStr[1], Length(HTMLStr));
         aStream.Seek(0, soFromBeginning);
         (WebBrowser1.Document as IPersistStreamInit).Load(TStreamAdapter.Create(aStream));
      finally
         aStream.Free;
      end;
      HTMLWindow2 := (WebBrowser1.Document as IHTMLDocument2).parentWindow;
      //HTMLWindow2.execScript('mode()', 'JavaScript')
    end;


end;


procedure TForm1.WebBrowser1UpdatePageStatus(ASender: TObject;
  const pDisp: IDispatch; var nPage, fDone: OleVariant);
begin
Showmessage(GetElementIdValue(WebBrowser1, 'input', 'result', 'value'));
end;

end.
Gruß
MiniMax
  Mit Zitat antworten Zitat