Skip to content

Instantly share code, notes, and snippets.

@ohayon
Created March 23, 2013 20:27
Show Gist options
  • Save ohayon/5229232 to your computer and use it in GitHub Desktop.
Save ohayon/5229232 to your computer and use it in GitHub Desktop.
#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
#import <CoreLocation/CoreLocation.h>
#import "AFJSONRequestOperation.h"
#import <SSToolkit/SSToolkit.h>
@interface DOViewController : UIViewController<CLLocationManagerDelegate, UITableViewDelegate, UITableViewDataSource>{
CLLocationManager* _locationManager;
}
@property (strong, nonatomic) IBOutlet UITableView *tableView;
@property (weak, nonatomic) IBOutlet MKMapView *mapView;
@property (strong, nonatomic) AFJSONRequestOperation *foursquareJSONRequestOperation;
@property (strong, nonatomic) NSURLRequest *foursquareRequest;
@property (strong, nonatomic) SSHUDView *progressHUD;
@property (strong, nonatomic) NSArray *foursquareVenues;
- (IBAction)refreshButton:(UIBarButtonItem *)sender;
@end
#import "DOViewController.h"
#import "DOAnnotation.h"
@interface DOViewController ()
@end
@implementation DOViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.title = @"Nearby";
_tableView.tableHeaderView = _mapView;
_locationManager = [[CLLocationManager alloc]init];
_locationManager.desiredAccuracy = kCLLocationAccuracyBest;
_locationManager.delegate = self;
[_locationManager startUpdatingLocation];
}
- (NSURL*)findLocalRestaurantsURL:(NSString*)currentLocation{
NSString *baseURL = @"https://api.foursquare.com/v2/venues/explore?";
NSString *queryParams = @"section=food&intent=browse&ll=";
NSString *clientInfo = @"&client_id=XXXXX&client_secret=XXXX&v=20130323";
NSString *url = [NSString stringWithFormat:@"%@%@%@%@", baseURL, queryParams, currentLocation, clientInfo];
// NSLog(@"%@", [NSURL URLWithString:url]);
return [NSURL URLWithString:url];
}
- (void)makeJSONRequestForLocation:(NSString*)location{
_progressHUD = [[SSHUDView alloc] initWithTitle:@"loading" loading:YES];
[_progressHUD show];
NSURL *url = [self findLocalRestaurantsURL:location];
_foursquareRequest = [NSURLRequest requestWithURL:url];
_foursquareJSONRequestOperation = [AFJSONRequestOperation JSONRequestOperationWithRequest:_foursquareRequest success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON){
NSDictionary *resp = [NSDictionary dictionaryWithDictionary:JSON[@"response"]];
NSArray *groups = resp[@"groups"];
_foursquareVenues = groups[0][@"items"];
[self plotVenuesOnMap:_foursquareVenues];
[_progressHUD dismissAnimated:YES];
} failure:nil];
[_foursquareJSONRequestOperation start];
}
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{
CLLocation *newLocation = [locations lastObject];
[self setupMapforLocation:newLocation];
[_locationManager stopUpdatingLocation];
NSString *location = [NSString stringWithFormat:@"%f,%f", newLocation.coordinate.latitude, newLocation.coordinate.longitude];
[self makeJSONRequestForLocation:location];
// [self getVenuesForLocation:newLocation];
}
-(void)setupMapforLocation:(CLLocation*)newLocation{
NSLog(@"SETUP MAP FOR LOCATION");
MKCoordinateRegion region;
MKCoordinateSpan span;
span.latitudeDelta = 0.003;
span.longitudeDelta = 0.003;
CLLocationCoordinate2D location;
location.latitude = newLocation.coordinate.latitude;
location.longitude = newLocation.coordinate.longitude;
region.span = span;
region.center = location;
[_mapView setRegion:region animated:YES];
}
- (void)plotVenuesOnMap:(NSArray*)venues{
for (id<MKAnnotation> annotation in _mapView.annotations){
[_mapView removeAnnotation:annotation];
}
for (NSDictionary *thisDict in venues){
NSDictionary *thisVenue = thisDict[@"venue"];
NSString *name = thisVenue[@"name"];
NSNumber *latitude = thisVenue[@"location"][@"lat"];
NSNumber *longitude = thisVenue[@"location"][@"lng"];
NSString *address = thisVenue[@"location"][@"address"];
CLLocationCoordinate2D coordinate;
coordinate.latitude = latitude.doubleValue;
coordinate.longitude = longitude.doubleValue;
DOAnnotation *annotation = [[DOAnnotation alloc] initWithName:name address:address coordinate:coordinate];
[_mapView addAnnotation:annotation];
}
}
- (IBAction)refreshButton:(UIBarButtonItem *)sender {
NSLog(@"TAPPED!");
CLLocation *currentLocation = _locationManager.location;
[self setupMapforLocation:currentLocation];
[_locationManager stopUpdatingLocation];
NSString *location = [NSString stringWithFormat:@"%f,%f", currentLocation.coordinate.latitude, currentLocation.coordinate.longitude];
[self makeJSONRequestForLocation:location];
}
#pragma mark - Table view data source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return _foursquareVenues.count;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
if (_foursquareVenues.count) {
return 1;
}
return 0;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"IN TABLEVIEW");
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
cell.textLabel.font = [UIFont boldSystemFontOfSize:12];
cell.detailTextLabel.font = [UIFont systemFontOfSize:12];
cell.detailTextLabel.textColor = [UIColor lightGrayColor];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
cell.textLabel.text = _foursquareVenues[indexPath.row][@"venue"][@"name"];
return cell;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment