Chilkat HOME .NET Core C# Android™ AutoIt C C# C++ Chilkat2-Python CkPython Classic ASP DataFlex Delphi ActiveX Delphi DLL Go Java Lianja Mono C# Node.js Objective-C PHP ActiveX PHP Extension Perl PowerBuilder PowerShell PureBasic Ruby SQL Server Swift 2 Swift 3,4,5... Tcl Unicode C Unicode C++ VB.NET VBScript Visual Basic 6.0 Visual FoxPro Xojo Plugin
(Delphi DLL) Google Maps Geolocation RequestDemonstrates how make a Google Maps Geolocation REST API request.
uses Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, JsonArray, Rest, JsonObject; ... procedure TForm1.Button1Click(Sender: TObject); var rest: HCkRest; bTls: Boolean; port: Integer; bAutoReconnect: Boolean; success: Boolean; json: HCkJsonObject; aCellTowers: HCkJsonArray; oCellTower: HCkJsonObject; aWifi: HCkJsonArray; oPoint: HCkJsonObject; responseJson: PWideChar; jsonResp: HCkJsonObject; jsonLoc: HCkJsonObject; latitude: PWideChar; longitude: PWideChar; accuracy: PWideChar; begin // This example duplicates the following CURL request: // curl -d @your_filename.json -H "Content-Type: application/json" -i "https://www.googleapis.com/geolocation/v1/geolocate?key=YOUR_API_KEY" // This example requires the Chilkat API to have been previously unlocked. // See Global Unlock Sample for sample code. rest := CkRest_Create(); // Connect to the Google API REST server. bTls := True; port := 443; bAutoReconnect := True; success := CkRest_Connect(rest,'www.googleapis.com',port,bTls,bAutoReconnect); // Add the Content-Type request header. CkRest_AddHeader(rest,'Content-Type','application/json'); // Add your API key as a query parameter CkRest_AddQueryParam(rest,'key','YOUR_API_KEY'); // The JSON query is contained in the body of the HTTP POST. // This is a sample query (which we'll dynamically build in this example) // { // "homeMobileCountryCode": 310, // "homeMobileNetworkCode": 260, // "radioType": "gsm", // "carrier": "T-Mobile", // "cellTowers": [ // { // "cellId": 39627456, // "locationAreaCode": 40495, // "mobileCountryCode": 310, // "mobileNetworkCode": 260, // "age": 0, // "signalStrength": -95 // } // ], // "wifiAccessPoints": [ // { // "macAddress": "01:23:45:67:89:AB", // "signalStrength": 8, // "age": 0, // "signalToNoiseRatio": -65, // "channel": 8 // }, // { // "macAddress": "01:23:45:67:89:AC", // "signalStrength": 4, // "age": 0 // } // ] // } json := CkJsonObject_Create(); CkJsonObject_AppendInt(json,'homeMobileCountryCode',310); CkJsonObject_AppendInt(json,'homeMobileNetworkCode',260); CkJsonObject_AppendString(json,'radioType','gsm'); CkJsonObject_AppendString(json,'carrier','T-Mobile'); aCellTowers := CkJsonObject_AppendArray(json,'cellTowers'); CkJsonArray_AddObjectAt(aCellTowers,0); oCellTower := CkJsonArray_ObjectAt(aCellTowers,0); CkJsonObject_AppendInt(oCellTower,'cellId',39627456); CkJsonObject_AppendInt(oCellTower,'locationAreaCode',40495); CkJsonObject_AppendInt(oCellTower,'mobileCountryCode',310); CkJsonObject_AppendInt(oCellTower,'mobileNetworkCode',260); CkJsonObject_AppendInt(oCellTower,'age',0); CkJsonObject_AppendInt(oCellTower,'signalStrength',-95); CkJsonObject_Dispose(oCellTower); CkJsonArray_Dispose(aCellTowers); aWifi := CkJsonObject_AppendArray(json,'wifiAccessPoints'); CkJsonArray_AddObjectAt(aWifi,0); oPoint := CkJsonArray_ObjectAt(aWifi,0); CkJsonObject_AppendString(oPoint,'macAddress','01:23:45:67:89:AB'); CkJsonObject_AppendInt(oPoint,'signalStrength',8); CkJsonObject_AppendInt(oPoint,'age',0); CkJsonObject_AppendInt(oPoint,'signalToNoiseRatio',-65); CkJsonObject_AppendInt(oPoint,'channel',8); CkJsonObject_Dispose(oPoint); CkJsonArray_AddObjectAt(aWifi,1); oPoint := CkJsonArray_ObjectAt(aWifi,1); CkJsonObject_AppendString(oPoint,'macAddress','01:23:45:67:89:AC'); CkJsonObject_AppendInt(oPoint,'signalStrength',4); CkJsonObject_AppendInt(oPoint,'age',0); CkJsonObject_Dispose(oPoint); CkJsonArray_Dispose(aWifi); responseJson := CkRest__fullRequestString(rest,'POST','/geolocation/v1/geolocate',CkJsonObject__emit(json)); if (CkRest_getLastMethodSuccess(rest) <> True) then begin Memo1.Lines.Add(CkRest__lastErrorText(rest)); Exit; end; // When successful, the response code is 200. if (CkRest_getResponseStatusCode(rest) <> 200) then begin // Examine the request/response to see what happened. Memo1.Lines.Add('response status code = ' + IntToStr(CkRest_getResponseStatusCode(rest))); Memo1.Lines.Add('response status text = ' + CkRest__responseStatusText(rest)); Memo1.Lines.Add('response header: ' + CkRest__responseHeader(rest)); Memo1.Lines.Add('response JSON: ' + responseJson); Memo1.Lines.Add('---'); Memo1.Lines.Add('LastRequestStartLine: ' + CkRest__lastRequestStartLine(rest)); Memo1.Lines.Add('LastRequestHeader: ' + CkRest__lastRequestHeader(rest)); Exit; end; CkJsonObject_putEmitCompact(json,False); Memo1.Lines.Add('JSON request body: ' + CkJsonObject__emit(json)); // The JSON response should look like this: // { // "location": { // "lat": 37.4248297, // "lng": -122.07346549999998 // }, // "accuracy": 1145.0 // } Memo1.Lines.Add('JSON response: ' + responseJson); jsonResp := CkJsonObject_Create(); CkJsonObject_Load(jsonResp,responseJson); jsonLoc := CkJsonObject_ObjectOf(jsonResp,'location'); // Any JSON value can be obtained as a string.. latitude := CkJsonObject__stringOf(jsonLoc,'lat'); Memo1.Lines.Add('latitude = ' + latitude); longitude := CkJsonObject__stringOf(jsonLoc,'lng'); Memo1.Lines.Add('longitude = ' + longitude); CkJsonObject_Dispose(jsonLoc); accuracy := CkJsonObject__stringOf(jsonResp,'accuracy'); Memo1.Lines.Add('accuracy = ' + accuracy); CkRest_Dispose(rest); CkJsonObject_Dispose(json); CkJsonObject_Dispose(jsonResp); end; |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.