Sample code for 30+ languages & platforms
PureBasic

Google Maps Geolocation Request

See more REST Examples

Demonstrates how make a Google Maps Geolocation REST API request.

Chilkat PureBasic Downloads

PureBasic
IncludeFile "CkRest.pb"
IncludeFile "CkJsonObject.pb"
IncludeFile "CkJsonArray.pb"

Procedure ChilkatExample()

    success.i = 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.

    rest.i = CkRest::ckCreate()
    If rest.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    ; Connect to the Google API REST server.
    bTls.i = 1
    port.i = 443
    bAutoReconnect.i = 1
    success = CkRest::ckConnect(rest,"www.googleapis.com",port,bTls,bAutoReconnect)

    ; Add the Content-Type request header.
    CkRest::ckAddHeader(rest,"Content-Type","application/json")

    ; Add your API key as a query parameter
    CkRest::ckAddQueryParam(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.i = CkJsonObject::ckCreate()
    If json.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkJsonObject::ckAppendInt(json,"homeMobileCountryCode",310)
    CkJsonObject::ckAppendInt(json,"homeMobileNetworkCode",260)
    CkJsonObject::ckAppendString(json,"radioType","gsm")
    CkJsonObject::ckAppendString(json,"carrier","T-Mobile")

    aCellTowers.i = CkJsonArray::ckCreate()
    If aCellTowers.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkJsonObject::ckAppendArray2(json,"cellTowers",aCellTowers)

    oCellTower.i = CkJsonObject::ckCreate()
    If oCellTower.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkJsonArray::ckAddObjectAt2(aCellTowers,0,oCellTower)
    CkJsonObject::ckAppendInt(oCellTower,"cellId",39627456)
    CkJsonObject::ckAppendInt(oCellTower,"locationAreaCode",40495)
    CkJsonObject::ckAppendInt(oCellTower,"mobileCountryCode",310)
    CkJsonObject::ckAppendInt(oCellTower,"mobileNetworkCode",260)
    CkJsonObject::ckAppendInt(oCellTower,"age",0)
    CkJsonObject::ckAppendInt(oCellTower,"signalStrength",-95)

    aWifi.i = CkJsonArray::ckCreate()
    If aWifi.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkJsonObject::ckAppendArray2(json,"wifiAccessPoints",aWifi)

    oPoint.i = CkJsonObject::ckCreate()
    If oPoint.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkJsonArray::ckAddObjectAt2(aWifi,0,oPoint)
    CkJsonObject::ckAppendString(oPoint,"macAddress","01:23:45:67:89:AB")
    CkJsonObject::ckAppendInt(oPoint,"signalStrength",8)
    CkJsonObject::ckAppendInt(oPoint,"age",0)
    CkJsonObject::ckAppendInt(oPoint,"signalToNoiseRatio",-65)
    CkJsonObject::ckAppendInt(oPoint,"channel",8)

    CkJsonArray::ckAddObjectAt2(aWifi,1,oPoint)
    CkJsonObject::ckAppendString(oPoint,"macAddress","01:23:45:67:89:AC")
    CkJsonObject::ckAppendInt(oPoint,"signalStrength",4)
    CkJsonObject::ckAppendInt(oPoint,"age",0)

    responseJson.s = CkRest::ckFullRequestString(rest,"POST","/geolocation/v1/geolocate",CkJsonObject::ckEmit(json))
    If CkRest::ckLastMethodSuccess(rest) = 0
        Debug CkRest::ckLastErrorText(rest)
        CkRest::ckDispose(rest)
        CkJsonObject::ckDispose(json)
        CkJsonArray::ckDispose(aCellTowers)
        CkJsonObject::ckDispose(oCellTower)
        CkJsonArray::ckDispose(aWifi)
        CkJsonObject::ckDispose(oPoint)
        ProcedureReturn
    EndIf

    ; When successful, the response code is 200.
    If CkRest::ckResponseStatusCode(rest) <> 200
        ; Examine the request/response to see what happened.
        Debug "response status code = " + Str(CkRest::ckResponseStatusCode(rest))
        Debug "response status text = " + CkRest::ckResponseStatusText(rest)
        Debug "response header: " + CkRest::ckResponseHeader(rest)
        Debug "response JSON: " + responseJson
        Debug "---"
        Debug "LastRequestStartLine: " + CkRest::ckLastRequestStartLine(rest)
        Debug "LastRequestHeader: " + CkRest::ckLastRequestHeader(rest)
        CkRest::ckDispose(rest)
        CkJsonObject::ckDispose(json)
        CkJsonArray::ckDispose(aCellTowers)
        CkJsonObject::ckDispose(oCellTower)
        CkJsonArray::ckDispose(aWifi)
        CkJsonObject::ckDispose(oPoint)
        ProcedureReturn
    EndIf

    CkJsonObject::setCkEmitCompact(json, 0)
    Debug "JSON request body: " + CkJsonObject::ckEmit(json)

    ; The JSON response should look like this:
    ; { 
    ;  "location": {
    ;   "lat": 37.4248297,
    ;   "lng": -122.07346549999998
    ;  },
    ;  "accuracy": 1145.0
    ; }

    Debug "JSON response: " + responseJson

    jsonResp.i = CkJsonObject::ckCreate()
    If jsonResp.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkJsonObject::ckLoad(jsonResp,responseJson)

    jsonLoc.i = CkJsonObject::ckCreate()
    If jsonLoc.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkJsonObject::ckObjectOf2(jsonResp,"location",jsonLoc)

    ; Any JSON value can be obtained as a string..
    latitude.s = CkJsonObject::ckStringOf(jsonLoc,"lat")
    Debug "latitude = " + latitude
    longitude.s = CkJsonObject::ckStringOf(jsonLoc,"lng")
    Debug "longitude = " + longitude

    accuracy.s = CkJsonObject::ckStringOf(jsonResp,"accuracy")
    Debug "accuracy = " + accuracy


    CkRest::ckDispose(rest)
    CkJsonObject::ckDispose(json)
    CkJsonArray::ckDispose(aCellTowers)
    CkJsonObject::ckDispose(oCellTower)
    CkJsonArray::ckDispose(aWifi)
    CkJsonObject::ckDispose(oPoint)
    CkJsonObject::ckDispose(jsonResp)
    CkJsonObject::ckDispose(jsonLoc)


    ProcedureReturn
EndProcedure