Pages

Showing posts with label CLLocation. Show all posts
Showing posts with label CLLocation. Show all posts

Saturday, May 14, 2011

How to find city name and other detail from lat/long


Here i m detailing, how to get detail information from lat/long.

for .h file.
import framework
implement MKMapViewDelegate and MKReverseGeocoderDelegate

Declare MKReverseGeocoder *reverseGeocoder;
@property (nonatomic, retain) MKReverseGeocoder *reverseGeocoder;

for .m file.
@synthesize reverseGeocoder;
    CLLocationCoordinate2D coord2d;
        coord2d.latitude =19.3;
        coord2d.longitude =73.9 ;
    self.reverseGeocoder =[[[MKReverseGeocoder alloc] initWithCoordinate:coord2d] autorelease];
 
    Note: If we get data detail for Current location then we can use below statement:
        [[[MKReverseGeocoder alloc] initWithCoordinate:mapView.userLocation.location.coordinate] autorelease];

    reverseGeocoder.delegate = self;
    [reverseGeocoder start];



- (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFailWithError:(NSError *)error
{
    NSString *errorMessage = [error localizedDescription];
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Cannot obtain address."
                                                                                                 message:errorMessage
                                                                                            delegate:nil
                                                                             cancelButtonTitle:@"OK"
                                                                             otherButtonTitles:nil];
    [alertView show];
    [alertView release];
}

- (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFindPlacemark:(MKPlacemark *)placemark
{
     NSLog(@"Thoroughfare : %@",placemark.thoroughfare);
     NSLog(@"Sub-thoroughfare : %@",placemark.subThoroughfare);
     NSLog(@"Locality : %@",placemark.locality);
     NSLog(@"Sub-locality : %@",placemark.subLocality);
     NSLog(@"Administrative Area : %@",placemark.administrativeArea);
     NSLog(@"Sub-administrative Area : %@",placemark.subAdministrativeArea);
     NSLog(@"Postal Code : %@",placemark.postalCode);
     NSLog(@"Country : %@",placemark.country);
     NSLog(@"Country Code: %@",placemark.countryCode);

}

Saturday, April 16, 2011

Find Current lat/lon with Iphone sdk:

In .h file:
#import
CLLocationManager *locationManager;
NSString *lat;
NSString *lon;
@property (nonatomic,retain) NSString *lat;
@property (nonatomic,retain) NSString *lon;
In .m file:
@synthesize lat,lon;
// Get Current Lat and lon
[self getCurrentLatLon];
-(void) getCurrentLatLon
{
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;

locationManager.distanceFilter = kCLDistanceFilterNone; // whenever we move
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[locationManager startUpdatingLocation];

}
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation
{

self.lat = [NSString stringWithFormat:@"%f",newLocation.coordinate.latitude];
self.lon = [NSString stringWithFormat:@"%f",newLocation.coordinate.longitude];
}



Happy programming..............