Skip to content

Instantly share code, notes, and snippets.

@ToeJamson
Created May 4, 2015 19:12
Show Gist options
  • Select an option

  • Save ToeJamson/54899c9d76f20b36c729 to your computer and use it in GitHub Desktop.

Select an option

Save ToeJamson/54899c9d76f20b36c729 to your computer and use it in GitHub Desktop.
Tracking Location Data for iOS w/ Swift and MapKit
$ pod init
$ {your favorite editor} Podfile
private var mapView = MKMapView()
private var locations = [CLLocation]()
private var coordinateList = [CLLocationCoordinate2D]()
private var isFirstMessage = Bool()
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
}
override func viewDidLoad() {
self.mapView.delegate = self
}
class YourViewController: UIViewController, PNDelegate, MKMapViewDelegate {
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)
}
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)
}
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)
}
func updateCurrentPositionMarker(currentLocation: CLLocation) {
self.currentPositionMarker.coordinate = currentLocation.coordinate
self.mapView.addAnnotation(self.currentPositionMarker)
}
platform :ios, "8.0"
target "YourProject" do
pod 'PubNub', '3.7.2'
end
target "YourProjectTests" do
end
$ pod install
#ifdef __OBJC__
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
#import "PNImports.h"
#endif
private var channel = PNChannel()
private let config = PNConfiguration(publishKey: "publish_key", subscribeKey: "subscribe_key", secretKey: nil)
PubNub.setDelegate(self)
PubNub.setConfiguration(self.config)
PubNub.connect()
self.channel = PNChannel.channelWithName("Channel-Name", shouldObservePresence: false) as PNChannel
PubNub.subscribeOnChannel(self.channel)
class YourViewController: UIViewController, PNDelegate {
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"]
}
import MapKit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment