Lua Requires Chilkat v11.1.0+
Lua
Google Maps Geolocation Request
See more REST Examples
Demonstrates how make a Google Maps Geolocation REST API request.Chilkat Lua Downloads
local chilkat = require("chilkat")
-- 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.
local rest = chilkat.Rest.new()
-- Connect to the Google API REST server.
local bTls = true
local port = 443
local bAutoReconnect = true
local success = rest:connect("www.googleapis.com", port, bTls, bAutoReconnect)
-- Add the Content-Type request header.
rest:add_header("Content-Type", "application/json")
-- Add your API key as a query parameter
rest:add_query_param("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
-- }
-- ]
-- }
local json = chilkat.JsonObject.new()
json:append_int("homeMobileCountryCode", 310)
json:append_int("homeMobileNetworkCode", 260)
json:append_string("radioType", "gsm")
json:append_string("carrier", "T-Mobile")
local aCellTowers = chilkat.JsonArray.new()
json:append_array2("cellTowers", aCellTowers)
local oCellTower = chilkat.JsonObject.new()
aCellTowers:add_object_at2(0, oCellTower)
oCellTower:append_int("cellId", 39627456)
oCellTower:append_int("locationAreaCode", 40495)
oCellTower:append_int("mobileCountryCode", 310)
oCellTower:append_int("mobileNetworkCode", 260)
oCellTower:append_int("age", 0)
oCellTower:append_int("signalStrength", -95)
local aWifi = chilkat.JsonArray.new()
json:append_array2("wifiAccessPoints", aWifi)
local oPoint = chilkat.JsonObject.new()
aWifi:add_object_at2(0, oPoint)
oPoint:append_string("macAddress", "01:23:45:67:89:AB")
oPoint:append_int("signalStrength", 8)
oPoint:append_int("age", 0)
oPoint:append_int("signalToNoiseRatio", -65)
oPoint:append_int("channel", 8)
aWifi:add_object_at2(1, oPoint)
oPoint:append_string("macAddress", "01:23:45:67:89:AC")
oPoint:append_int("signalStrength", 4)
oPoint:append_int("age", 0)
local responseJson = rest:full_request_string("POST", "/geolocation/v1/geolocate", json:emit())
if not rest.last_method_success then
print(rest.last_error_text)
return
end
-- When successful, the response code is 200.
if rest.response_status_code ~= 200 then
-- Examine the request/response to see what happened.
print("response status code = " .. rest.response_status_code)
print("response status text = " .. rest.response_status_text)
print("response header: " .. rest.response_header)
print("response JSON: " .. responseJson)
print("---")
print("LastRequestStartLine: " .. rest.last_request_start_line)
print("LastRequestHeader: " .. rest.last_request_header)
return
end
json.emit_compact = false
print("JSON request body: " .. json:emit())
-- The JSON response should look like this:
-- {
-- "location": {
-- "lat": 37.4248297,
-- "lng": -122.07346549999998
-- },
-- "accuracy": 1145.0
-- }
print("JSON response: " .. responseJson)
local jsonResp = chilkat.JsonObject.new()
jsonResp:load(responseJson)
local jsonLoc = chilkat.JsonObject.new()
jsonResp:object_of2("location", jsonLoc)
-- Any JSON value can be obtained as a string..
local latitude = jsonLoc:string_of("lat")
print("latitude = " .. latitude)
local longitude = jsonLoc:string_of("lng")
print("longitude = " .. longitude)
local accuracy = jsonResp:string_of("accuracy")
print("accuracy = " .. accuracy)