Sample code for 30+ languages & platforms
Zig Requires Chilkat v11.1.0+

Google Maps Geolocation Request

See more REST Examples

Demonstrates how make a Google Maps Geolocation REST API request.

Chilkat Zig Downloads

Zig
const std = @import("std");
const chilkat = @import("chilkat");

pub fn main(init: std.process.Init) !void {
    const alloc = init.arena.allocator();

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

    const rest = try chilkat.Rest.init();
    defer rest.deinit();

    // Connect to the Google API REST server.
    const b_tls = true;
    const port = 443;
    const b_auto_reconnect = true;
    try rest.connect("www.googleapis.com", port, b_tls, b_auto_reconnect);

    // Add the Content-Type request header.
    rest.addHeader("Content-Type", "application/json") catch {};

    // Add your API key as a query parameter
    rest.addQueryParam("key", "YOUR_API_KEY") catch {};

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

    const json = try chilkat.JsonObject.init();
    defer json.deinit();
    json.appendInt("homeMobileCountryCode", 310) catch {};
    json.appendInt("homeMobileNetworkCode", 260) catch {};
    json.appendString("radioType", "gsm") catch {};
    json.appendString("carrier", "T-Mobile") catch {};

    const a_cell_towers = try chilkat.JsonArray.init();
    defer a_cell_towers.deinit();
    json.appendArray2("cellTowers", a_cell_towers) catch {};

    const o_cell_tower = try chilkat.JsonObject.init();
    defer o_cell_tower.deinit();
    a_cell_towers.addObjectAt2(0, o_cell_tower) catch {};
    o_cell_tower.appendInt("cellId", 39627456) catch {};
    o_cell_tower.appendInt("locationAreaCode", 40495) catch {};
    o_cell_tower.appendInt("mobileCountryCode", 310) catch {};
    o_cell_tower.appendInt("mobileNetworkCode", 260) catch {};
    o_cell_tower.appendInt("age", 0) catch {};
    o_cell_tower.appendInt("signalStrength", -95) catch {};

    const a_wifi = try chilkat.JsonArray.init();
    defer a_wifi.deinit();
    json.appendArray2("wifiAccessPoints", a_wifi) catch {};

    const o_point = try chilkat.JsonObject.init();
    defer o_point.deinit();
    a_wifi.addObjectAt2(0, o_point) catch {};
    o_point.appendString("macAddress", "01:23:45:67:89:AB") catch {};
    o_point.appendInt("signalStrength", 8) catch {};
    o_point.appendInt("age", 0) catch {};
    o_point.appendInt("signalToNoiseRatio", -65) catch {};
    o_point.appendInt("channel", 8) catch {};

    a_wifi.addObjectAt2(1, o_point) catch {};
    o_point.appendString("macAddress", "01:23:45:67:89:AC") catch {};
    o_point.appendInt("signalStrength", 4) catch {};
    o_point.appendInt("age", 0) catch {};

    const response_json = rest.fullRequestString(alloc, "POST", "/geolocation/v1/geolocate", try json.emit(alloc)) catch {
        std.debug.print("{s}\n", .{try rest.getLastErrorText(alloc)});
        return;
    };

    // When successful, the response code is 200.
    if (rest.getResponseStatusCode() != 200) {
        // Examine the request/response to see what happened.
        std.debug.print("response status code = {d}\n", .{rest.getResponseStatusCode()});
        std.debug.print("response status text = {s}\n", .{try rest.getResponseStatusText(alloc)});
        std.debug.print("response header: {s}\n", .{try rest.getResponseHeader(alloc)});
        std.debug.print("response JSON: {s}\n", .{response_json});
        std.debug.print("---\n", .{});
        std.debug.print("LastRequestStartLine: {s}\n", .{try rest.getLastRequestStartLine(alloc)});
        std.debug.print("LastRequestHeader: {s}\n", .{try rest.getLastRequestHeader(alloc)});
        return;
    }

    json.setEmitCompact(false);
    std.debug.print("JSON request body: {s}\n", .{try json.emit(alloc)});

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

    std.debug.print("JSON response: {s}\n", .{response_json});

    const json_resp = try chilkat.JsonObject.init();
    defer json_resp.deinit();
    json_resp.load(response_json) catch {};

    const json_loc = try chilkat.JsonObject.init();
    defer json_loc.deinit();
    json_resp.objectOf2("location", json_loc) catch {};

    // Any JSON value can be obtained as a string..
    const latitude = try json_loc.stringOf(alloc, "lat");
    std.debug.print("latitude = {s}\n", .{latitude});
    const longitude = try json_loc.stringOf(alloc, "lng");
    std.debug.print("longitude = {s}\n", .{longitude});

    const accuracy = try json_resp.stringOf(alloc, "accuracy");
    std.debug.print("accuracy = {s}\n", .{accuracy});
}