Skip to content

Commit

Permalink
Add model object
Browse files Browse the repository at this point in the history
  • Loading branch information
lazerwalker committed Aug 20, 2016
1 parent 83fd5d8 commit ebbbb5e
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 8 deletions.
4 changes: 4 additions & 0 deletions GeoFencer.xcodeproj/project.pbxproj
Expand Up @@ -14,6 +14,7 @@
C69984CC1D68CA7F004F761B /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = C69984CA1D68CA7F004F761B /* LaunchScreen.storyboard */; };
C69984D41D68CB33004F761B /* MapKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = C69984D31D68CB33004F761B /* MapKit.framework */; };
C69984D61D68CBA3004F761B /* CoreLocation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = C69984D51D68CBA3004F761B /* CoreLocation.framework */; };
C69984D81D68F484004F761B /* Geofence.swift in Sources */ = {isa = PBXBuildFile; fileRef = C69984D71D68F483004F761B /* Geofence.swift */; };
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
Expand All @@ -26,6 +27,7 @@
C69984CD1D68CA7F004F761B /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
C69984D31D68CB33004F761B /* MapKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = MapKit.framework; path = System/Library/Frameworks/MapKit.framework; sourceTree = SDKROOT; };
C69984D51D68CBA3004F761B /* CoreLocation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = CoreLocation.framework; path = System/Library/Frameworks/CoreLocation.framework; sourceTree = SDKROOT; };
C69984D71D68F483004F761B /* Geofence.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Geofence.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -68,6 +70,7 @@
C69984C81D68CA7F004F761B /* Assets.xcassets */,
C69984CA1D68CA7F004F761B /* LaunchScreen.storyboard */,
C69984CD1D68CA7F004F761B /* Info.plist */,
C69984D71D68F483004F761B /* Geofence.swift */,
);
path = GeoFencer;
sourceTree = "<group>";
Expand Down Expand Up @@ -145,6 +148,7 @@
files = (
C69984C41D68CA7F004F761B /* ViewController.swift in Sources */,
C69984C21D68CA7F004F761B /* AppDelegate.swift in Sources */,
C69984D81D68F484004F761B /* Geofence.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
25 changes: 25 additions & 0 deletions GeoFencer/Geofence.swift
@@ -0,0 +1,25 @@
import Foundation
import MapKit

class Geofence {
let points:[CLLocationCoordinate2D]
let circle: MKCircle

init (points:[CLLocationCoordinate2D]) {
self.points = points

// TODO: This assumes only 2 points
circle = Geofence.circleFromPoints(points.first!, points.last!)
}

class func circleFromPoints(first:CLLocationCoordinate2D, _ second:CLLocationCoordinate2D) -> MKCircle {
let p1 = MKMapPointForCoordinate(first)
let p2 = MKMapPointForCoordinate(second)

let mapRect = MKMapRectMake(fmin(p1.x,p2.x),
fmin(p1.y,p2.y),
fabs(p1.x-p2.x),
fabs(p1.y-p2.y))
return MKCircle(mapRect:mapRect)
}
}
9 changes: 1 addition & 8 deletions GeoFencer/ViewController.swift
Expand Up @@ -61,14 +61,7 @@ class ViewController: UIViewController, MKMapViewDelegate {
mapView.removeAnnotation(circle)
}

let p1 = MKMapPointForCoordinate(first.coordinate)
let p2 = MKMapPointForCoordinate(second.coordinate)

let mapRect = MKMapRectMake(fmin(p1.x,p2.x),
fmin(p1.y,p2.y),
fabs(p1.x-p2.x),
fabs(p1.y-p2.y))
circle = MKCircle(mapRect: mapRect)
circle = Geofence.circleFromPoints(first.coordinate, second.coordinate)

if let circle = circle {
circle.title = "My Geofence"
Expand Down

0 comments on commit ebbbb5e

Please sign in to comment.