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
(Unicode C) Google Maps Geolocation RequestDemonstrates how make a Google Maps Geolocation REST API request.
#include <C_CkRestW.h> #include <C_CkJsonObjectW.h> #include <C_CkJsonArrayW.h> void ChilkatSample(void) { HCkRestW rest; BOOL bTls; int port; BOOL bAutoReconnect; BOOL success; HCkJsonObjectW json; HCkJsonArrayW aCellTowers; HCkJsonObjectW oCellTower; HCkJsonArrayW aWifi; HCkJsonObjectW oPoint; const wchar_t *responseJson; HCkJsonObjectW jsonResp; HCkJsonObjectW jsonLoc; const wchar_t *latitude; const wchar_t *longitude; const wchar_t *accuracy; // 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 = CkRestW_Create(); // Connect to the Google API REST server. bTls = TRUE; port = 443; bAutoReconnect = TRUE; success = CkRestW_Connect(rest,L"www.googleapis.com",port,bTls,bAutoReconnect); // Add the Content-Type request header. CkRestW_AddHeader(rest,L"Content-Type",L"application/json"); // Add your API key as a query parameter CkRestW_AddQueryParam(rest,L"key",L"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 = CkJsonObjectW_Create(); CkJsonObjectW_AppendInt(json,L"homeMobileCountryCode",310); CkJsonObjectW_AppendInt(json,L"homeMobileNetworkCode",260); CkJsonObjectW_AppendString(json,L"radioType",L"gsm"); CkJsonObjectW_AppendString(json,L"carrier",L"T-Mobile"); aCellTowers = CkJsonObjectW_AppendArray(json,L"cellTowers"); CkJsonArrayW_AddObjectAt(aCellTowers,0); oCellTower = CkJsonArrayW_ObjectAt(aCellTowers,0); CkJsonObjectW_AppendInt(oCellTower,L"cellId",39627456); CkJsonObjectW_AppendInt(oCellTower,L"locationAreaCode",40495); CkJsonObjectW_AppendInt(oCellTower,L"mobileCountryCode",310); CkJsonObjectW_AppendInt(oCellTower,L"mobileNetworkCode",260); CkJsonObjectW_AppendInt(oCellTower,L"age",0); CkJsonObjectW_AppendInt(oCellTower,L"signalStrength",-95); CkJsonObjectW_Dispose(oCellTower); CkJsonArrayW_Dispose(aCellTowers); aWifi = CkJsonObjectW_AppendArray(json,L"wifiAccessPoints"); CkJsonArrayW_AddObjectAt(aWifi,0); oPoint = CkJsonArrayW_ObjectAt(aWifi,0); CkJsonObjectW_AppendString(oPoint,L"macAddress",L"01:23:45:67:89:AB"); CkJsonObjectW_AppendInt(oPoint,L"signalStrength",8); CkJsonObjectW_AppendInt(oPoint,L"age",0); CkJsonObjectW_AppendInt(oPoint,L"signalToNoiseRatio",-65); CkJsonObjectW_AppendInt(oPoint,L"channel",8); CkJsonObjectW_Dispose(oPoint); CkJsonArrayW_AddObjectAt(aWifi,1); oPoint = CkJsonArrayW_ObjectAt(aWifi,1); CkJsonObjectW_AppendString(oPoint,L"macAddress",L"01:23:45:67:89:AC"); CkJsonObjectW_AppendInt(oPoint,L"signalStrength",4); CkJsonObjectW_AppendInt(oPoint,L"age",0); CkJsonObjectW_Dispose(oPoint); CkJsonArrayW_Dispose(aWifi); responseJson = CkRestW_fullRequestString(rest,L"POST",L"/geolocation/v1/geolocate",CkJsonObjectW_emit(json)); if (CkRestW_getLastMethodSuccess(rest) != TRUE) { wprintf(L"%s\n",CkRestW_lastErrorText(rest)); CkRestW_Dispose(rest); CkJsonObjectW_Dispose(json); return; } // When successful, the response code is 200. if (CkRestW_getResponseStatusCode(rest) != 200) { // Examine the request/response to see what happened. wprintf(L"response status code = %d\n",CkRestW_getResponseStatusCode(rest)); wprintf(L"response status text = %s\n",CkRestW_responseStatusText(rest)); wprintf(L"response header: %s\n",CkRestW_responseHeader(rest)); wprintf(L"response JSON: %s\n",responseJson); wprintf(L"---\n"); wprintf(L"LastRequestStartLine: %s\n",CkRestW_lastRequestStartLine(rest)); wprintf(L"LastRequestHeader: %s\n",CkRestW_lastRequestHeader(rest)); CkRestW_Dispose(rest); CkJsonObjectW_Dispose(json); return; } CkJsonObjectW_putEmitCompact(json,FALSE); wprintf(L"JSON request body: %s\n",CkJsonObjectW_emit(json)); // The JSON response should look like this: // { // "location": { // "lat": 37.4248297, // "lng": -122.07346549999998 // }, // "accuracy": 1145.0 // } wprintf(L"JSON response: %s\n",responseJson); jsonResp = CkJsonObjectW_Create(); CkJsonObjectW_Load(jsonResp,responseJson); jsonLoc = CkJsonObjectW_ObjectOf(jsonResp,L"location"); // Any JSON value can be obtained as a string.. latitude = CkJsonObjectW_stringOf(jsonLoc,L"lat"); wprintf(L"latitude = %s\n",latitude); longitude = CkJsonObjectW_stringOf(jsonLoc,L"lng"); wprintf(L"longitude = %s\n",longitude); CkJsonObjectW_Dispose(jsonLoc); accuracy = CkJsonObjectW_stringOf(jsonResp,L"accuracy"); wprintf(L"accuracy = %s\n",accuracy); CkRestW_Dispose(rest); CkJsonObjectW_Dispose(json); CkJsonObjectW_Dispose(jsonResp); } |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.