-
Notifications
You must be signed in to change notification settings - Fork 0
/
page3InterfaceController.swift
123 lines (95 loc) · 4.36 KB
/
page3InterfaceController.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
//
// page1InterfaceController.swift
// appleWatch2
//
// Created by Elizabeth Siegle on 7/11/15.
// Copyright (c) 2015 Elizabeth Siegle. All rights reserved.
//
import WatchKit
import Foundation
class page3InterfaceController: WKInterfaceController {
@IBOutlet weak var page3Label: WKInterfaceLabel!
@IBOutlet weak var page3Map: WKInterfaceMap!
override func awakeWithContext(context: AnyObject?) {
super.awakeWithContext(context)
// Configure interface objects here.
}
func getDistance(lat1: Double, lng1: Double, lat2: Double, lng2: Double) -> Double{
var R=6371.0
var x=(lng2-lng1) * cos(0.5*(lat2+lat1))
var y=lat2-lat1
var distance = R*sqrt(x*x + y*y)
return distance
}
func sorterForDistance(this:[Double], that:[Double]) -> Bool {
let thisDistance=getDistance(userLocation[0], lng1: userLocation[1], lat2: this[0], lng2: this[1])
let thatDistance=getDistance(userLocation[0], lng1: userLocation[1], lat2: that[0], lng2: that[1])
return thisDistance < thatDistance
}
override func willActivate() {
// This method is called when watch view controller is about to be visible to user
super.willActivate()
println(userLocation)
self.page3Map.removeAllAnnotations()
let defaults = NSUserDefaults.standardUserDefaults()
let bundle = NSBundle.mainBundle()
let path = bundle.pathForResource("MemoryData", ofType: "json")
let content = NSData(contentsOfFile: path!)! as NSData
//println(content) // prints the content of data.txt
let json: NSDictionary = NSJSONSerialization.JSONObjectWithData(content, options: NSJSONReadingOptions.MutableContainers, error: nil) as! NSDictionary
defaults.setObject(json, forKey: "json")
var memoryArray=json["result"] as! NSArray
var locationOfMemories:[[Double]]=[]
var idx=0.0
for i in memoryArray{
//println(i)
locationOfMemories.append([(i["lat"] as! NSString).doubleValue, (i["lng"] as! NSString).doubleValue, idx])
//print(i["lat"] as! String+" ")
//println(i["lng"] as! String)
idx+=1
}
locationOfMemories.sort(sorterForDistance)
defaults.setObject(locationOfMemories, forKey: "locationOfMemories")
var location = CLLocationCoordinate2D(
latitude: userLocation[0],
longitude: userLocation[1]
)
var span = MKCoordinateSpanMake(0.2, 0.2)
var region = MKCoordinateRegion(center: location, span: span)
self.page3Map.setRegion(region)
if let temp = defaults.dictionaryForKey("json")
{
let defaults = NSUserDefaults.standardUserDefaults()
if let temp = defaults.dictionaryForKey("json")
{
let jsonArray=temp["result"] as! NSArray
var locationOfMemories=defaults.arrayForKey("locationOfMemories") as! [[Double]]
let closestMemoryIndex=Int(locationOfMemories[0][2])
//self.page3TWKInterfaceImage.setImage(jsonArray[closestMemoryIndex]["media"] as! String)
}
let jsonArray=temp["result"] as! NSArray
var locationOfMemories=defaults.arrayForKey("locationOfMemories") as! [[Double]]
for i in locationOfMemories[0..<4]{
let curIndex=Int(i[2])
location = CLLocationCoordinate2D(
latitude: i[0],
longitude: i[1]
)
var annotation = MKPointAnnotation()
annotation.coordinate = location
annotation.title = "Roatan"
annotation.subtitle = "Honduras"
self.page3Map.addAnnotation(location, withPinColor: WKInterfaceMapPinColor.Red)
}
}
location = CLLocationCoordinate2D(
latitude: userLocation[0],
longitude: userLocation[1]
)
self.page3Map.addAnnotation(location, withPinColor: WKInterfaceMapPinColor.Purple)
}
override func didDeactivate() {
// This method is called when watch view controller is no longer visible
super.didDeactivate()
}
}