Xbase++ Requires Chilkat v11.1.0+
Xbase++
Google Maps Geolocation Request
See more REST Examples
Demonstrates how make a Google Maps Geolocation REST API request.Chilkat Xbase++ Downloads
LOCAL nSuccess
LOCAL oRest
LOCAL nBTls
LOCAL nPort
LOCAL nBAutoReconnect
LOCAL oJson
LOCAL oACellTowers
LOCAL oOCellTower
LOCAL oAWifi
LOCAL oOPoint
LOCAL cResponseJson
LOCAL oJsonResp
LOCAL oJsonLoc
LOCAL cLatitude
LOCAL cLongitude
LOCAL cAccuracy
nSuccess := 0
// 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.
oRest := CreateObject("Chilkat.Rest")
// Connect to the Google API REST server.
nBTls := 1
nPort := 443
nBAutoReconnect := 1
nSuccess := oRest:Connect("www.googleapis.com", nPort, nBTls, nBAutoReconnect)
// Add the Content-Type request header.
oRest:AddHeader("Content-Type", "application/json")
// Add your API key as a query parameter
oRest:AddQueryParam("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
// }
// ]
// }
oJson := CreateObject("Chilkat.JsonObject")
oJson:AppendInt("homeMobileCountryCode", 310)
oJson:AppendInt("homeMobileNetworkCode", 260)
oJson:AppendString("radioType", "gsm")
oJson:AppendString("carrier", "T-Mobile")
oACellTowers := CreateObject("Chilkat.JsonArray")
oJson:AppendArray2("cellTowers", oACellTowers)
oOCellTower := CreateObject("Chilkat.JsonObject")
oACellTowers:AddObjectAt2(0, oOCellTower)
oOCellTower:AppendInt("cellId", 39627456)
oOCellTower:AppendInt("locationAreaCode", 40495)
oOCellTower:AppendInt("mobileCountryCode", 310)
oOCellTower:AppendInt("mobileNetworkCode", 260)
oOCellTower:AppendInt("age", 0)
oOCellTower:AppendInt("signalStrength", -95)
oAWifi := CreateObject("Chilkat.JsonArray")
oJson:AppendArray2("wifiAccessPoints", oAWifi)
oOPoint := CreateObject("Chilkat.JsonObject")
oAWifi:AddObjectAt2(0, oOPoint)
oOPoint:AppendString("macAddress", "01:23:45:67:89:AB")
oOPoint:AppendInt("signalStrength", 8)
oOPoint:AppendInt("age", 0)
oOPoint:AppendInt("signalToNoiseRatio", -65)
oOPoint:AppendInt("channel", 8)
oAWifi:AddObjectAt2(1, oOPoint)
oOPoint:AppendString("macAddress", "01:23:45:67:89:AC")
oOPoint:AppendInt("signalStrength", 4)
oOPoint:AppendInt("age", 0)
cResponseJson := oRest:FullRequestString("POST", "/geolocation/v1/geolocate", oJson:Emit())
IF (oRest:LastMethodSuccess == 0)
? oRest:LastErrorText
oRest:destroy()
oJson:destroy()
oACellTowers:destroy()
oOCellTower:destroy()
oAWifi:destroy()
oOPoint:destroy()
RETURN
ENDIF
// When successful, the response code is 200.
IF (oRest:ResponseStatusCode != 200)
// Examine the request/response to see what happened.
? "response status code = " + Str(oRest:ResponseStatusCode)
? "response status text = " + oRest:ResponseStatusText
? "response header: " + oRest:ResponseHeader
? "response JSON: " + cResponseJson
? "---"
? "LastRequestStartLine: " + oRest:LastRequestStartLine
? "LastRequestHeader: " + oRest:LastRequestHeader
oRest:destroy()
oJson:destroy()
oACellTowers:destroy()
oOCellTower:destroy()
oAWifi:destroy()
oOPoint:destroy()
RETURN
ENDIF
oJson:EmitCompact := 0
? "JSON request body: " + oJson:Emit()
// The JSON response should look like this:
// {
// "location": {
// "lat": 37.4248297,
// "lng": -122.07346549999998
// },
// "accuracy": 1145.0
// }
? "JSON response: " + cResponseJson
oJsonResp := CreateObject("Chilkat.JsonObject")
oJsonResp:Load(cResponseJson)
oJsonLoc := CreateObject("Chilkat.JsonObject")
oJsonResp:ObjectOf2("location", oJsonLoc)
// Any JSON value can be obtained as a string..
cLatitude := oJsonLoc:StringOf("lat")
? "latitude = " + cLatitude
cLongitude := oJsonLoc:StringOf("lng")
? "longitude = " + cLongitude
cAccuracy := oJsonResp:StringOf("accuracy")
? "accuracy = " + cAccuracy
oRest:destroy()
oJson:destroy()
oACellTowers:destroy()
oOCellTower:destroy()
oAWifi:destroy()
oOPoint:destroy()
oJsonResp:destroy()
oJsonLoc:destroy()