Delphi Component for OpenWeatherMap API
unit OpenWeatherMap;
interface
uses
System.SysUtils, System.Classes, System.Net.HttpClient, System.Net.URLClient;
type
TOpenWeatherMap = class(TComponent)
private
FHttpClient: THttpClient;
FApiKey: string;
FLatitude: Double;
FLongitude: Double;
FLanguage: string;
FUnits: string;
FResponse: string;
function GetUrl: string;
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
procedure GetWeather;
property Response: string read FResponse;
published
property ApiKey: string read FApiKey write FApiKey; // Comment: The API key used for authentication with the OpenWeatherMap API.
property Latitude: Double read FLatitude write FLatitude; // Comment: The latitude coordinate for the weather location.
property Longitude: Double read FLongitude write FLongitude; // Comment: The longitude coordinate for the weather location.
property Language: string read FLanguage write FLanguage; // Comment: The language code used for the weather data.
property Units: string read FUnits write FUnits; // Comment: The unit system used for the weather data.
end;
implementation
constructor TOpenWeatherMap.Create(AOwner: TComponent);
begin
inherited;
FHttpClient := THttpClient.Create; // Comment: Create an instance of THttpClient for making HTTP requests.
end;
destructor TOpenWeatherMap.Destroy;
begin
FHttpClient.Free; // Comment: Free the THttpClient instance to release resources.
inherited;
end;
function TOpenWeatherMap.GetUrl: string;
begin
Result := Format('https://api.openweathermap.org/data/2.5/weather?lat=%f&lon=%f&appid=%s&lang=%s&units=%s',
[FLatitude, FLongitude, FApiKey, FLanguage, FUnits]); // Comment: Construct the URL for the OpenWeatherMap API request.
end;
procedure TOpenWeatherMap.GetWeather;
begin
FResponse := FHttpClient.Get(GetUrl).ContentAsString; // Comment: Make an HTTP GET request to the OpenWeatherMap API and store the response.
end;
end.
The code provided is a Delphi unit named OpenWeatherMap
that defines a component TOpenWeatherMap
for interacting with the OpenWeatherMap API to retrieve weather data.
The TOpenWeatherMap
component has the following properties:
ApiKey
: The API key used for authentication with the OpenWeatherMap API.Latitude
: The latitude coordinate for the weather location.Longitude
: The longitude coordinate for the weather location.Language
: The language code used for the weather data.Units
: The unit system used for the weather data.
The component also has a Response
property that stores the response received from the OpenWeatherMap API after calling the GetWeather
method.
The TOpenWeatherMap
component has the following methods:
Create
: The constructor of the component. It creates an instance ofTHttpClient
to make HTTP requests.Destroy
: The destructor of the component. It frees theTHttpClient
instance to release resources.GetUrl
: A private function that constructs the URL for the OpenWeatherMap API request based on the component's properties.GetWeather
: A public procedure that makes an HTTP GET request to the OpenWeatherMap API using the constructed URL and stores the response in theResponse
property.
To use this component, you need to set the appropriate values for the ApiKey
, Latitude
, Longitude
, Language
, and Units
properties before calling the GetWeather
method. After calling the GetWeather
method, you can access the weather data response through the Response
property.