Questo sito utilizza cookies solo per scopi di autenticazione sul sito e nient'altro. Nessuna informazione personale viene tracciata. Leggi l'informativa sui cookies.
Username: Password: oppure
Delphi - Delphi e Google Maps
Forum - Delphi - Delphi e Google Maps

Avatar
systemgvp (Normal User)
Expert


Messaggi: 296
Iscritto: 14/04/2008

Segnala al moderatore
Postato alle 15:12
Martedė, 17/01/2012
Salve, sto creando una semplice applicazione (da esempi trovati) che mi permette di visualizzare una mappa di GoogleMaps e aggiungere su di essa dei punti, e fin qui tutto ok, ma ora vorrei vcaricare un file KML, ho scritto il seguente listato, ma lo JScript mi da sempre errore quando premo sul pulsante CaricaLayer e non capisco perchč,

potete darmi una mano?

Codice sorgente - presumibilmente Delphi

  1. unit MenuGE;
  2.  
  3. interface
  4.  
  5. uses
  6.   Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  7.   Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.OleCtrls, SHDocVw, MSHTML, ActiveX;
  8.  
  9. type
  10.   TMenuGEN = class(TForm)
  11.     LabelLatitude: TLabel;
  12.     LatitudineManuale: TEdit;
  13.     LongitudineManuale: TEdit;
  14.     LabelLongitude: TLabel;
  15.     AggiungiPunto: TButton;
  16.     PulisciPunti: TButton;
  17.     WebBrowser1: TWebBrowser;
  18.     Label1: TLabel;
  19.     Label2: TLabel;
  20.     LatitudineDaMappa: TEdit;
  21.     LongitudineDaMappa: TEdit;
  22.     CaricaLayer: TButton;
  23.     procedure AggiungiPuntoClick(Sender: TObject);
  24.     procedure FormCreate(Sender: TObject);
  25.     procedure PulisciPuntiClick(Sender: TObject);
  26.     procedure WebBrowser1CommandStateChange(ASender: TObject; Command: Integer;
  27.       Enable: WordBool);
  28.     procedure CaricaLayerClick(Sender: TObject);
  29.   private
  30.     { Private declarations }
  31.     HTMLWindow2: IHTMLWindow2;
  32.   public
  33.     { Public declarations }
  34.   end;
  35.  
  36. var
  37.   MenuGEN: TMenuGEN;
  38.  
  39. implementation
  40.  
  41. {$R *.dfm}
  42.  
  43. const
  44. HTMLStr: AnsiString =
  45. '<html> '+
  46. '<head> '+
  47. '<meta name="viewport" content="initial-scale=1.0, user-scalable=yes" /> '+
  48. //'<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false&language=it"></script> '+
  49. '<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=true"></script> '+
  50. '<script type="text/javascript"> '+
  51. ''+
  52. ''+
  53. '  var geocoder; '+
  54. '  var map;  '+
  55. '  var trafficLayer;'+
  56. '  var bikeLayer;'+
  57. '  var layer;'+
  58. '  var markersArray = [];'+
  59. ''+
  60. ''+
  61. '  function initialize() { '+
  62. '    geocoder = new google.maps.Geocoder();'+
  63. '    var latlng = new google.maps.LatLng(38.578561,15.937498); '+
  64. '    var myOptions = '+
  65. '    { '+
  66. '      zoom: 14, '+
  67. '      center: latlng, '+
  68. '      mapTypeId: google.maps.MapTypeId.ROADMAP '+
  69. '    }; '+
  70. '    map = new google.maps.Map(document.getElementById("map_canvas"), myOptions); '+
  71. //attributi
  72. '    trafficLayer = new google.maps.TrafficLayer();'+
  73. '    bikeLayer = new google.maps.BicyclingLayer();'+
  74. '    map.set("streetViewControl", true);'+
  75. '    trafficLayer.setMap(null);'+
  76. '    bikeLayer.setMap(null);'+
  77. //per aggiungere l'evento OnClick
  78. '    google.maps.event.addListener(map, "click", '+
  79. '         function(event) '+
  80. '                        {'+
  81. '                         document.getElementById("LatValue").value = event.latLng.lat(); '+
  82. '                         document.getElementById("LngValue").value = event.latLng.lng(); '+
  83. '                         PutMarkerPiuPunti(document.getElementById("LatValue").value, document.getElementById("LngValue").value,"") '+
  84. '                        } '+
  85. '   ); '+
  86. ''+
  87. '  } '+
  88. ''+
  89. //vai alle coordinate, centra e aggiungiungi un punto
  90. '  function GotoLatLngPuntoSingolo(Lat, Lang) { '+
  91. '   var latlng = new google.maps.LatLng(Lat,Lang);'+
  92. '   map.setCenter(latlng);'+
  93. '   PutMarkerPuntoSingolo(Lat, Lang, Lat+","+Lang);'+
  94. '  }'+
  95. ''+
  96. //vai alle coordinate e centra la mappa
  97. '  function GotoLatLngPiuPunti(Lat, Lang) { '+
  98. '   var latlng = new google.maps.LatLng(Lat,Lang);'+
  99. '   map.setCenter(latlng);'+
  100. '  }'+
  101. ''+
  102. //pulisci i punti
  103. 'function ClearMarkers() {  '+
  104. '  if (markersArray) {        '+
  105. '    for (i in markersArray) {  '+
  106. '      markersArray[i].setMap(null); '+
  107. '    } '+
  108. '  markersArray.length = 0;'+
  109. '  } '+
  110. '}  '+
  111. ''+
  112. //carica layer KML
  113. '  function CaricaLayer(indirizzo) { '+
  114. '   var layer = new google.maps.KmlLayer(indirizzo);'+
  115. '   layer.setMap(map);'+
  116. '  }'+
  117. ''+
  118. //per un solo punto
  119. '  function PutMarkerPuntoSingolo(Lat, Lang, Msg) { '+
  120. '   var latlng = new google.maps.LatLng(Lat,Lang);'+
  121. '   var marker = new google.maps.Marker({'+
  122. '      position: latlng, '+
  123. '      map: map,'+
  124. '      title: Msg+" ("+Lat+","+Lang+")"'+
  125. '  });'+
  126. ' markersArray.push(marker); '+
  127. '  }'+
  128. //per pių punti
  129. '  function PutMarkerPiuPunti(Lat, Lang, Msg) { '+
  130. '   var latlng = new google.maps.LatLng(Lat,Lang);'+
  131. '   var marker = new google.maps.Marker({'+
  132. '      position: latlng, '+
  133. '      map: map,'+
  134. '      title: Msg+" ("+Lat+","+Lang+")"'+
  135. '  });'+
  136. '  markersArray.push(marker); '+
  137. '  index= 1;'+//(markersArray.length % 10);'+
  138. '  if (index==0) { index=10 } '+
  139. '  icon = "http://www.google.com/mapfiles/kml/paddle/"+index+"-lv.png"; '+
  140. //'  icon = "http://maps.google.com/mapfiles/kml/shapes/shaded_dot.png"; '+
  141. '  marker.setIcon(icon); '+
  142. '  }'+
  143. ''+
  144. ''+'</script> '+
  145. '</head> '+
  146. '<body onload="initialize()"> '+
  147. '  <div id="map_canvas" style="width:100%; height:100%"></div> '+
  148. '  <div id="latlong"> '+
  149. '  <input type="hidden" id="LatValue" >'+
  150. '  <input type="hidden" id="LngValue" >'+
  151. '  </div>  '+
  152. '</body> '+
  153. '</html> ';
  154.  
  155. procedure TMenuGEN.PulisciPuntiClick(Sender: TObject);
  156. begin
  157.   HTMLWindow2.execScript('ClearMarkers()', 'JavaScript')
  158. end;
  159.  
  160. procedure TMenuGEN.AggiungiPuntoClick(Sender: TObject);
  161. begin
  162.    HTMLWindow2.execScript(Format('GotoLatLngPuntoSingolo(%s,%s)',[LatitudineManuale.Text,LongitudineManuale.Text]), 'JavaScript');
  163. end;
  164.  
  165. procedure TMenuGEN.CaricaLayerClick(Sender: TObject);
  166. var indirizzo : string;
  167. begin
  168.   indirizzo := 'C:\Users\systemgvp\Desktop\Google Maps da script\TestKML_1.kml';
  169.   HTMLWindow2.execScript(Format('CaricaLayer(%s)',[indirizzo]), 'JavaScript');
  170. end;
  171.  
  172. procedure TMenuGEN.FormCreate(Sender: TObject);
  173. var
  174.   aStream     : TMemoryStream;
  175. begin
  176.    WebBrowser1.Navigate('about:blank');
  177.     if Assigned(WebBrowser1.Document) then
  178.     begin
  179.       aStream := TMemoryStream.Create;
  180.       try
  181.          aStream.WriteBuffer(Pointer(HTMLStr)^, Length(HTMLStr));
  182.          //aStream.Write(HTMLStr[1], Length(HTMLStr));
  183.          aStream.Seek(0, soFromBeginning);
  184.          (WebBrowser1.Document as IPersistStreamInit).Load(TStreamAdapter.Create(aStream));
  185.       finally
  186.          aStream.Free;
  187.       end;
  188.       HTMLWindow2 := (WebBrowser1.Document as IHTMLDocument2).parentWindow;
  189.  
  190.     end;
  191. end;
  192.  
  193. procedure TMenuGEN.WebBrowser1CommandStateChange(ASender: TObject;
  194.   Command: Integer; Enable: WordBool);
  195. var
  196.   ADocument : IHTMLDocument2;
  197.   ABody     : IHTMLElement2;
  198.   Lat : string;
  199.   Lng : string;
  200.  
  201.       function GetIdValue(const Id : string):string;
  202.       var
  203.         Tag      : IHTMLElement;
  204.         TagsList : IHTMLElementCollection;
  205.         Index    : Integer;
  206.       begin
  207.         Result:='';
  208.         TagsList := ABody.getElementsByTagName('input');
  209.         for Index := 0 to TagsList.length-1 do
  210.         begin
  211.           Tag:=TagsList.item(Index, EmptyParam) As IHTMLElement;
  212.           if CompareText(Tag.id,Id)=0 then
  213.             Result := Tag.getAttribute('value', 0);
  214.         end;
  215.       end;
  216.  
  217. begin
  218.   if TOleEnum(Command) <> CSC_UPDATECOMMANDS then
  219.     Exit;
  220.  
  221.   ADocument := WebBrowser1.Document as IHTMLDocument2;
  222.   if not Assigned(ADocument) then
  223.     Exit;
  224.  
  225.   if not Supports(ADocument.body, IHTMLElement2, ABody) then
  226.     exit;
  227.  
  228.   Lat :=GetIdValue('LatValue');
  229.   Lng :=GetIdValue('LngValue');
  230.   if  (Lat<>'') and (Lng<>'') and ((Lat<>LatitudineDaMappa.Text) or (Lng<>LongitudineDaMappa.Text)) then
  231.   begin
  232.     LatitudineDaMappa.Text :=Lat;
  233.     LongitudineDaMappa.Text:=Lng;
  234.   end;
  235. end;
  236.  
  237. end.


PM Quote