Sample code for 30+ languages & platforms
DataFlex

Google Maps Geolocation Request

See more REST Examples

Demonstrates how make a Google Maps Geolocation REST API request.

Chilkat DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoRest
    Boolean iBTls
    Integer iPort
    Boolean iBAutoReconnect
    Handle hoJson
    Variant vACellTowers
    Handle hoACellTowers
    Variant vOCellTower
    Handle hoOCellTower
    Variant vAWifi
    Handle hoAWifi
    Variant vOPoint
    Handle hoOPoint
    String sResponseJson
    Handle hoJsonResp
    Variant vJsonLoc
    Handle hoJsonLoc
    String sLatitude
    String sLongitude
    String sAccuracy
    String sTemp1
    Integer iTemp1
    Boolean bTemp1

    Move False To iSuccess

    // 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.

    Get Create (RefClass(cComChilkatRest)) To hoRest
    If (Not(IsComObjectCreated(hoRest))) Begin
        Send CreateComObject of hoRest
    End

    // Connect to the Google API REST server.
    Move True To iBTls
    Move 443 To iPort
    Move True To iBAutoReconnect
    Get ComConnect Of hoRest "www.googleapis.com" iPort iBTls iBAutoReconnect To iSuccess

    // Add the Content-Type request header.
    Get ComAddHeader Of hoRest "Content-Type" "application/json" To iSuccess

    // Add your API key as a query parameter
    Get ComAddQueryParam Of hoRest "key" "YOUR_API_KEY" To iSuccess

    // 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
    //   }
    //  ]
    // }

    Get Create (RefClass(cComChilkatJsonObject)) To hoJson
    If (Not(IsComObjectCreated(hoJson))) Begin
        Send CreateComObject of hoJson
    End
    Get ComAppendInt Of hoJson "homeMobileCountryCode" 310 To iSuccess
    Get ComAppendInt Of hoJson "homeMobileNetworkCode" 260 To iSuccess
    Get ComAppendString Of hoJson "radioType" "gsm" To iSuccess
    Get ComAppendString Of hoJson "carrier" "T-Mobile" To iSuccess

    Get Create (RefClass(cComChilkatJsonArray)) To hoACellTowers
    If (Not(IsComObjectCreated(hoACellTowers))) Begin
        Send CreateComObject of hoACellTowers
    End
    Get pvComObject of hoACellTowers to vACellTowers
    Get ComAppendArray2 Of hoJson "cellTowers" vACellTowers To iSuccess

    Get Create (RefClass(cComChilkatJsonObject)) To hoOCellTower
    If (Not(IsComObjectCreated(hoOCellTower))) Begin
        Send CreateComObject of hoOCellTower
    End
    Get pvComObject of hoOCellTower to vOCellTower
    Get ComAddObjectAt2 Of hoACellTowers 0 vOCellTower To iSuccess
    Get ComAppendInt Of hoOCellTower "cellId" 39627456 To iSuccess
    Get ComAppendInt Of hoOCellTower "locationAreaCode" 40495 To iSuccess
    Get ComAppendInt Of hoOCellTower "mobileCountryCode" 310 To iSuccess
    Get ComAppendInt Of hoOCellTower "mobileNetworkCode" 260 To iSuccess
    Get ComAppendInt Of hoOCellTower "age" 0 To iSuccess
    Get ComAppendInt Of hoOCellTower "signalStrength" -95 To iSuccess

    Get Create (RefClass(cComChilkatJsonArray)) To hoAWifi
    If (Not(IsComObjectCreated(hoAWifi))) Begin
        Send CreateComObject of hoAWifi
    End
    Get pvComObject of hoAWifi to vAWifi
    Get ComAppendArray2 Of hoJson "wifiAccessPoints" vAWifi To iSuccess

    Get Create (RefClass(cComChilkatJsonObject)) To hoOPoint
    If (Not(IsComObjectCreated(hoOPoint))) Begin
        Send CreateComObject of hoOPoint
    End
    Get pvComObject of hoOPoint to vOPoint
    Get ComAddObjectAt2 Of hoAWifi 0 vOPoint To iSuccess
    Get ComAppendString Of hoOPoint "macAddress" "01:23:45:67:89:AB" To iSuccess
    Get ComAppendInt Of hoOPoint "signalStrength" 8 To iSuccess
    Get ComAppendInt Of hoOPoint "age" 0 To iSuccess
    Get ComAppendInt Of hoOPoint "signalToNoiseRatio" -65 To iSuccess
    Get ComAppendInt Of hoOPoint "channel" 8 To iSuccess

    Get pvComObject of hoOPoint to vOPoint
    Get ComAddObjectAt2 Of hoAWifi 1 vOPoint To iSuccess
    Get ComAppendString Of hoOPoint "macAddress" "01:23:45:67:89:AC" To iSuccess
    Get ComAppendInt Of hoOPoint "signalStrength" 4 To iSuccess
    Get ComAppendInt Of hoOPoint "age" 0 To iSuccess

    Get ComEmit Of hoJson To sTemp1
    Get ComFullRequestString Of hoRest "POST" "/geolocation/v1/geolocate" sTemp1 To sResponseJson
    Get ComLastMethodSuccess Of hoRest To bTemp1
    If (bTemp1 = False) Begin
        Get ComLastErrorText Of hoRest To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    // When successful, the response code is 200.
    Get ComResponseStatusCode Of hoRest To iTemp1
    If (iTemp1 <> 200) Begin
        // Examine the request/response to see what happened.
        Get ComResponseStatusCode Of hoRest To iTemp1
        Showln "response status code = " iTemp1
        Get ComResponseStatusText Of hoRest To sTemp1
        Showln "response status text = " sTemp1
        Get ComResponseHeader Of hoRest To sTemp1
        Showln "response header: " sTemp1
        Showln "response JSON: " sResponseJson
        Showln "---"
        Get ComLastRequestStartLine Of hoRest To sTemp1
        Showln "LastRequestStartLine: " sTemp1
        Get ComLastRequestHeader Of hoRest To sTemp1
        Showln "LastRequestHeader: " sTemp1
        Procedure_Return
    End

    Set ComEmitCompact Of hoJson To False
    Get ComEmit Of hoJson To sTemp1
    Showln "JSON request body: " sTemp1

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

    Showln "JSON response: " sResponseJson

    Get Create (RefClass(cComChilkatJsonObject)) To hoJsonResp
    If (Not(IsComObjectCreated(hoJsonResp))) Begin
        Send CreateComObject of hoJsonResp
    End
    Get ComLoad Of hoJsonResp sResponseJson To iSuccess

    Get Create (RefClass(cComChilkatJsonObject)) To hoJsonLoc
    If (Not(IsComObjectCreated(hoJsonLoc))) Begin
        Send CreateComObject of hoJsonLoc
    End
    Get pvComObject of hoJsonLoc to vJsonLoc
    Get ComObjectOf2 Of hoJsonResp "location" vJsonLoc To iSuccess

    // Any JSON value can be obtained as a string..
    Get ComStringOf Of hoJsonLoc "lat" To sLatitude
    Showln "latitude = " sLatitude
    Get ComStringOf Of hoJsonLoc "lng" To sLongitude
    Showln "longitude = " sLongitude

    Get ComStringOf Of hoJsonResp "accuracy" To sAccuracy
    Showln "accuracy = " sAccuracy


End_Procedure