Back to Devexpress

TdxMapControlAzureMapRouteProvider.OnResponse Event

vcl-dxazuremapinformationproviders-dot-tdxmapcontrolazuremaprouteprovider-37af1872.md

latest7.2 KB
Original Source

TdxMapControlAzureMapRouteProvider.OnResponse Event

Allows you to accept and process responses from an Azure Maps server.

Declaration

delphi
property OnResponse: TdxAzureMapRouteProviderOnResponse read; write;

Remarks

Handle the OnResponse event to receive and process responses from an Azure Maps server after an ExecuteAsync procedure call.

Event Occurrence

The OnResponse event occurs every time an Azure Maps server sends a response after an ExecuteAsync procedure call.

Event Parameters

The following parameters are available within an OnResponse event handler:

ASenderProvides access to the TdxMapControlAzureMapRouteProvider component that raised the server response event.AResponseProvides access to a container populated within information returned by a server.ADestroyResponse

Specifies if the OnResponse event handler automatically destroys the information container accessible through the AResponse parameter.

If this parameter value is True, the container is destroyed once the application exits the current OnResponse event handler. Otherwise, you need to release the container manually.

Refer to the TdxAzureMapRouteProviderOnResponse procedural type for detailed information on all available options.

Code Example: Build a Map Route Asynchronously

The code example in this section demonstrates a procedure that sends a query to an Azure Maps Route server and an OnResponse event handler that receives and processes the server response. The event handler uses an existing map item layer to draw a route between two specified points on a map if the query is successful.

delphi
uses
  dxAzureMapInformationProviders, // Declares TdxMapControlAzureMapRouteProvider
  dxMessageDialog, // Declares the dxMessageDlg global function
  dxCoreGraphics; // Declares TdxAlphaColors
// ...

procedure TMyForm.SendRouteQuery(ARouteStart, ARouteEnd: TdxMapControlGeoPoint);
var
  AQueryParams: IdxAzureMapRouteQueryParams;
  ARouteWaypoints: TdxMapControlGeoPoints;
begin
  AQueryParams := dxMapControl1AzureMapRouteProvider1.CreateQueryParams;
  SetLength(ARouteWaypoints, 2);
  ARouteWaypoints[0] := ARouteStart;
  ARouteWaypoints[1] := ARouteEnd;
  AQueryParams.WayPoints := ARouteWaypoints;
  AQueryParams.TravelMode := TdxAzureMapRouteTravelMode.Car;
  AQueryParams.MaxAlternatives := 1;
  dxMapControl1AzureMapRouteProvider1.ExecuteAsync(AQueryParams);
end;

procedure TMyForm.dxMapControl1AzureMapRouteProvider1Response(
  ASender: TdxMapControlAzureMapRouteProvider;
  AResponse: TdxAzureMapRouteRequestResponse; var ADestroyResponse: Boolean);
var
  APolyline: TdxMapPolyline;
  ARoute: TdxAzureMapRouteItem;
  I, J: Integer;
begin
  dxMapControl1.BeginUpdate; // Initiates the following batch change
  try
    if AResponse <> nil then
    begin
      if AResponse.IsSuccess then // Draws a route if a valid server response is received
      begin
        APolyline := dxMapControl1ItemLayer1.AddItem(TdxMapPolyline) as TdxMapPolyline;
        ARoute := AResponse.Routes.Items[0]; // Selects the first returned route
        for I := 0 to ARoute.Legs.Count - 1 do
          for J := 0 to ARoute.Legs.Items[I].Points.Count - 1 do
            APolyline.GeoPoints.Add.GeoPoint := ARoute.Legs.Items[I].Points.Items[J];

        APolyline.Style.BorderWidth := 4;
        APolyline.Style.BorderColor := TdxAlphaColors.DarkBlue;
      end
      else if Assigned(AResponse.ErrorInfo) then
        dxMessageDlg(AResponse.ErrorInfo.Message, TMsgDlgType.mtError, [mbOK], 0);
    end;
  finally
    dxMapControl1.EndUpdate; // Calls EndUpdate regardless of the batch operation's success
  end;
end;
cpp
#include "dxAzureMapInformationProviders.hpp" // Declares TdxMapControlAzureMapRouteProvider
#include "dxMessageDialog.hpp" // Declares the dxMessageDlg global function
#include "dxCoreGraphics.hpp" // Declares TdxAlphaColors
// ...

void __fastcall TMyForm::SendRouteQuery(TdxMapControlGeoPoint *ARouteStart, TdxMapControlGeoPoint *ARouteEnd)
{
  _di_IdxAzureMapRouteQueryParams AQueryParams;
  TdxMapControlGeoPoints *ARouteWaypoints;
  AQueryParams = dxMapControl1AzureMapRouteProvider1->CreateQueryParams();
  SetLength(ARouteWaypoints, 2);
  ARouteWaypoints[0] = ARouteStart;
  ARouteWaypoints[1] = ARouteEnd;
  AQueryParams->WayPoints = ARouteWaypoints;
  AQueryParams->TravelMode = TdxAzureMapRouteTravelMode::Car;
  AQueryParams->MaxAlternatives = 1;
  dxMapControl1AzureMapRouteProvider1->ExecuteAsync(AQueryParams);
}

void __fastcall TMyForm::dxMapControl1AzureMapRouteProvider1Response(
  TdxMapControlAzureMapRouteProvider *ASender,
  TdxAzureMapRouteRequestResponse *AResponse, bool &ADestroyResponse)
{
  TdxMapItem *AMapItem;
  TdxMapPolyline *APolyline;
  TdxAzureMapRouteItem *ARoute;
  dxMapControl1->BeginUpdate(); // Initiates the following batch change
  try
  {
    if(AResponse != nullptr)
    {
      if(AResponse->IsSuccess)
      {
        AMapItem = dxMapControl1ItemLayer1->AddItem(__classid(TdxMapPolyline));
        APolyline = dynamic_cast<TdxMapPolyline*>(AMapItem);
        ARoute = AResponse->Routes->Items[0]; // Selects the first returned route
        for(int i = 0; i < ARoute->Legs->Count; i++)
          for(int j = 0; j < ARoute->Legs->Items[i]->Points->Count; j++)
            APolyline->GeoPoints->Add()->GeoPoint = ARoute->Legs->Items[i]->Points->Items[j];

        APolyline->Style->BorderWidth = 4;
        APolyline->Style->BorderColor = TdxAlphaColors::DarkBlue;
      }
      else if(AResponse->ErrorInfo != nullptr)
      {
        dxMessageDlg(AResponse->ErrorInfo->Message, TMsgDlgType::mtError, TMsgDlgButtons() << mbOK, 0);
      }
    }
  }
  __finally
  {
    dxMapControl1->EndUpdate(); // Calls EndUpdate regardless of the batch operation's success
  }
}

See Also

TdxMapControlAzureMapRouteProvider.Execute Procedure

TdxMapControlAzureMapRouteProvider Class

TdxMapControlAzureMapRouteProvider Members

dxAzureMapInformationProviders Unit