AutoIt
AutoIt
Google Maps Geolocation Request
See more REST Examples
Demonstrates how make a Google Maps Geolocation REST API request.Chilkat AutoIt Downloads
Local $bSuccess = False
; 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
$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")
$oACellTowers = ObjCreate("Chilkat.JsonArray")
$oJson.AppendArray2("cellTowers",$oACellTowers)
$oOCellTower = ObjCreate("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 = ObjCreate("Chilkat.JsonArray")
$oJson.AppendArray2("wifiAccessPoints",$oAWifi)
$oOPoint = ObjCreate("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)
Local $sResponseJson = $oRest.FullRequestString("POST","/geolocation/v1/geolocate",$oJson.Emit())
If ($oRest.LastMethodSuccess = False) 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)
$oJsonLoc = ObjCreate("Chilkat.JsonObject")
$oJsonResp.ObjectOf2("location",$oJsonLoc)
; 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)