Skip to content

Instantly share code, notes, and snippets.

@Alicelovecode
Created August 19, 2017 08:49
Show Gist options
  • Select an option

  • Save Alicelovecode/496411f49993dc94ebd072709b7d98c2 to your computer and use it in GitHub Desktop.

Select an option

Save Alicelovecode/496411f49993dc94ebd072709b7d98c2 to your computer and use it in GitHub Desktop.
clima1.swift
import UIKit
import CoreLocation
class WeatherViewController: UIViewController,CLLocationManagerDelegate {
//Constants
let WEATHER_URL = "http://api.openweathermap.org/data/2.5/weather"
let APP_ID = "e72ca729af228beabd5d20e3b7749713"
//TODO: Declare instance variables here
let locationManager = CLLocationManager()
//Pre-linked IBOutlets
@IBOutlet weak var weatherIcon: UIImageView!
@IBOutlet weak var cityLabel: UILabel!
@IBOutlet weak var temperatureLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
//TODO:Set up the location manager here.
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters
locationManager.requestWhenInUseAuthorization()
locationManager.startUpdatingLocation()
}
//MARK: - Networking
/***************************************************************/
//Write the getWeatherData method here:
//MARK: - JSON Parsing
/***************************************************************/
//Write the updateWeatherData method here:
//MARK: - UI Updates
/***************************************************************/
//Write the updateUIWithWeatherData method here:
//MARK: - Location Manager Delegate Methods
/***************************************************************/
//Write the didUpdateLocations method here:
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let location = locations[locations.count - 1 ]
//如果高度太高就停止讀入新的地理位置,因為高度會影響溫度
if location.horizontalAccuracy > 0 {
locationManager.stopUpdatingLocation()
print("longitude = \(location.coordinate.longitude),latitude = \(location.coordinate.latitude)")
let latitude = String(location.coordinate.latitude)
let longitude = String(location.coordinate.longitude)
let params : [String : String] = ["lat" : latitude, "lon" : longitude, "appid" : APP_ID ]
}
}
//Write the didFailWithError method here:
func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
print(error)
cityLabel.text = "找不到所在地"
}
//MARK: - Change City Delegate methods
/***************************************************************/
//Write the userEnteredANewCityName Delegate method here:
//Write the PrepareForSegue Method here
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment