Sample code for 30+ languages & platforms
Swift

Google Maps Geolocation Request

See more REST Examples

Demonstrates how make a Google Maps Geolocation REST API request.

Chilkat Swift Downloads

Swift

func chilkatTest() {
    var success: Bool = 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.

    let rest = CkoRest()!

    // Connect to the Google API REST server.
    var bTls: Bool = true
    var port: Int = 443
    var bAutoReconnect: Bool = true
    success = rest.connect(hostname: "www.googleapis.com", port: port, tls: bTls, autoReconnect: bAutoReconnect)

    // Add the Content-Type request header.
    rest.addHeader(name: "Content-Type", value: "application/json")

    // Add your API key as a query parameter
    rest.addQueryParam(name: "key", value: "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 = CkoJsonObject()!
    json.appendInt(name: "homeMobileCountryCode", value: 310)
    json.appendInt(name: "homeMobileNetworkCode", value: 260)
    json.appendString(name: "radioType", value: "gsm")
    json.appendString(name: "carrier", value: "T-Mobile")

    let aCellTowers = CkoJsonArray()!
    json.appendArray2(name: "cellTowers", jarr: aCellTowers)

    let oCellTower = CkoJsonObject()!
    aCellTowers.addObjectAt2(index: 0, json: oCellTower)
    oCellTower.appendInt(name: "cellId", value: 39627456)
    oCellTower.appendInt(name: "locationAreaCode", value: 40495)
    oCellTower.appendInt(name: "mobileCountryCode", value: 310)
    oCellTower.appendInt(name: "mobileNetworkCode", value: 260)
    oCellTower.appendInt(name: "age", value: 0)
    oCellTower.appendInt(name: "signalStrength", value: -95)

    let aWifi = CkoJsonArray()!
    json.appendArray2(name: "wifiAccessPoints", jarr: aWifi)

    let oPoint = CkoJsonObject()!
    aWifi.addObjectAt2(index: 0, json: oPoint)
    oPoint.appendString(name: "macAddress", value: "01:23:45:67:89:AB")
    oPoint.appendInt(name: "signalStrength", value: 8)
    oPoint.appendInt(name: "age", value: 0)
    oPoint.appendInt(name: "signalToNoiseRatio", value: -65)
    oPoint.appendInt(name: "channel", value: 8)

    aWifi.addObjectAt2(index: 1, json: oPoint)
    oPoint.appendString(name: "macAddress", value: "01:23:45:67:89:AC")
    oPoint.appendInt(name: "signalStrength", value: 4)
    oPoint.appendInt(name: "age", value: 0)

    var responseJson: String? = rest.fullRequestString(httpVerb: "POST", uriPath: "/geolocation/v1/geolocate", bodyText: json.emit())
    if rest.lastMethodSuccess == false {
        print("\(rest.lastErrorText!)")
        return
    }

    // When successful, the response code is 200.
    if rest.responseStatusCode.intValue != 200 {
        // Examine the request/response to see what happened.
        print("response status code = \(rest.responseStatusCode.intValue)")
        print("response status text = \(rest.responseStatusText!)")
        print("response header: \(rest.responseHeader!)")
        print("response JSON: \(responseJson!)")
        print("---")
        print("LastRequestStartLine: \(rest.lastRequestStartLine!)")
        print("LastRequestHeader: \(rest.lastRequestHeader!)")
        return
    }

    json.emitCompact = 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!)")

    let jsonResp = CkoJsonObject()!
    jsonResp.load(json: responseJson)

    let jsonLoc = CkoJsonObject()!
    jsonResp.objectOf2(jsonPath: "location", jsonObj: jsonLoc)

    // Any JSON value can be obtained as a string..
    var latitude: String? = jsonLoc.string(of: "lat")
    print("latitude = \(latitude!)")
    var longitude: String? = jsonLoc.string(of: "lng")
    print("longitude = \(longitude!)")

    var accuracy: String? = jsonResp.string(of: "accuracy")
    print("accuracy = \(accuracy!)")

}