Skip to content

Instantly share code, notes, and snippets.

@ChrisRisner
Created September 14, 2012 05:48
Show Gist options
  • Save ChrisRisner/3720038 to your computer and use it in GitHub Desktop.
Save ChrisRisner/3720038 to your computer and use it in GitHub Desktop.
GeoDemo-ios1
#import <Foundation/Foundation.h>
@interface Constants : NSObject
#define kBgQueue dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)
extern NSString *kGetPOIUrl;
@end
#import "Constants.h"
@implementation Constants
NSString *kGetPOIUrl = @"http://yoursubdomain.azurewebsites.net/api/Location/FindPointsOfInterestWithinRadius";
@end
#import <Foundation/Foundation.h>
#import "CoreLocation/CoreLocation.h"
@protocol CoreLocationControllerDelegate
@required
- (void)locationUpdate:(CLLocation *)location; // Our location updates are sent here
- (void)locationError:(NSError *)error; // Any errors are sent here
@end
@interface CoreLocationController : NSObject <CLLocationManagerDelegate> {
CLLocationManager *locMgr;
id delegate;
}
@property (nonatomic, retain) CLLocationManager *locMgr;
@property (nonatomic, retain) id delegate;
@end
#import "CoreLocationController.h"
#import "CoreLocation/CoreLocation.h"
@implementation CoreLocationController
@synthesize locMgr, delegate;
- (id)init {
self = [super init];
if(self != nil) {
self.locMgr = [[CLLocationManager alloc] init]; // Create new instance of locMgr
self.locMgr.delegate = self; // Set the delegate as self.
}
return self;
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
if([self.delegate conformsToProtocol:@protocol(CoreLocationControllerDelegate)]) {
// Check if the class assigning itself as the delegate conforms to our protocol. If not, the message will go nowhere. Not good.
[self.delegate locationUpdate:newLocation];
}
}
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {
if([self.delegate conformsToProtocol:@protocol(CoreLocationControllerDelegate)]) {
// Check if the class assigning itself as the delegate conforms to our protocol. If not, the message will go nowhere. Not good.
[self.delegate locationError:error];
}
}
- (void)dealloc {
}
@end
#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>
@interface MapAnnotation : NSObject <MKAnnotation> {
CLLocationCoordinate2D _coordinate;
}
- (id)initWithCoordinate:(CLLocationCoordinate2D)coordinate;
@end
#import "MapAnnotation.h"
@implementation MapAnnotation
@synthesize coordinate = _coordinate;
- (id)initWithCoordinate:(CLLocationCoordinate2D)coordinate
{
self = [super init];
if (self != nil)
{
_coordinate = coordinate;
}
return self;
}
@end
#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
@interface ViewController : UIViewController
@property (weak, nonatomic) IBOutlet UILabel *labelLatitude;
@property (weak, nonatomic) IBOutlet UILabel *labelLongitude;
@property (weak, nonatomic) IBOutlet MKMapView *mapView;
- (IBAction)tapRefresh:(id)sender;
@end
#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
#import "CoreLocationController.h"
@interface ViewController : UIViewController<CoreLocationControllerDelegate, MKMapViewDelegate> {
CoreLocationController *CLController;
CLLocation * currentLocation;
}
@property (weak, nonatomic) IBOutlet UILabel *labelLatitude;
@property (weak, nonatomic) IBOutlet UILabel *labelLongitude;
@property (weak, nonatomic) IBOutlet MKMapView *mapView;
- (IBAction)tapRefresh:(id)sender;
@end
- (void)locationUpdate:(CLLocation *)location {
//Set the private currentLocation field to be the location passed in
currentLocation = location;
labelLatitude.text= [NSString stringWithFormat:@"Latitude: %f", [location coordinate].latitude];
labelLongitude.text = [NSString stringWithFormat:@"Longitude: %f", [location coordinate].longitude];
//Save our long and latitude into defaults for adding a POI later
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
[prefs setObject:[NSString stringWithFormat:@"%f", [location coordinate].latitude] forKey:@"Latitude"];
[prefs setObject:[NSString stringWithFormat:@"%f", [location coordinate].longitude] forKey:@"Longitude"];
[prefs synchronize];
//Center on our location
CLLocationCoordinate2D mapCenter = mapView.centerCoordinate;
mapCenter = [location coordinate];
[mapView setCenterCoordinate:mapCenter animated:YES];
MKCoordinateRegion theRegion = mapView.region;
//Here we're only rechecking for POIs if the lat / long delta is a significant amount
if (theRegion.span.longitudeDelta > 2 ||
theRegion.span.latitudeDelta > 2) {
//Decreasing the scale will zoom the map in farther
//Increasing will zoom out
double scale = .01;
theRegion.span.longitudeDelta = scale;
theRegion.span.latitudeDelta = scale;
[mapView setRegion:theRegion animated:YES];
[self getCurrentPointsOfInterest];
}
}
- (void)viewDidLoad
{
[super viewDidLoad];
//Start getting the current location
CLController = [[CoreLocationController alloc] init];
CLController.delegate = self;
[CLController.locMgr startUpdatingLocation];
//Set the map view to show the current location and use self for updates
mapView.showsUserLocation = YES;
[mapView setDelegate:self];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment