wasm_leaflet.lpr 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. library wasm_leaflet;
  2. {$mode objfpc}
  3. {$h+}
  4. {$codepage UTF8}
  5. uses
  6. uJSLeaflet,
  7. uCoordinate,
  8. NoThreads, SysUtils, StrUtils, JOB.Shared, JOB_Web, JOB.JS;
  9. type
  10. { Twasm_leaflet }
  11. Twasm_leaflet
  12. =
  13. class
  14. //Lifecycle Management
  15. public
  16. constructor Create;
  17. destructor Destroy; override;
  18. //Parameters
  19. private
  20. Latitude, Longitude: T_Coordinate;
  21. iParameters_Latitude : IJSHTMLInputElement;
  22. iParameters_Longitude: IJSHTMLInputElement;
  23. bCalculation: IJSHTMLButtonElement;
  24. procedure bCalculation_Show;
  25. procedure bCalculation_Hide;
  26. procedure iParameters_LatitudeInput (Event: IJSEvent);
  27. procedure iParameters_LongitudeInput(Event: IJSEvent);
  28. procedure bCalculationClick(Event: IJSEvent);
  29. procedure _from_Parameters;
  30. //Result
  31. private
  32. dCalculation_Result: IJSHTMLDivElement;
  33. procedure Display;
  34. //Leaflet Map
  35. private
  36. LeafLet_initialized: Boolean;
  37. Leaflet: IJSLeaflet;
  38. map: IJSLeafletMap;
  39. procedure mapClick(_e: IJSEvent);
  40. procedure RefreshMap;
  41. procedure Set_Map_to( _Latitude, _Longitude: Extended);
  42. procedure Ensure_LeafLet_initialized;
  43. //GeoLocation
  44. private
  45. procedure DoGeoLocation;
  46. procedure successCallback(_Position : IJSGeolocationPosition);
  47. procedure errorCallback(_Value : IJSGeolocationPositionError);
  48. procedure Process_the_location( _Latitude_Degrees, _Longitude_Degrees: Extended);
  49. //Execution
  50. public
  51. procedure Run;
  52. end;
  53. { Twasm_leaflet }
  54. constructor Twasm_leaflet.Create;
  55. begin
  56. inherited;
  57. LeafLet_initialized:= False;
  58. Latitude := T_Coordinate.Create( True);
  59. Longitude:= T_Coordinate.Create( False);
  60. Latitude.Longitude_from_LatitudeOverflow:= @Longitude.Longitude_Turnaround;
  61. end;
  62. destructor Twasm_leaflet.Destroy;
  63. begin
  64. inherited;
  65. end;
  66. procedure Twasm_leaflet.Display;
  67. var
  68. sResult: String;
  69. begin
  70. //WriteLn(ClassName+'.Display: Latitude: ',UTF8Encode(iParameters_Latitude.value));
  71. iParameters_Latitude.value:= UTF8Decode(Latitude.Str);
  72. //WriteLn(ClassName+'.Display: Longitude: ',UTF8Encode(iParameters_Longitude.value));
  73. iParameters_Longitude.value:= UTF8Decode(Longitude.Str);
  74. DefaultFormatSettings.ThousandSeparator:= ' ';
  75. DefaultFormatSettings.DecimalSeparator:= ',';
  76. sResult
  77. :=
  78. 'Latitude:' +Latitude .Str+'<br/>'
  79. +'Longitude:'+Longitude.Str+'<br/>'
  80. ;
  81. dCalculation_Result.innerHTML:= UTF8Decode( sResult);
  82. RefreshMap;
  83. end;
  84. procedure Twasm_leaflet._from_Parameters;
  85. begin
  86. //WriteLn(ClassName+'._from_Parameters: Latitude: ',UTF8Encode(iParameters_Latitude.value));
  87. Latitude.Set_Str( UTF8Encode(iParameters_Latitude.value));
  88. //WriteLn(ClassName+'._from_Parameters: Longitude: ',UTF8Encode(iParameters_Longitude.value));
  89. Longitude.Set_Str( UTF8Encode(iParameters_Longitude.value));
  90. Display;
  91. bCalculation_Hide;
  92. end;
  93. procedure Twasm_leaflet.bCalculationClick(Event: IJSEvent);
  94. begin
  95. _from_Parameters;
  96. end;
  97. procedure Twasm_leaflet.bCalculation_Show;
  98. begin
  99. //WriteLn(ClassName+'.bCalculation_Show');
  100. WriteLn(ClassName+'.bCalculation_Show: bCalculation.style.cssText:' ,bCalculation.style.cssText);
  101. bCalculation.style.cssText:= 'visibility: visible;';
  102. end;
  103. procedure Twasm_leaflet.bCalculation_Hide;
  104. begin
  105. bCalculation.style.cssText:= 'visibility: hidden;';
  106. end;
  107. procedure Twasm_leaflet.iParameters_LatitudeInput(Event: IJSEvent);
  108. begin
  109. bCalculation_Show;
  110. end;
  111. procedure Twasm_leaflet.iParameters_LongitudeInput(Event: IJSEvent);
  112. begin
  113. bCalculation_Show;
  114. end;
  115. procedure Twasm_leaflet.mapClick(_e: IJSEvent);
  116. var
  117. latlng: TJSObject;
  118. lat, lng: double;
  119. procedure dump_latlng_variables;
  120. var
  121. latlng_variables: TJSObject;
  122. begin
  123. latlng_variables:= JSObject.InvokeJSObjectResult( 'keys' ,[latlng],TJSObject);
  124. WriteLn( ClassName+'.Set_Map_To global_variables: type', latlng_variables.JSClassName, 'value: ', latlng_variables.toString);
  125. end;
  126. begin
  127. //WriteLn( Classname+'.mapClick: _e.type_:',_e.type_);
  128. latlng:= _e.ReadJSPropertyObject('latlng', TJSObject);
  129. //dump_latlng_variables;
  130. lat:= latlng.ReadJSPropertyDouble('lat');
  131. lng:= latlng.ReadJSPropertyDouble('lng');
  132. Process_the_location( lat, lng);
  133. end;
  134. procedure Twasm_leaflet.Ensure_LeafLet_initialized;
  135. procedure L_tileLayer;
  136. var
  137. params: TJSObject;
  138. begin
  139. //L
  140. // .tileLayer( 'https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png',
  141. // {
  142. // attribution: 'Map data &copy; <a href="https://www.openstreetmap.org/">OpenStreetMap</a> contributors',
  143. // maxZoom: 19,
  144. // }
  145. // )
  146. // .addTo(map);
  147. params:= TJSObject.JOBCreate([]);
  148. params.Properties['attribution']:= 'Map data &copy; <a href="https://www.openstreetmap.org/">OpenStreetMap</a> contributors';
  149. params.Properties['maxZoom' ]:= 19;
  150. Leaflet
  151. .tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', params)
  152. .addTo( map);
  153. end;
  154. begin
  155. if LeafLet_initialized then exit;
  156. Leaflet:= TJSLeaflet._from_id( 'L');
  157. map:= Leaflet.map('dMap'); //dMap is the container <div> id
  158. L_tileLayer;
  159. map.Set_onClick(@mapClick);
  160. LeafLet_initialized:= True;
  161. end;
  162. procedure Twasm_leaflet.Set_Map_to(_Latitude, _Longitude: Extended);
  163. procedure dump_global_variables;
  164. var
  165. global_variables: TJSObject;
  166. begin
  167. global_variables:= JSObject.InvokeJSObjectResult( 'keys',[JSWindow],TJSObject);
  168. WriteLn( ClassName+'.Set_Map_To global_variables: type',
  169. global_variables.JSClassName, 'valeur: ', global_variables.toString);
  170. end;
  171. begin
  172. Ensure_LeafLet_initialized;
  173. //dump_global_variables;
  174. //const map = L.map('map').setView([latitude, longitude], 13);
  175. map.setView( _Latitude, _Longitude, 13);
  176. end;
  177. procedure Twasm_leaflet.RefreshMap;
  178. begin
  179. Set_Map_to( Latitude.Degrees, Longitude.Degrees);
  180. end;
  181. procedure Twasm_leaflet.Process_the_location( _Latitude_Degrees, _Longitude_Degrees: Extended);
  182. begin
  183. Latitude .Degrees:= _Latitude_Degrees ;
  184. Longitude.Degrees:= _Longitude_Degrees;
  185. Display;
  186. end;
  187. procedure Twasm_leaflet.successCallback(_Position : IJSGeolocationPosition);
  188. //procedure dump_latlng_variables;
  189. //var
  190. // latlng_variables: TJSObject;
  191. //begin
  192. // latlng_variables:= JSObject.InvokeJSObjectResult( 'keys' ,[latlng],TJSObject);
  193. // WriteLn( ClassName+'.Set_Map_To global_variables: type', latlng_variables.JSClassName, 'valeur: ', latlng_variables.toString);
  194. //end;
  195. begin
  196. //The longitude given by the navigator is negative towards the West
  197. Process_the_location( _Position.coords.latitude, _Position.coords.longitude);
  198. end;
  199. procedure Twasm_leaflet.errorCallback(_Value : IJSGeolocationPositionError);
  200. begin
  201. WriteLn( ClassName+'.b_Click: geolocation.getCurrentPosition: ', _Value.message);
  202. Process_the_location( 43.604312,1.4436825);//Toulouse, France
  203. end;
  204. procedure Twasm_leaflet.DoGeoLocation;
  205. begin
  206. if true//window.navigator.hasOwnProperty('geoLocation')
  207. then
  208. JSWindow.navigator.geolocation.getCurrentPosition(@successCallback, @errorCallback)
  209. else
  210. WriteLn(ClassName+'.b_Click: GeoLocation unavailable');
  211. end;
  212. procedure Twasm_leaflet.Run;
  213. begin
  214. iParameters_Latitude :=TJSHTMLInputElement .Cast(JSDocument.getElementById('iParameters_Latitude' ));
  215. iParameters_Longitude:=TJSHTMLInputElement .Cast(JSDocument.getElementById('iParameters_Longitude'));
  216. bCalculation :=TJSHTMLButtonElement.Cast(JSDocument.getElementById('bCalculation' ));
  217. dCalculation_Result :=TJSHTMLDivElement .Cast(JSDocument.getElementById('dCalculation_Result' ));
  218. iParameters_Latitude .addEventListener('input', @iParameters_LatitudeInput );
  219. iParameters_Longitude.addEventListener('input', @iParameters_LongitudeInput);
  220. bCalculation .addEventListener('click' ,@bCalculationClick );
  221. DoGeoLocation;
  222. end;
  223. var
  224. Application: Twasm_leaflet;
  225. begin
  226. Application:=Twasm_leaflet.Create;
  227. Application.Run;
  228. end.