![]() |
Chilkat HOME Android™ AutoIt C C# C++ Chilkat2-Python CkPython Classic ASP DataFlex Delphi DLL Go Java Node.js Objective-C PHP Extension Perl PowerBuilder PowerShell PureBasic Ruby SQL Server Swift Tcl Unicode C Unicode C++ VB.NET VBScript Visual Basic 6.0 Visual FoxPro Xojo Plugin
(AutoIt) Google Maps Geolocation RequestDemonstrates how make a Google Maps Geolocation REST API request.
; 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 = ObjCreate("Chilkat.Rest") ; Connect to the Google API REST server. Local $bTls = True Local $iPort = 443 Local $bAutoReconnect = True Local $bSuccess = $oRest.Connect("www.googleapis.com",$iPort,$bTls,$bAutoReconnect) ; 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 = ObjCreate("Chilkat.JsonObject") $oJson.AppendInt("homeMobileCountryCode",310) $oJson.AppendInt("homeMobileNetworkCode",260) $oJson.AppendString("radioType","gsm") $oJson.AppendString("carrier","T-Mobile") Local $oACellTowers = $oJson.AppendArray("cellTowers") $oACellTowers.AddObjectAt(0) Local $oOCellTower = $oACellTowers.ObjectAt(0) $oOCellTower.AppendInt("cellId",39627456) $oOCellTower.AppendInt("locationAreaCode",40495) $oOCellTower.AppendInt("mobileCountryCode",310) $oOCellTower.AppendInt("mobileNetworkCode",260) $oOCellTower.AppendInt("age",0) $oOCellTower.AppendInt("signalStrength",-95) Local $oAWifi = $oJson.AppendArray("wifiAccessPoints") $oAWifi.AddObjectAt(0) Local $oOPoint = $oAWifi.ObjectAt(0) $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.AddObjectAt(1) $oOPoint = $oAWifi.ObjectAt(1) $oOPoint.AppendString("macAddress","01:23:45:67:89:AC") $oOPoint.AppendInt("signalStrength",4) $oOPoint.AppendInt("age",0) Local $sResponseJson = $oRest.FullRequestString("POST","/geolocation/v1/geolocate",$oJson.Emit()) If ($oRest.LastMethodSuccess <> True) Then ConsoleWrite($oRest.LastErrorText & @CRLF) Exit EndIf ; When successful, the response code is 200. If ($oRest.ResponseStatusCode <> 200) Then ; Examine the request/response to see what happened. ConsoleWrite("response status code = " & $oRest.ResponseStatusCode & @CRLF) ConsoleWrite("response status text = " & $oRest.ResponseStatusText & @CRLF) ConsoleWrite("response header: " & $oRest.ResponseHeader & @CRLF) ConsoleWrite("response JSON: " & $sResponseJson & @CRLF) ConsoleWrite("---" & @CRLF) ConsoleWrite("LastRequestStartLine: " & $oRest.LastRequestStartLine & @CRLF) ConsoleWrite("LastRequestHeader: " & $oRest.LastRequestHeader & @CRLF) Exit EndIf $oJson.EmitCompact = False ConsoleWrite("JSON request body: " & $oJson.Emit() & @CRLF) ; The JSON response should look like this: ; { ; "location": { ; "lat": 37.4248297, ; "lng": -122.07346549999998 ; }, ; "accuracy": 1145.0 ; } ConsoleWrite("JSON response: " & $sResponseJson & @CRLF) $oJsonResp = ObjCreate("Chilkat.JsonObject") $oJsonResp.Load($sResponseJson) Local $oJsonLoc = $oJsonResp.ObjectOf("location") ; Any JSON value can be obtained as a string.. Local $sLatitude = $oJsonLoc.StringOf("lat") ConsoleWrite("latitude = " & $sLatitude & @CRLF) Local $sLongitude = $oJsonLoc.StringOf("lng") ConsoleWrite("longitude = " & $sLongitude & @CRLF) Local $sAccuracy = $oJsonResp.StringOf("accuracy") ConsoleWrite("accuracy = " & $sAccuracy & @CRLF) |
© 2000-2025 Chilkat Software, Inc. All Rights Reserved.