Skip to content

Instantly share code, notes, and snippets.

@domadev812
Forked from ToeJamson/1.bash
Last active November 15, 2017 21:06
Show Gist options
  • Save domadev812/9efe9636f7e6bd264c5ca4e5bb90fb83 to your computer and use it in GitHub Desktop.
Save domadev812/9efe9636f7e6bd264c5ca4e5bb90fb83 to your computer and use it in GitHub Desktop.
Publishing iOS Location Data w/ Swift and Google Maps API
$ pod init
$ {your favorite editor} Podfile
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
}
override func viewDidLoad() {
self.mapView.delegate = self
}
class YourViewController: UIViewController, PNDelegate, GMSMapViewDelegate {
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)
}
func initializePolylineAnnotation() {
self.polyline.strokeColor = UIColor.blueColor()
self.polyline.strokeWidth = 5.0
self.polyline.map = self.mapView
}
func updateOverlay(currentPosition: CLLocation) {
self.path.addCoordinate(currentPosition.coordinate)
self.polyline.path = self.path
}
func updateMapFrame(newLocation: CLLocation, zoom: Float) {
let camera = GMSCameraPosition.cameraWithTarget(newLocation.coordinate, zoom: zoom)
self.mapView.animateToCameraPosition(camera)
}
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
}
platform :ios, "8.0"
target "YourProject" do
pod "PubNub", "~> 4"
pod 'Google-Maps-iOS-SDK'
end
target "YourProjectTests" do
end
$ pod install
#ifdef __OBJC__
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
#import "PNImports.h"
#import "GoogleMaps.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"]
}
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