Permalink
Browse files

Hacked in support for 'Current Location'. Very hacky, but seems to work.

  • Loading branch information...
1 parent 339344b commit 99ab0ecf8ace76e626eba131c4a24894d7bd1512 @simonmaddox committed Sep 20, 2012
Showing with 75 additions and 7 deletions.
  1. +5 −0 GoogleTransit.xcodeproj/project.pbxproj
  2. +70 −7 GoogleTransit/AppDelegate.m
@@ -7,6 +7,7 @@
objects = {
/* Begin PBXBuildFile section */
+ F44E8EF3160B501700DED148 /* CoreLocation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = F44E8EF2160B501700DED148 /* CoreLocation.framework */; };
F4F4C2EA160B396F0055EF42 /* UIKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = F4F4C2E9160B396F0055EF42 /* UIKit.framework */; };
F4F4C2EC160B396F0055EF42 /* Foundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = F4F4C2EB160B396F0055EF42 /* Foundation.framework */; };
F4F4C2EE160B396F0055EF42 /* CoreGraphics.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = F4F4C2ED160B396F0055EF42 /* CoreGraphics.framework */; };
@@ -20,6 +21,7 @@
/* End PBXBuildFile section */
/* Begin PBXFileReference section */
+ F44E8EF2160B501700DED148 /* CoreLocation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = CoreLocation.framework; path = System/Library/Frameworks/CoreLocation.framework; sourceTree = SDKROOT; };
F4F4C2E5160B396F0055EF42 /* GoogleTransit.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = GoogleTransit.app; sourceTree = BUILT_PRODUCTS_DIR; };
F4F4C2E9160B396F0055EF42 /* UIKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = UIKit.framework; path = System/Library/Frameworks/UIKit.framework; sourceTree = SDKROOT; };
F4F4C2EB160B396F0055EF42 /* Foundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Foundation.framework; path = System/Library/Frameworks/Foundation.framework; sourceTree = SDKROOT; };
@@ -42,6 +44,7 @@
isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647;
files = (
+ F44E8EF3160B501700DED148 /* CoreLocation.framework in Frameworks */,
F4F4C308160B3DBD0055EF42 /* MapKit.framework in Frameworks */,
F4F4C2EA160B396F0055EF42 /* UIKit.framework in Frameworks */,
F4F4C2EC160B396F0055EF42 /* Foundation.framework in Frameworks */,
@@ -72,6 +75,7 @@
F4F4C2E8160B396F0055EF42 /* Frameworks */ = {
isa = PBXGroup;
children = (
+ F44E8EF2160B501700DED148 /* CoreLocation.framework */,
F4F4C307160B3DBD0055EF42 /* MapKit.framework */,
F4F4C2E9160B396F0055EF42 /* UIKit.framework */,
F4F4C2EB160B396F0055EF42 /* Foundation.framework */,
@@ -279,6 +283,7 @@
F4F4C305160B39700055EF42 /* Release */,
);
defaultConfigurationIsVisible = 0;
+ defaultConfigurationName = Release;
};
/* End XCConfigurationList section */
};
@@ -8,6 +8,17 @@
#import "AppDelegate.h"
#import <MapKit/MapKit.h>
+#import <CoreLocation/CoreLocation.h>
+
+@interface AppDelegate () <CLLocationManagerDelegate>
+
+@property (nonatomic, strong) CLLocationManager *locationManager;
+@property (nonatomic, strong) CLLocation *currentLocation;
+
+@property (nonatomic, strong) NSString *currentSource;
+@property (nonatomic, strong) NSString *currentDestination;
+
+@end
@implementation AppDelegate
@@ -16,28 +27,80 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.backgroundColor = [UIColor blackColor];
[self.window makeKeyAndVisible];
+
+ self.locationManager = [[CLLocationManager alloc] init];
+ self.locationManager.delegate = self;
+
return YES;
}
+- (void) applicationDidBecomeActive:(UIApplication *)application
+{
+ [self.locationManager startUpdatingLocation];
+}
+
+- (void) applicationWillResignActive:(UIApplication *)application
+{
+ [self.locationManager stopUpdatingLocation];
+}
+
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
if ([MKDirectionsRequest isDirectionsRequestURL:url]){
+ self.currentSource = nil;
+ self.currentDestination = nil;
+
MKDirectionsRequest *directionsRequest = [[MKDirectionsRequest alloc] initWithContentsOfURL:url];
- NSString *directionsURLString = [NSString stringWithFormat:@"https://maps.google.com/maps?f=d&source=s_d&saddr=%f,%f&daddr=%f,%f&hl=en&vps=3&jsv=432b&vpsrc=0&gl=us&dirflg=r&ttype=now&noexp=0&noal=0&sort=def&mra=atm&ie=UTF8&ui=maps_mini",
- directionsRequest.source.placemark.location.coordinate.latitude,
- directionsRequest.source.placemark.location.coordinate.longitude,
- directionsRequest.destination.placemark.location.coordinate.latitude,
- directionsRequest.destination.placemark.location.coordinate.longitude];
+ if (!directionsRequest.source.isCurrentLocation){
+ self.currentSource = [NSString stringWithFormat:@"%f,%f", directionsRequest.source.placemark.location.coordinate.latitude,
+ directionsRequest.source.placemark.location.coordinate.longitude];
+ } else if (self.currentLocation){
+ self.currentSource = [NSString stringWithFormat:@"%f,%f", self.currentLocation.coordinate.latitude, self.currentLocation.coordinate.longitude];
+ }
- NSURL *directionsURL = [NSURL URLWithString:directionsURLString];
+ if (!directionsRequest.destination.isCurrentLocation){
+ self.currentDestination = [NSString stringWithFormat:@"%f,%f", directionsRequest.destination.placemark.location.coordinate.latitude,
+ directionsRequest.destination.placemark.location.coordinate.longitude];
+ } else if (self.currentLocation){
+ self.currentDestination = [NSString stringWithFormat:@"%f,%f", self.currentLocation.coordinate.latitude, self.currentLocation.coordinate.longitude];
+ }
- [[UIApplication sharedApplication] openURL:directionsURL];
+ if (self.currentSource && self.currentDestination){
+ [self openTransitDirections];
+ }
return YES;
}
return NO;
}
+- (void) openTransitDirections
+{
+ if (self.currentSource && self.currentDestination){
+ NSString *directionsURLString = [NSString stringWithFormat:@"https://maps.google.com/maps?f=d&source=s_d&saddr=%@&daddr=%@&hl=en&vps=3&jsv=432b&vpsrc=0&gl=us&dirflg=r&ttype=now&noexp=0&noal=0&sort=def&mra=atm&ie=UTF8&ui=maps_mini",
+ self.currentSource, self.currentDestination];
+
+ NSURL *directionsURL = [NSURL URLWithString:directionsURLString];
+
+ self.currentSource = nil;
+ self.currentDestination = nil;
+
+ [self openURL:directionsURL];
+ }
+}
+
+- (void) openURL:(NSURL *)url
+{
+ [[UIApplication sharedApplication] openURL:url];
+}
+
+- (void)locationManager:(CLLocationManager *)manager
+ didUpdateLocations:(NSArray *)locations
+{
+ self.currentLocation = [locations lastObject];
+ [self openTransitDirections];
+}
+
@end

0 comments on commit 99ab0ec

Please sign in to comment.