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
(PureBasic) Google Maps Geolocation RequestDemonstrates how make a Google Maps Geolocation REST API request.
IncludeFile "CkRest.pb" IncludeFile "CkJsonObject.pb" IncludeFile "CkJsonArray.pb" Procedure ChilkatExample() ; 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.i = 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 = CkJsonObject::ckAppendArray(json,"cellTowers") CkJsonArray::ckAddObjectAt(aCellTowers,0) oCellTower.i = CkJsonArray::ckObjectAt(aCellTowers,0) 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) CkJsonObject::ckDispose(oCellTower) CkJsonArray::ckDispose(aCellTowers) aWifi.i = CkJsonObject::ckAppendArray(json,"wifiAccessPoints") CkJsonArray::ckAddObjectAt(aWifi,0) oPoint.i = CkJsonArray::ckObjectAt(aWifi,0) 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) CkJsonObject::ckDispose(oPoint) CkJsonArray::ckAddObjectAt(aWifi,1) oPoint = CkJsonArray::ckObjectAt(aWifi,1) CkJsonObject::ckAppendString(oPoint,"macAddress","01:23:45:67:89:AC") CkJsonObject::ckAppendInt(oPoint,"signalStrength",4) CkJsonObject::ckAppendInt(oPoint,"age",0) CkJsonObject::ckDispose(oPoint) CkJsonArray::ckDispose(aWifi) responseJson.s = CkRest::ckFullRequestString(rest,"POST","/geolocation/v1/geolocate",CkJsonObject::ckEmit(json)) If CkRest::ckLastMethodSuccess(rest) <> 1 Debug CkRest::ckLastErrorText(rest) CkRest::ckDispose(rest) CkJsonObject::ckDispose(json) 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) 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::ckObjectOf(jsonResp,"location") ; 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 CkJsonObject::ckDispose(jsonLoc) accuracy.s = CkJsonObject::ckStringOf(jsonResp,"accuracy") Debug "accuracy = " + accuracy CkRest::ckDispose(rest) CkJsonObject::ckDispose(json) CkJsonObject::ckDispose(jsonResp) ProcedureReturn EndProcedure |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.