Skip to content

Instantly share code, notes, and snippets.

@veritech
Created May 13, 2017 16:41
Show Gist options
  • Save veritech/b7947c2bde96c4538422bfcd9ab4a729 to your computer and use it in GitHub Desktop.
Save veritech/b7947c2bde96c4538422bfcd9ab4a729 to your computer and use it in GitHub Desktop.
Current location experiment
//
// ViewController.swift
// CurrentLocation
//
// Created by Jonathan Dalrymple on 13/05/2017.
// Copyright © 2017 float-right. All rights reserved.
//
import UIKit
import GoogleMaps
class CurrentLocationView: UIView {
private class CircularView: UIView {
override class var layerClass: AnyClass {
return CAShapeLayer.self
}
private var shapeLayer: CAShapeLayer? {
get {
if let shape = self.layer as? CAShapeLayer {
return shape
}
return nil
}
}
override init(frame: CGRect) {
super.init(frame: frame)
super.backgroundColor = .clear
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func layoutSubviews() {
super.layoutSubviews()
let path = UIBezierPath(roundedRect: self.bounds,
cornerRadius: self.bounds.width * 0.5).cgPath
self.shapeLayer?.path = path
}
override var backgroundColor: UIColor? {
set (newValue){
self.shapeLayer?.fillColor = newValue?.cgColor
}
get {
if let color = self.layer.backgroundColor {
return UIColor(cgColor: color)
}
return nil
}
}
}
private let baseView = CircularView()
private let dotView = CircularView()
private let rangeView = CircularView()
override init(frame: CGRect) {
super.init(frame: frame)
self.addSubview(self.rangeView)
self.addSubview(self.baseView)
self.addSubview(self.dotView)
self.rangeView.backgroundColor = UIColor.blue.withAlphaComponent(0.25)
self.baseView.backgroundColor = .white
self.dotView.backgroundColor = .blue
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func layoutSubviews() {
super.layoutSubviews()
self.rangeView.bounds = self.bounds
self.rangeView.center = self.center
self.baseView.bounds = CGRect(x: 0, y: 0, width: 32, height: 32)
self.baseView.center = self.center
self.dotView.bounds = CGRect(x: 0, y: 0, width: 20, height: 20)
self.dotView.center = self.center
}
func startAnimating() {
let dotPulse = CABasicAnimation(keyPath: "transform")
dotPulse.byValue = CATransform3DMakeScale(0.8, 0.8, 1)
dotPulse.duration = 1.25
dotPulse.repeatCount = .infinity
dotPulse.autoreverses = true
self.dotView.layer.add(dotPulse, forKey: "pulsate")
let rangePulse = CABasicAnimation(keyPath: "transform")
rangePulse.fromValue = CATransform3DMakeScale(0.1, 0.1, 1)
rangePulse.duration = 2.5
rangePulse.repeatCount = .infinity
self.rangeView.layer.add(rangePulse, forKey: "pulsate")
let fadeOut = CABasicAnimation(keyPath: "opacity")
fadeOut.toValue = 0.0
fadeOut.duration = 2.5
fadeOut.repeatCount = .infinity
self.rangeView.layer.add(fadeOut, forKey: "fadeOut")
}
}
class ViewController: UIViewController, GMSMapViewDelegate {
lazy var mapView: GMSMapView = {
let view = GMSMapView()
view.delegate = self
return view
}()
lazy var currentLocationMarker: GMSMarker = {
let marker = GMSMarker(position: CLLocationCoordinate2D(latitude: 0, longitude: 0))
let annotationView = CurrentLocationView(frame: CGRect(x: 0, y: 0, width: 320, height: 320))
marker.groundAnchor = CGPoint(x: 0.5, y: 0.5)
marker.iconView = annotationView
return marker
}()
override func viewDidLoad() {
super.viewDidLoad()
GMSServices.provideAPIKey("INSERT YOUR GMS API KEY")
self.view.addSubview(self.mapView)
let location = CLLocationCoordinate2D(latitude: 51, longitude: 0)
self.currentLocationMarker.position = location
self.currentLocationMarker.map = self.mapView
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
self.mapView.frame = self.view.bounds
}
func mapViewSnapshotReady(_ mapView: GMSMapView) {
if let v = self.currentLocationMarker.iconView as? CurrentLocationView {
v.startAnimating()
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment