Search code examples
iosobjective-cmkmapviewcllocationmanager

How to find current location of user in mkmapview


i want to find the current location of the user below the code for finding coordinates from the well defined adress hom can i find the current location of the user ..please help me

.h file #import #import

@interface ViewController : UIViewController<MKMapViewDelegate,CLLocationManagerDelegate>
@property(nonatomic,retain)  MKMapView *myMapView;
@property (nonatomic, strong) CLGeocoder *myGeocoder;
@end

.m file

#import "ViewController.h"
#import <CoreLocation/CoreLocation.h>

@interface ViewController ()
{
    CLLocationManager *locationManager;
}

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];


    self.myMapView.showsUserLocation=YES;

    self.view.backgroundColor=[UIColor whiteColor];
    self.myMapView=[[MKMapView alloc]initWithFrame:self.view.bounds];

    self.myMapView.mapType=MKMapTypeHybrid;
    self.myMapView.autoresizingMask=UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
    [self.view addSubview:self.myMapView];


    locationManager = [[CLLocationManager alloc] init];
    locationManager.delegate=self;
    locationManager.desiredAccuracy=kCLLocationAccuracyBest;
    locationManager.distanceFilter=kCLDistanceFilterNone;

    [locationManager startUpdatingLocation];

   NSString *oreillyAddress =@"india";
  self.myGeocoder = [[CLGeocoder alloc] init];
    [self.myGeocoder
     geocodeAddressString:oreillyAddress completionHandler:^(NSArray *placemarks, NSError *error)
   {
        if ([placemarks count] > 0 && error == nil)
            {
       NSLog(@"Found %lu placemark(s).", (unsigned long)[placemarks count]);
             CLPlacemark *firstPlacemark = [placemarks objectAtIndex:0]; NSLog(@"Longitude = %f", firstPlacemark.location.coordinate.longitude); NSLog(@"Latitude = %f", firstPlacemark.location.coordinate.latitude);


         }
         else if ([placemarks count] == 0 &&
                  error == nil){ NSLog(@"Found no placemarks.");
        }
         else if (error != nil){
             NSLog(@"An error occurred = %@", error); }
     }];


    // Do any additional setup after loading the view, typically from a nib.
}
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
    return YES;
}
- (void)locationManager:(CLLocationManager *)manager
    didUpdateToLocation:(CLLocation *)newLocation
           fromLocation:(CLLocation *)oldLocation
{
    float latitude=newLocation.coordinate.latitude;
    float longitude=newLocation.coordinate.longitude;
    NSLog(@"%f",latitude);
    NSLog(@"%f",longitude);
    [locationManager stopUpdatingLocation];

}

- (void)locationManager:(CLLocationManager *)manager
       didFailWithError:(NSError *)error
{
    NSLog(@"error==>%@",error);
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

Solution

  • Use this

    self.myMapView.showsUserLocation = YES;