Thursday, November 22, 2012

How to show user current city name

Keyword: ios6, Core Location frame work, iphone,sample project, CLLocationManagerDelegate, CLLocationManager, startUpdatingLocation, setDesiredAccuracy,map

Get contacts of all your emergency needs on single tap. Download Quick-Finder for iPhone


How to show user current city name



1) Create a new Project with name of CityFinder and 
2) add FrameWork CoreLocation.FrameWork


3) Update the code of AppDelegate.h as follows ( Modified code is in different color - yellow )


#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>

@class ViewController;

@interface AppDelegate : UIResponder <UIApplicationDelegate,CLLocationManagerDelegate>

@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) ViewController *viewController;
@property (nonatomic,retain) CLLocationManager *manager;
@property float latitude;
@property float longitude;




4) Synthesize all variables

@synthesize manager;
@synthesize latitude;
@synthesize longitude;








5) Update the code of method didFinishLaunching in AppDelegate.m as follows

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
    // Override point for customization after application launch.
    self.viewController = [[[ViewController alloc] initWithNibName:@"ViewController" bundle:nil]   autorelease];
    self.window.rootViewController = self.viewController;
    
    self.manager=[[CLLocationManager alloc]init];
    [self.manager setDistanceFilter:CLLocationDistanceMax];
    [self.manager setDelegate:self];
    [self.manager setDesiredAccuracy:kCLLocationAccuracyBest];
    [self.manager startUpdatingLocation];
    [self.window makeKeyAndVisible];

    return YES;
}




6) Update xib of ViewController

   i) add a text view named txtView;
  ii) add an button and also an IBAction with it named  - (IBAction)clickToGetLocation:(id)sender


7) update the code of ViewController.h
   

    #import <UIKit/UIKit.h>

    @interface ViewController : UIViewController

    @property (retain, nonatomic) IBOutlet UITextView *txtView;
    - (IBAction)clickToGetLocation:(id)sender;
   @end

  

8) Code of click action 


- (IBAction)clickToGetLocation:(id)sender {
    
    AppDelegate *delegate=(AppDelegate *)[[UIApplication sharedApplication]delegate];
    
    NSString *urlString = [NSString stringWithFormat:@"http://maps.google.com/maps/geo?q=%f,%f&output=csv",delegate.latitude, delegate.longitude];
    NSError* error;
    NSString *locationString = [NSString stringWithContentsOfURL:[NSURL URLWithString:urlString] encoding:NSASCIIStringEncoding error:&error];
    locationString = [locationString stringByReplacingOccurrencesOfString:@"\"" withString:@""];
    self.txtView.text=[locationString substringFromIndex:6];
}






Now thats it 

Click on button. You will get current location on text View.



Note:   If you are running this code on simulator you will get address  "1 stockton st,francisco, ca 94108 usa"  default location of simulator . You need to run this code on real device to get the current location.




You can also download the sample code from here


  http://adf.ly/F9ECK


If you have any question, please put it in comment section.



No comments:

Post a Comment