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

Google Maps Geolocation Request

See more REST Examples

Demonstrates how make a Google Maps Geolocation REST API request.

Chilkat Rust Downloads

Rust

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

let rest = chilkat::Rest::new();

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

// Add the Content-Type request header.
let _ = rest.add_header("Content-Type", "application/json");

// Add your API key as a query parameter
let _ = 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
//   }
//  ]
// }

let json = chilkat::JsonObject::new();
let _ = json.append_int("homeMobileCountryCode", 310);
let _ = json.append_int("homeMobileNetworkCode", 260);
let _ = json.append_string("radioType", "gsm");
let _ = json.append_string("carrier", "T-Mobile");

let a_cell_towers = chilkat::JsonArray::new();
let _ = json.append_array2("cellTowers", &a_cell_towers);

let o_cell_tower = chilkat::JsonObject::new();
let _ = a_cell_towers.add_object_at2(0, &o_cell_tower);
let _ = o_cell_tower.append_int("cellId", 39627456);
let _ = o_cell_tower.append_int("locationAreaCode", 40495);
let _ = o_cell_tower.append_int("mobileCountryCode", 310);
let _ = o_cell_tower.append_int("mobileNetworkCode", 260);
let _ = o_cell_tower.append_int("age", 0);
let _ = o_cell_tower.append_int("signalStrength", -95);

let a_wifi = chilkat::JsonArray::new();
let _ = json.append_array2("wifiAccessPoints", &a_wifi);

let o_point = chilkat::JsonObject::new();
let _ = a_wifi.add_object_at2(0, &o_point);
let _ = o_point.append_string("macAddress", "01:23:45:67:89:AB");
let _ = o_point.append_int("signalStrength", 8);
let _ = o_point.append_int("age", 0);
let _ = o_point.append_int("signalToNoiseRatio", -65);
let _ = o_point.append_int("channel", 8);

let _ = a_wifi.add_object_at2(1, &o_point);
let _ = o_point.append_string("macAddress", "01:23:45:67:89:AC");
let _ = o_point.append_int("signalStrength", 4);
let _ = o_point.append_int("age", 0);

let Ok(response_json) = rest.full_request_string("POST", "/geolocation/v1/geolocate", &json.emit().unwrap_or_default()) else {
    println!("{}", rest.last_error_text());
    return;
};

// When successful, the response code is 200.
if rest.response_status_code() != 200 {
    // Examine the request/response to see what happened.
    println!("response status code = {}", rest.response_status_code());
    println!("response status text = {}", rest.response_status_text());
    println!("response header: {}", rest.response_header());
    println!("response JSON: {}", response_json);
    println!("---");
    println!("LastRequestStartLine: {}", rest.last_request_start_line());
    println!("LastRequestHeader: {}", rest.last_request_header());
    return;
}

json.set_emit_compact(false);
println!("JSON request body: {}", json.emit().unwrap_or_default());

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

println!("JSON response: {}", response_json);

let json_resp = chilkat::JsonObject::new();
let _ = json_resp.load(&response_json);

let json_loc = chilkat::JsonObject::new();
let _ = json_resp.object_of2("location", &json_loc);

// Any JSON value can be obtained as a string..
let latitude = json_loc.string_of("lat").unwrap_or_default();
println!("latitude = {}", latitude);
let longitude = json_loc.string_of("lng").unwrap_or_default();
println!("longitude = {}", longitude);

let accuracy = json_resp.string_of("accuracy").unwrap_or_default();
println!("accuracy = {}", accuracy);