WOEIDListArchive

WOEIDListArchive

WOEIDListArchive

//
//  WOEIDImporter.swift
//  WOEID
//
//  Created by Park on 11/21/15.
//  Copyright © 2015 Park. All rights reserved.
//

import Foundation

extension String {
func toDouble() -> Double {
if let unwrappedNum = Double(self) {
return unwrappedNum
}
else {
print(“Error converting \”” + self + “\” to Double”)
return 0.0
}
}
func toInt64() -> Int64 {
if let unwrappedNum = Int64(self) {
return unwrappedNum
}
else {
print(“Error converting \”” + self + “\” to Int64″)
return 0
}
}
}

class WOEIDImporter: NSObject {
var woeidList : [WOEID] = [WOEID]() // XML parser woeidList
var woeid: WOEID! = nil // XML parser woeid
var parser: NSXMLParser? = nil // XML parser
var xmlData: String = “” // XML elementName data

override init() {
super.init()
print(“WOEIDImporter”)
}

func loadData() -> [WOEID] {
var woeidList:[WOEID] = [WOEID] ()
woeidList.append(WOEID(code: 4118, city: “Toronto”, country: “Canada”, latitude: 43.64856, longitude: -79.385368))
woeidList.append(WOEID(code: 44418, city: “London”, country: “United Kingdom”, latitude: 51.507702, longitude: -0.12797))
woeidList.append(WOEID(code: 2487956, city: “San Francisco”, country: “United States”, latitude: 37.747398, longitude: -122.439217))
woeidList.append(WOEID(code: 1132599, city: “Seoul”, country: “South Korea”, latitude: 37.557121, longitude: 126.977379))
woeidList.append(WOEID(code: 718345, city: “Milan”, country: “Italy”, latitude: 45.4781, longitude: 9.17789))
return woeidList
}

}

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet weak var aTableView: UITableView!
let importer: WOEIDImporter = WOEIDImporter()
var woeidList: [WOEID]! = nil
var woeid: WOEID! = nil

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.title = “WOEID”

aTableView.delegate = self
aTableView.dataSource = self
aTableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: “cell”)

woeidList = importer.loadData() // test data

}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
return woeidList.count
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{

let cell: UITableViewCell = aTableView.dequeueReusableCellWithIdentifier(“cell”) as UITableViewCell!
let woeid = woeidList[indexPath.row] as WOEID
cell.textLabel?.text = “\(woeid.code) \(woeid.city) \(woeid.country)”
cell.accessoryType = UITableViewCellAccessoryType.DisclosureIndicator
print(cell.textLabel?.text)
return cell
}

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
{
tableView.deselectRowAtIndexPath(indexPath, animated: true)
print(“You selected cell #\(indexPath.row)”)

woeid = woeidList[indexPath.row] as WOEID
self.performSegueWithIdentifier(“ShowSimpleVCSegue”, sender: self)

}

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
if (segue.identifier == “ShowSimpleVCSegue”) {
if let destinationVC = segue.destinationViewController as? SimpleViewController {
print(“SimpleTableViewController”)
destinationVC.woeid = woeid
}
}
}

}

 

//
//  SimpleTableViewController.swift
//  WOEID
//
//  Created by Park on 11/22/15.
//  Copyright © 2015 Park. All rights reserved.
//

import UIKit
import MapKit
import CoreLocation

class SimpleViewController: UIViewController, MKMapViewDelegate {

@IBOutlet weak var aLabel: UILabel!
var woeid: WOEID!

@IBOutlet weak var aMapView: MKMapView!

override func viewDidLoad() {
super.viewDidLoad()

// Do any additional setup after loading the view.
print(woeid.description)
aLabel.text = woeid.description

zoomToRegion(woeid.latitude, woeid.longitude)
aMapView.addAnnotation(woeid)
aMapView.delegate = self
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

func zoomToRegion(lat: CLLocationDegrees, _ lon: CLLocationDegrees) {
let regionRadius: CLLocationDistance = 1000
let location = CLLocationCoordinate2D(latitude: lat, longitude: lon)
let region = MKCoordinateRegionMakeWithDistance(location, regionRadius * 5.0, regionRadius * 7.0)

aMapView.setRegion(region, animated: true)
}

func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {
if let annotation = annotation as? WOEID {
let identifier = “pin”
var view: MKPinAnnotationView
if let dequeuedView = mapView.dequeueReusableAnnotationViewWithIdentifier(identifier)
as? MKPinAnnotationView {
dequeuedView.annotation = annotation
view = dequeuedView
} else {
view = MKPinAnnotationView(annotation: annotation, reuseIdentifier: identifier)
view.canShowCallout = true
view.calloutOffset = CGPoint(x: -5, y: 5)
view.rightCalloutAccessoryView = UIButton(type: .DetailDisclosure) as? UIView
}
return view
}
return nil
}

/*
// MARK: – Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
*/

}