Created
May 4, 2015 22:41
-
-
Save ToeJamson/6c4d2d446e6b7322349b to your computer and use it in GitHub Desktop.
Publishing iOS Location Data w/ Swift and Google Maps API
This file contains 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 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 | |
GMSServices.provideAPIKey("Insert your API Key here") | |
self.mapView = GMSMapView(frame: screenFrame) | |
contentView.addSubview(self.mapView) | |
// Set the built view as our view | |
self.view = contentView | |
} |
This file contains 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 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, GMSMapViewDelegate { |
This file contains 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) | |
// Initilize the polyline | |
if (self.isFirstMessage) { | |
self.initializePolylineAnnotation() | |
self.isFirstMessage = false | |
} | |
// Drawing the line | |
self.updateOverlay(newLocation) | |
// Update the map frame | |
self.updateMapFrame(newLocation, zoom: 17.0) | |
// Update Marker position | |
self.updateCurrentPositionMarker(newLocation) | |
} |
This file contains 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 initializePolylineAnnotation() { | |
self.polyline.strokeColor = UIColor.blueColor() | |
self.polyline.strokeWidth = 5.0 | |
self.polyline.map = self.mapView | |
} |
This file contains 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(currentPosition: CLLocation) { | |
self.path.addCoordinate(currentPosition.coordinate) | |
self.polyline.path = self.path | |
} |
This file contains 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(newLocation: CLLocation, zoom: Float) { | |
let camera = GMSCameraPosition.cameraWithTarget(newLocation.coordinate, zoom: zoom) | |
self.mapView.animateToCameraPosition(camera) | |
} |
This file contains 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.map = nil | |
self.currentPositionMarker = GMSMarker(position: currentLocation.coordinate) | |
self.currentPositionMarker.icon = GMSMarker.markerImageWithColor(UIColor.cyanColor()) | |
self.currentPositionMarker.map = self.mapView | |
} |
This file contains 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' | |
pod 'Google-Maps-iOS-SDK' | |
end | |
target "YourProjectTests" do | |
end |
This file contains 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 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" | |
#import "GoogleMaps.h" | |
#endif |
This file contains 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 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 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 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 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 : GMSMapView! | |
private var locations = [CLLocation]() | |
private var path = GMSMutablePath() | |
private var polyline = GMSPolyline() | |
private var currentPositionMarker = GMSMarker() | |
private var isFirstMessage = true |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment