mardi 26 novembre 2019

webAPI calls using swift

I am working with webAPI calls using GeoNames WebServices to find the earthquake information for a given city and surrounding area. How does one make webAPI calls. So far I've gotten the longitude and latitude of the address. I am having difficulty understanding how we get the information. I would like to display the Display the datetime and magnitude of each earth quick

@IBAction func getAddress(_ sender: Any) {
    let addString = location.text
    CLGeocoder().geocodeAddressString(addString!, completionHandler:
    {(placemarks, error) in

        if error != nil {
            print("Geocode failed: \(error!.localizedDescription)")
        }else if placemarks!.count > 0{
            let placemark = placemarks![0]
            let location = placemark.location
            let coords = location!.coordinate

            self.lat = coords.latitude
            self.long = coords.longitude

            print(self.lat, "\n")
            print(self.long, "\n")
        }
        self.south = self.south + self.lat
        self.north = self.north + self.lat
        self.east = self.east + self.long
        self.west = self.west + self.long
        print("\n",self.north, "\n", self.south, "\n", self.east, "\n", self.west)
    })


    let urlAaString = "http://api.geonames.org/earthquakesJSON?north=" + String(self.north) +
    "&south=" + String(self.south) +
    "&east=" + String(self.east) +
    "&west=" + String(self.west) + "&username=demo"

    let url = NSURL(string: urlAaString)!
    let urlSession = URLSession.shared

    let jsonQuery = urlSession.dataTask(with: url as URL, completionHandler: { data, response, error -> Void in
        if (error != nil) {
            print(error!.localizedDescription)
        }
        var err: NSError?


        let jsonResult = (try! JSONSerialization.jsonObject(with: data!, options: JSONSerialization.ReadingOptions.mutableContainers)) as? NSDictionary
        if (err != nil) {
            print("JSON Error \(err!.localizedDescription)")
        }

        print(jsonResult)

    })

    jsonQuery.resume()
}



Aucun commentaire:

Enregistrer un commentaire