C
C
Google Maps Geolocation Request
See more REST Examples
Demonstrates how make a Google Maps Geolocation REST API request.Chilkat C Downloads
#include <C_CkRest.h>
#include <C_CkJsonObject.h>
#include <C_CkJsonArray.h>
void ChilkatSample(void)
{
BOOL success;
HCkRest rest;
BOOL bTls;
int port;
BOOL bAutoReconnect;
HCkJsonObject json;
HCkJsonArray aCellTowers;
HCkJsonObject oCellTower;
HCkJsonArray aWifi;
HCkJsonObject oPoint;
const char *responseJson;
HCkJsonObject jsonResp;
HCkJsonObject jsonLoc;
const char *latitude;
const char *longitude;
const char *accuracy;
success = FALSE;
// 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 = CkJsonArray_Create();
CkJsonObject_AppendArray2(json,"cellTowers",aCellTowers);
oCellTower = CkJsonObject_Create();
CkJsonArray_AddObjectAt2(aCellTowers,0,oCellTower);
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);
aWifi = CkJsonArray_Create();
CkJsonObject_AppendArray2(json,"wifiAccessPoints",aWifi);
oPoint = CkJsonObject_Create();
CkJsonArray_AddObjectAt2(aWifi,0,oPoint);
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);
CkJsonArray_AddObjectAt2(aWifi,1,oPoint);
CkJsonObject_AppendString(oPoint,"macAddress","01:23:45:67:89:AC");
CkJsonObject_AppendInt(oPoint,"signalStrength",4);
CkJsonObject_AppendInt(oPoint,"age",0);
responseJson = CkRest_fullRequestString(rest,"POST","/geolocation/v1/geolocate",CkJsonObject_emit(json));
if (CkRest_getLastMethodSuccess(rest) == FALSE) {
printf("%s\n",CkRest_lastErrorText(rest));
CkRest_Dispose(rest);
CkJsonObject_Dispose(json);
CkJsonArray_Dispose(aCellTowers);
CkJsonObject_Dispose(oCellTower);
CkJsonArray_Dispose(aWifi);
CkJsonObject_Dispose(oPoint);
return;
}
// When successful, the response code is 200.
if (CkRest_getResponseStatusCode(rest) != 200) {
// Examine the request/response to see what happened.
printf("response status code = %d\n",CkRest_getResponseStatusCode(rest));
printf("response status text = %s\n",CkRest_responseStatusText(rest));
printf("response header: %s\n",CkRest_responseHeader(rest));
printf("response JSON: %s\n",responseJson);
printf("---\n");
printf("LastRequestStartLine: %s\n",CkRest_lastRequestStartLine(rest));
printf("LastRequestHeader: %s\n",CkRest_lastRequestHeader(rest));
CkRest_Dispose(rest);
CkJsonObject_Dispose(json);
CkJsonArray_Dispose(aCellTowers);
CkJsonObject_Dispose(oCellTower);
CkJsonArray_Dispose(aWifi);
CkJsonObject_Dispose(oPoint);
return;
}
CkJsonObject_putEmitCompact(json,FALSE);
printf("JSON request body: %s\n",CkJsonObject_emit(json));
// The JSON response should look like this:
// {
// "location": {
// "lat": 37.4248297,
// "lng": -122.07346549999998
// },
// "accuracy": 1145.0
// }
printf("JSON response: %s\n",responseJson);
jsonResp = CkJsonObject_Create();
CkJsonObject_Load(jsonResp,responseJson);
jsonLoc = CkJsonObject_Create();
CkJsonObject_ObjectOf2(jsonResp,"location",jsonLoc);
// Any JSON value can be obtained as a string..
latitude = CkJsonObject_stringOf(jsonLoc,"lat");
printf("latitude = %s\n",latitude);
longitude = CkJsonObject_stringOf(jsonLoc,"lng");
printf("longitude = %s\n",longitude);
accuracy = CkJsonObject_stringOf(jsonResp,"accuracy");
printf("accuracy = %s\n",accuracy);
CkRest_Dispose(rest);
CkJsonObject_Dispose(json);
CkJsonArray_Dispose(aCellTowers);
CkJsonObject_Dispose(oCellTower);
CkJsonArray_Dispose(aWifi);
CkJsonObject_Dispose(oPoint);
CkJsonObject_Dispose(jsonResp);
CkJsonObject_Dispose(jsonLoc);
}