GET CURREN LOCATION ON MAP
/// Using CoreLocation.framework
File..h
---------
#import <CoreLocation/CoreLocation.h>
@interface ReceiveJobView : UIViewController<CLLocationManagerDelegate>
{
CLLocationManager *locationManager;
}
@end
-------------
File..m
-------------
#import <CoreLocation/CoreLocation.h>
- (void)viewDidLoad
{
UIToolbar* numberToolbar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 50)];
numberToolbar.barStyle = UIBarStyleBlackTranslucent;
numberToolbar.items = [NSArray arrayWithObjects:
[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil],
[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil],
[[UIBarButtonItem alloc]initWithTitle:@"Done" style:UIBarButtonItemStyleDone target:self action:@selector(doneWithNumberPad)],nil];
[numberToolbar sizeToFit];
txtCurrentLocation.inputAccessoryView = numberToolbar;
// txtCurrentLocation.text=[self getAddressFromLatLon:22.2721 withLongitude:72.265];
/////
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
locationManager.distanceFilter = kCLDistanceFilterNone;
[locationManager startUpdatingLocation];
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
if (!newLocation) return;
File..m
-------------
#import <CoreLocation/CoreLocation.h>
- (void)viewDidLoad
{
UIToolbar* numberToolbar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 50)];
numberToolbar.barStyle = UIBarStyleBlackTranslucent;
numberToolbar.items = [NSArray arrayWithObjects:
[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil],
[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil],
[[UIBarButtonItem alloc]initWithTitle:@"Done" style:UIBarButtonItemStyleDone target:self action:@selector(doneWithNumberPad)],nil];
[numberToolbar sizeToFit];
txtCurrentLocation.inputAccessoryView = numberToolbar;
// txtCurrentLocation.text=[self getAddressFromLatLon:22.2721 withLongitude:72.265];
/////
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
locationManager.distanceFilter = kCLDistanceFilterNone;
[locationManager startUpdatingLocation];
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
if (!newLocation) return;
if ((oldLocation.coordinate.latitude != newLocation.coordinate.latitude) && (oldLocation.coordinate.longitude != newLocation.coordinate.longitude))
{
CLLocation *loc2 = [[CLLocation alloc] initWithLatitude:newLocation.coordinate.latitude longitude:newLocation.coordinate.longitude];
// NSLog(@"current latitude :%f",loc2.coordinate.latitude);
CLGeocoder *ceo = [[CLGeocoder alloc]init];
CLLocation *loc = [[CLLocation alloc]initWithLatitude:loc2.coordinate.latitude longitude:loc2.coordinate.longitude];
[ceo reverseGeocodeLocation: loc completionHandler:
^(NSArray *placemarks, NSError *error) {
CLPlacemark *placemark = [placemarks objectAtIndex:0];
NSLog(@"placemark%@",placemark);
// String to hold address
NSString *locatedAt = [[placemark.addressDictionary valueForKey:@"FormattedAddressLines"] componentsJoinedByString:@","];
NSLog(@"addressDictionary%@", placemark.addressDictionary);
NSLog(@"placemark%@",placemark.region);
NSLog(@"placemark%@",placemark.country);// Give Country Name
NSLog(@"placemark%@",placemark.locality);// Extract the city name
NSLog(@"location%@",placemark.name);
NSLog(@"location%@",placemark.ocean);
NSLog(@"location%@",placemark.postalCode);
NSLog(@"location%@",placemark.subLocality);
NSLog(@"location%@",placemark.location);
// Print the location to console
NSLog(@"I am currently at %@",locatedAt);
txtCurrentLocation.text=locatedAt;
}];
[locationManager stopUpdatingLocation];
}
}
Comments
Post a Comment
Thank You.