Skip to content

Instantly share code, notes, and snippets.

@swarut
Created November 12, 2012 13:56
Show Gist options
  • Save swarut/4059530 to your computer and use it in GitHub Desktop.
Save swarut/4059530 to your computer and use it in GitHub Desktop.
Objective C : Map Example
//
// LocationViewController.m
// BoxBox
//
// Created by Warut Surapat on 12/19/11.
// Copyright (c) 2011 StardustDream. All rights reserved.
//
#import "LocationViewController.h"
#import "GlobalData.h"
#import "URLHelper.h"
#import "MerchantLocation.h"
#import "NotifierHelper.h"
#import "Logger.h"
#import "SVProgressHUD.h"
#define kBgQueue dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)
#define METERS_PER_MILE 1609.344
@implementation LocationViewController{
CLLocationCoordinate2D currentLocation;
}
@synthesize map;
@synthesize locationManager;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
#pragma mark - View lifecycle
/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView
{
}
*/
- (void)viewDidUnload
{
[self setMap:nil];
[super viewDidUnload];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
- (void)viewDidLoad
{
[Logger logAll:@"LocationView:::Enter view"];
[super viewDidLoad];
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
[SVProgressHUD showWithStatus:[GlobalData applicationTextForKey:@"BasicLoading"]];
[self updateData];
}
- (void)viewWillAppear:(BOOL)animated {
}
- (void) updateData{
dispatch_async(kBgQueue, ^{
NSString *target_url_string = [URLHelper URLStringWithBaseURL:[GlobalData locationURL]
withSuffix:[NSArray arrayWithObjects:[GlobalData userId], nil]
withParameters:[NSArray arrayWithObjects:
[NSString stringWithFormat:@"ApplicationKey=%@",[GlobalData applicationKey]],
[NSString stringWithFormat:@"AuthenticationKey=%@",[GlobalData authenticationKey]],
[NSString stringWithFormat:@"Radius=%d",10],
//[NSString stringWithFormat:@"Latitude=%f", currentLocation.latitude],
//[NSString stringWithFormat:@"Longtitude=%f", currentLocation.longitude],
[NSString stringWithFormat:@"Latitude=%f",13.730313],
[NSString stringWithFormat:@"Longitude=%f",100.494937],
nil]];
NSURL *target_url = [NSURL URLWithString:target_url_string];
NSData *data = [NSData dataWithContentsOfURL:target_url];
[self performSelectorInBackground:@selector(plotMap:) withObject:data];
// [self performSelectorOnMainThread:@selector(plotMap:)
// withObject:data waitUntilDone:YES];
});
}
- (void) plotMap:(NSData *)respondedData{
if(respondedData != nil){
NSError *error;
NSDictionary *data_dic = [NSJSONSerialization
JSONObjectWithData:respondedData
options:kNilOptions
error:&error];
NSNumber *returned_code = [data_dic objectForKey:@"StatusCode"];
if([@"200" isEqualToString:[returned_code stringValue]]){
NSArray *data = [data_dic objectForKey:@"Items"];
for (NSDictionary * item in data){
NSDictionary *location = [item objectForKey:@"Location"];
NSNumber *latitude = [location objectForKey:@"Latitude"];
NSNumber *longitude = [location objectForKey:@"Longitude"];
NSString *name = [NSString stringWithFormat:@"%@\n%@",
[item objectForKey:@"MerchantName"],
[item objectForKey:@"BranchName"]];
CLLocationCoordinate2D coordinate;
coordinate.latitude = latitude.doubleValue;
coordinate.longitude = longitude.doubleValue;
MerchantLocation *annotation = [[MerchantLocation alloc] initWithName:name coordinate:coordinate];
[map addAnnotation:annotation];
[Logger logAll:@"LocationView:::Finished loading data"];
[locationManager startUpdatingLocation];
[SVProgressHUD dismiss];
}
}
else{
[SVProgressHUD dismiss];
[NotifierHelper showBasicError:self];
[Logger logAll:@"LocationView:::Failed to load data"];
}
}
else{
[SVProgressHUD dismiss];
[NotifierHelper showBasicError:self];
[Logger logAll:@"LocationView:::Failed to load data"];
}
}
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation {
static NSString *identifier = @"MyLocation";
if ([annotation isKindOfClass:[MerchantLocation class]]) {
MKPinAnnotationView *annotationView = (MKPinAnnotationView *) [map dequeueReusableAnnotationViewWithIdentifier:@"PinMerchantLocation"];
if (annotationView == nil) {
annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier];
} else {
annotationView.annotation = annotation;
}
annotationView.enabled = YES;
annotationView.canShowCallout = YES;
annotationView.image=[UIImage imageNamed:@"map_location.png"];//here we use a nice image instead of the default pins
return annotationView;
}
return nil;
}
- (IBAction)refresh:(id)sender {
[Logger logAll:@"LocationView:::Refresh data"];
[self updateData];
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation{
//map.showsUserLocation=TRUE;
// zoom to a specific area
// currentLocation.latitude = newLocation.coordinate.latitude;
// currentLocation.longitude = newLocation.coordinate.longitude;
// map.delegate=self;
//[self zoomToCurrentLocation];
CLLocationCoordinate2D zoomLocation;
zoomLocation.latitude = map.userLocation.coordinate.latitude;//13.730313;
zoomLocation.longitude= map.userLocation.coordinate.longitude;//100.494937;
MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(zoomLocation, 3.0*METERS_PER_MILE, 3.0*METERS_PER_MILE);
MKCoordinateRegion adjustedRegion = [map regionThatFits:viewRegion];
[map setRegion:adjustedRegion animated:YES];
[locationManager stopUpdatingLocation];
}
- (void) zoomToCurrentLocation{
MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(
currentLocation, 40*METERS_PER_MILE, 40*METERS_PER_MILE);
MKCoordinateRegion adjustedRegion = [map regionThatFits:viewRegion];
map.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
[map setRegion:adjustedRegion animated:YES];
// MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(currentLocation, 50000, 50000);
// [map setRegion:region animated:YES];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment