Skip to content

Commit

Permalink
Merge pull request #3 from yoman07/feature/follow_button
Browse files Browse the repository at this point in the history
Feature/follow button
  • Loading branch information
yoman07 authored May 6, 2017
2 parents 11e953d + e048de8 commit a217629
Show file tree
Hide file tree
Showing 10 changed files with 225 additions and 63 deletions.
15 changes: 15 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# 0.1.4
- fixing bugs for getting location permission
- localize me button as example

# 0.1.3

- swift 3.1
- config for region tracking
- improvements in asking for location permission

# 0.1.1
- heading tracking

# 0.1.0
- background location tracking
28 changes: 28 additions & 0 deletions Example/SwiftBackgroundLocation.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
objects = {

/* Begin PBXBuildFile section */
3944C7BE1EBDC44100972519 /* LocalizeMeButton.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3944C7BD1EBDC44100972519 /* LocalizeMeButton.swift */; };
3944C7C11EBDC94400972519 /* MKMapView+Center.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3944C7C01EBDC94400972519 /* MKMapView+Center.swift */; };
3944C7C31EBDD56B00972519 /* ViewController+LocalizeMe.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3944C7C21EBDD56B00972519 /* ViewController+LocalizeMe.swift */; };
395B50761E6C3992009CDC8A /* BackgroundDebug.swift in Sources */ = {isa = PBXBuildFile; fileRef = 395B50721E6C3992009CDC8A /* BackgroundDebug.swift */; };
395B50771E6C3992009CDC8A /* LocationLogger.swift in Sources */ = {isa = PBXBuildFile; fileRef = 395B50731E6C3992009CDC8A /* LocationLogger.swift */; };
395B50781E6C3992009CDC8A /* Points+Hermite.swift in Sources */ = {isa = PBXBuildFile; fileRef = 395B50741E6C3992009CDC8A /* Points+Hermite.swift */; };
Expand All @@ -30,6 +33,9 @@
/* End PBXContainerItemProxy section */

/* Begin PBXFileReference section */
3944C7BD1EBDC44100972519 /* LocalizeMeButton.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = LocalizeMeButton.swift; sourceTree = "<group>"; };
3944C7C01EBDC94400972519 /* MKMapView+Center.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "MKMapView+Center.swift"; sourceTree = "<group>"; };
3944C7C21EBDD56B00972519 /* ViewController+LocalizeMe.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "ViewController+LocalizeMe.swift"; sourceTree = "<group>"; };
395B50721E6C3992009CDC8A /* BackgroundDebug.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = BackgroundDebug.swift; sourceTree = "<group>"; };
395B50731E6C3992009CDC8A /* LocationLogger.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = LocationLogger.swift; sourceTree = "<group>"; };
395B50741E6C3992009CDC8A /* Points+Hermite.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "Points+Hermite.swift"; sourceTree = "<group>"; };
Expand Down Expand Up @@ -73,6 +79,23 @@
/* End PBXFrameworksBuildPhase section */

/* Begin PBXGroup section */
3944C7BC1EBDC43700972519 /* Views */ = {
isa = PBXGroup;
children = (
3944C7BD1EBDC44100972519 /* LocalizeMeButton.swift */,
);
path = Views;
sourceTree = "<group>";
};
3944C7BF1EBDC93A00972519 /* Extensions */ = {
isa = PBXGroup;
children = (
3944C7C01EBDC94400972519 /* MKMapView+Center.swift */,
3944C7C21EBDD56B00972519 /* ViewController+LocalizeMe.swift */,
);
path = Extensions;
sourceTree = "<group>";
};
607FACC71AFB9204008FA782 = {
isa = PBXGroup;
children = (
Expand Down Expand Up @@ -105,6 +128,8 @@
607FACD91AFB9204008FA782 /* Main.storyboard */,
607FACDC1AFB9204008FA782 /* Images.xcassets */,
607FACDE1AFB9204008FA782 /* LaunchScreen.xib */,
3944C7BF1EBDC93A00972519 /* Extensions */,
3944C7BC1EBDC43700972519 /* Views */,
607FACD31AFB9204008FA782 /* Supporting Files */,
);
name = "Example for SwiftBackgroundLocation";
Expand Down Expand Up @@ -369,9 +394,12 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
3944C7BE1EBDC44100972519 /* LocalizeMeButton.swift in Sources */,
3944C7C31EBDD56B00972519 /* ViewController+LocalizeMe.swift in Sources */,
395B50771E6C3992009CDC8A /* LocationLogger.swift in Sources */,
607FACD81AFB9204008FA782 /* ViewController.swift in Sources */,
395B50761E6C3992009CDC8A /* BackgroundDebug.swift in Sources */,
3944C7C11EBDC94400972519 /* MKMapView+Center.swift in Sources */,
395B50781E6C3992009CDC8A /* Points+Hermite.swift in Sources */,
607FACD61AFB9204008FA782 /* AppDelegate.swift in Sources */,
);
Expand Down
8 changes: 7 additions & 1 deletion Example/SwiftBackgroundLocation/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="12120" systemVersion="16F60a" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES" initialViewController="BYZ-38-t0r">
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="12120" systemVersion="16A322" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES" initialViewController="BYZ-38-t0r">
<device id="retina4_7" orientation="portrait">
<adaptation id="fullscreen"/>
</device>
Expand Down Expand Up @@ -59,6 +59,11 @@
<action selector="readLog:" destination="BYZ-38-t0r" eventType="touchUpInside" id="eJ4-fm-GYy"/>
</connections>
</button>
<button opaque="NO" contentMode="scaleToFill" fixedFrame="YES" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="HA1-n5-cxZ" customClass="LocalizeMeButton" customModule="SwiftBackgroundLocation_Example" customModuleProvider="target">
<rect key="frame" x="278" y="519" width="81" height="30"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
<state key="normal" title="Localize me"/>
</button>
</subviews>
<color key="backgroundColor" red="1" green="1" blue="1" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<constraints>
Expand All @@ -79,6 +84,7 @@
</constraints>
</view>
<connections>
<outlet property="localizeMeButton" destination="HA1-n5-cxZ" id="EII-Sv-Sy8"/>
<outlet property="mapView" destination="pb1-Re-U9G" id="tLi-t4-cfZ"/>
</connections>
</viewController>
Expand Down
10 changes: 10 additions & 0 deletions Example/SwiftBackgroundLocation/Extensions/MKMapView+Center.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import MapKit

extension MKMapView {
public func centerCamera(to location: CLLocation? = nil, distance: Double = 500, pitch: CGFloat = 0, heading: Double? = 0, animated: Bool = false) {
let coordinate: CLLocationCoordinate2D = location?.coordinate ?? camera.centerCoordinate

let c = MKMapCamera(lookingAtCenter: coordinate, fromDistance: distance, pitch: pitch, heading: heading ?? 0)
setCamera(c, animated: animated)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import MapKit

extension ViewController: UIGestureRecognizerDelegate {
func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
return true
}

func didDragMap(gestureRecognizer: UIGestureRecognizer) {
if (gestureRecognizer.state == UIGestureRecognizerState.began) {
localizeMeButton.localizeMeState = .unlocalized
}
}

func setUpLocalizeMeButton() {

let mapDragRecognizer = UIPanGestureRecognizer(target: self, action: #selector(self.didDragMap(gestureRecognizer:)))
mapDragRecognizer.delegate = self
self.view.addGestureRecognizer(mapDragRecognizer)

localizeMeButton.listener = { [weak self] localizeMeState in
switch localizeMeState {
case .localized:
self?.localizeMeManager.stop()
self?.localizeMeManager.manager(for: .whenInUse, completion: { result in
if case let .Success(manager) = result {
manager.startUpdatingLocation(isHeadingEnabled: false) { [weak self] result in
if case let .Success(locationHeading) = result, let location = locationHeading.location {
self?.mapView.centerCamera(to: location)
}
}
}
})
case .unlocalized:
self?.localizeMeManager.stop()
case .navigated:
self?.localizeMeManager.stop()
self?.localizeMeManager.manager(for: .whenInUse, completion: { result in
if case let .Success(manager) = result {
manager.startUpdatingLocation(isHeadingEnabled: true) { [weak self] result in
if case let .Success(locationHeading) = result {
self?.mapView.centerCamera(to: locationHeading.location, heading: locationHeading.heading?.trueHeading)
}
}
}
})
}
}
}
}
2 changes: 0 additions & 2 deletions Example/SwiftBackgroundLocation/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,6 @@
<key>UISupportedInterfaceOrientations</key>
<array>
<string>UIInterfaceOrientationPortrait</string>
<string>UIInterfaceOrientationLandscapeLeft</string>
<string>UIInterfaceOrientationLandscapeRight</string>
</array>
</dict>
</plist>
32 changes: 13 additions & 19 deletions Example/SwiftBackgroundLocation/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,19 @@ class ViewController: UIViewController {

var circles: [MKCircle] = []

@IBOutlet weak var localizeMeButton: LocalizeMeButton!

lazy var localizeMeManager: TrackingHeadingLocationManager = {
return TrackingHeadingLocationManager()
}()

override func viewDidLoad() {
mapView.delegate = self
super.viewDidLoad()

BackgroundDebug().print()


setUpLocalizeMeButton()
}

@IBAction func start(_ sender: Any) {
Expand Down Expand Up @@ -48,7 +55,7 @@ class ViewController: UIViewController {
@IBAction func readLog(_ sender: Any) {
if let locations = logger.readLocation() {
drawLocation(locations: locations)
centerCamera(to: locations.last!)
mapView.centerCamera(to: locations.last)
}
}

Expand All @@ -74,14 +81,11 @@ class ViewController: UIViewController {
}
}
}


})

}


var isCenter = false

private func updateBackgroundLocation(location: CLLocation) {
backgroundLocations.append(location)
Expand All @@ -105,23 +109,9 @@ class ViewController: UIViewController {
}

drawLocation(locations: locations)
centerCamera(to: location)

}

private func centerCamera(to location: CLLocation) {
if !isCenter {
let camera = mapView.camera
camera.centerCoordinate = location.coordinate

mapView.camera = camera
let viewRegion = MKCoordinateRegionMakeWithDistance(location.coordinate, 500, 500)

mapView.setRegion(viewRegion, animated: true)
isCenter = true
}
}

func drawLocation(locations: [CLLocation]) {
currentPolyline = ViewController.polyline(locations: locations, title: "location")
mapView.add(currentPolyline!)
Expand Down Expand Up @@ -185,5 +175,9 @@ extension ViewController: MKMapViewDelegate {

return renderer
}


}



36 changes: 36 additions & 0 deletions Example/SwiftBackgroundLocation/Views/LocalizeMeButton.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import UIKit

class LocalizeMeButton: UIButton {
typealias LocalizeMeButtonOnTouchListener = ((LocalizeMeState) -> ())
var listener: LocalizeMeButtonOnTouchListener?

var localizeMeState: LocalizeMeState = .unlocalized {
didSet {
switch localizeMeState {
case .localized:
setTitle("Localized", for: .normal)
case .unlocalized:
setTitle("Unlocalized", for: .normal)
case .navigated:
setTitle("Navigated", for: .normal)
}

listener?(localizeMeState)
}
}

override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
switch localizeMeState {
case .localized:
localizeMeState = .navigated
case .unlocalized:
localizeMeState = .localized
case .navigated:
localizeMeState = .localized
}
}

enum LocalizeMeState {
case localized, unlocalized, navigated
}
}
2 changes: 1 addition & 1 deletion SwiftBackgroundLocation.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = 'SwiftBackgroundLocation'
s.version = '0.1.3'
s.version = '0.1.4'
s.summary = 'Efficient and Easy Location Background Monitoring in Swift'
s.description = <<-DESC
Easy Location Background Monitoring based on regions and significant location change.
Expand Down
Loading

0 comments on commit a217629

Please sign in to comment.