Created
May 4, 2015 19:12
-
-
Save ToeJamson/54899c9d76f20b36c729 to your computer and use it in GitHub Desktop.
Tracking Location Data for iOS w/ Swift and MapKit
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| $ pod init | |
| $ {your favorite editor} Podfile |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| private var mapView = MKMapView() | |
| private var locations = [CLLocation]() | |
| private var coordinateList = [CLLocationCoordinate2D]() | |
| private var isFirstMessage = Bool() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| override func loadView() { | |
| // Building a view | |
| let screenFrame = UIScreen.mainScreen().applicationFrame | |
| let contentView = UIView(frame: screenFrame) | |
| // Add Map | |
| self.mapView = MKMapView(frame: screenFrame) | |
| contentView.addSubview(self.mapView) | |
| // Set the built view as our view | |
| self.view = contentView | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| override func viewDidLoad() { | |
| self.mapView.delegate = self | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class YourViewController: UIViewController, PNDelegate, MKMapViewDelegate { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| func pubnubClient(client: PubNub!, didReceiveMessage message: PNMessage!) { | |
| // Extract content from received message | |
| let receivedMessage = message.message as [NSString : Double] | |
| let lng : CLLocationDegrees! = receivedMessage["lng"] | |
| let lat : CLLocationDegrees! = receivedMessage["lat"] | |
| let alt : CLLocationDegrees! = receivedMessage["alt"] | |
| let newLocation2D = CLLocationCoordinate2DMake(lat, lng) | |
| let newLocation = CLLocation(coordinate: newLocation2D, altitude: alt, horizontalAccuracy: 0, verticalAccuracy: 0, timestamp: nil) | |
| self.locations.append(newLocation) | |
| self.coordinateList.append(newLocation.coordinate) | |
| // Drawing the line | |
| self.updateOverlay() | |
| // Focusing on the new position | |
| self.updateMapFrame() | |
| // Update Marker Position | |
| self.updateCurrentPositionMarker(newLocation) | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| func updateOverlay() { | |
| // Build the overlay | |
| let line = MKPolyline(coordinates: &self.coordinateList, count: self.coordinateList.count) | |
| // Replace overlay | |
| if !self.isFirstMessage { | |
| self.mapView.removeOverlays(self.mapView.overlays) | |
| } | |
| self.mapView.addOverlay(line) | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| func updateMapFrame() { | |
| let currentPosition = self.locations.last! | |
| let latitudeSpan = CLLocationDistance(500) | |
| let longitudeSpan = latitudeSpan | |
| let region = MKCoordinateRegionMakeWithDistance(currentPosition.coordinate, latitudeSpan, longitudeSpan) | |
| self.mapView.setRegion(region, animated: true) | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| func updateCurrentPositionMarker(currentLocation: CLLocation) { | |
| self.currentPositionMarker.coordinate = currentLocation.coordinate | |
| self.mapView.addAnnotation(self.currentPositionMarker) | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| platform :ios, "8.0" | |
| target "YourProject" do | |
| pod 'PubNub', '3.7.2' | |
| end | |
| target "YourProjectTests" do | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| $ pod install |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #ifdef __OBJC__ | |
| #import <UIKit/UIKit.h> | |
| #import <Foundation/Foundation.h> | |
| #import "PNImports.h" | |
| #endif |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| private var channel = PNChannel() | |
| private let config = PNConfiguration(publishKey: "publish_key", subscribeKey: "subscribe_key", secretKey: nil) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| PubNub.setDelegate(self) | |
| PubNub.setConfiguration(self.config) | |
| PubNub.connect() | |
| self.channel = PNChannel.channelWithName("Channel-Name", shouldObservePresence: false) as PNChannel | |
| PubNub.subscribeOnChannel(self.channel) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class YourViewController: UIViewController, PNDelegate { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| func pubnubClient(client: PubNub!, didReceiveMessage message: PNMessage!) { | |
| // Extract content from received message | |
| let receivedMessage = message.message as [NSString : Double] | |
| let lng : CLLocationDegrees! = receivedMessage["lng"] | |
| let lat : CLLocationDegrees! = receivedMessage["lat"] | |
| let alt : CLLocationDegrees! = receivedMessage["alt"] | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import MapKit |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment