-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMapViewController.swift
More file actions
131 lines (109 loc) · 5.68 KB
/
MapViewController.swift
File metadata and controls
131 lines (109 loc) · 5.68 KB
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
124
125
126
127
128
129
130
131
//
// MapViewController.swift
// Yago
//
// Created by Chad Collins on 2/24/15.
// Copyright (c) 2015 Team Socket Power. All rights reserved.
//
import UIKit
import MapKit
import MobileCoreServices
import CoreLocation
class MapViewController: CameraMenuItemController, CLLocationManagerDelegate, MKMapViewDelegate, UINavigationControllerDelegate, UIImagePickerControllerDelegate {
@IBOutlet weak var mapView: MKMapView!
var locationManager: CLLocationManager!
var districts: [DistrictModel] = []
var location: CLLocationCoordinate2D!
override func viewDidLoad() {
super.viewDidLoad()
mapView.delegate = self
locationManager = CLLocationManager()
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.requestAlwaysAuthorization()
locationManager.distanceFilter = 100.0 //how often manager is notified of view change
locationManager.startUpdatingLocation()
mapView.showsUserLocation = true
self.title = "Yago"
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {
self.location = locationManager.location.coordinate
getDistricts()
let span = MKCoordinateSpanMake(0.5,0.5)
mapView.setRegion(MKCoordinateRegionMake(location, span), animated: true)
}
func mapView(mapView: MKMapView!, viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView! {
if annotation is BarDistrictAnnotation {
let pinAnnotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: "myPin")
pinAnnotationView.canShowCallout = true
let selectButton: UIButton! = UIButton.buttonWithType(UIButtonType.Custom) as! UIButton
selectButton.frame.size.width = 44
selectButton.frame.size.height = 44
selectButton.backgroundColor = UIColor.darkGrayColor()
selectButton.setTitle(">", forState: UIControlState.Normal)
selectButton.setTitleColor(UIColor.whiteColor(), forState: UIControlState.Normal)
pinAnnotationView.rightCalloutAccessoryView = selectButton
return pinAnnotationView
}
return nil
}
func mapView(mapView: MKMapView!, annotationView view: MKAnnotationView!, calloutAccessoryControlTapped control: UIControl!) {
if let annotation = view.annotation as? BarDistrictAnnotation {
self.performSegueWithIdentifier("DistrictFeedSelected", sender: annotation)
}
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "DistrictFeedSelected" {
if let annotation = sender as? BarDistrictAnnotation {
let nextScene = segue.destinationViewController as! DistrictFeedViewController
let selectedDistrict = annotation.district
nextScene.currentDistrict = selectedDistrict
}
}
}
func extractLocationFromString(locationString: String) -> CLLocationCoordinate2D {
var pieces: [String] = locationString.componentsSeparatedByString(",")
return CLLocationCoordinate2DMake((pieces[0] as NSString).doubleValue, (pieces[1] as NSString).doubleValue)
}
func getDistricts() {
var districts:[DistrictModel] = []
let manager = AFHTTPRequestOperationManager()
var latLong = "\(String(self.location.latitude.description)),\(String(self.location.longitude.description))"
manager.GET( API_BASE_URL + "feed/location_feed/\(latLong)",
parameters: nil,
success: { (operation: AFHTTPRequestOperation!,responseObject: AnyObject!) in
if let items = responseObject as? NSArray {
for item in items {
var positionResult = item["position"]as! String
var feedItemName = item["name"] as! String
var feedItemLocation = self.extractLocationFromString(item["position"] as! String)
var feedItemId = item["pk"] as! Int
var feedItem:DistrictModel = DistrictModel(
id: item["pk"] as! Int,
name: feedItemName,
location: feedItemLocation
)
districts += [feedItem]
let annotation = BarDistrictAnnotation()
annotation.setCoordinate(feedItemLocation) //buckhead hard coded
annotation.title = feedItemName
annotation.district = DistrictModel(id: feedItemId, name: feedItemName, location: feedItemLocation)
self.mapView.addAnnotation(annotation)
self.mapView.selectAnnotation(annotation, animated: false)
/**
The intended functionality here is to have every pin display their label. It appears iOS only allows one
pin to be selected at anytime. http://stackoverflow.com/questions/2417952/multiple-annotation-callouts-displaying-in-mkmapview
**/
}
}
self.districts = districts
},
failure: { (operation: AFHTTPRequestOperation!,error: NSError!) in
println("Error: " + error.localizedDescription)
})
}
}