preciouslogic
preciouslogic

Reputation: 65

How to keep update user Location blue point using ios MapKit

I checked many links and googled it a lot. I tried those codes too but didn't succeeded so at last i am posting it here.

can any one help me regarding user location update.

I tried few code in which people are updating the map in location manager update delegate. But it's not working for me.It just show the blue point on user current location but it's not keep updating if i move.

So, can any one guide me from start that what should i do. How to show the User current location keep update on map when ever or where ever user move.

Upvotes: 0

Views: 2920

Answers (2)

Tony
Tony

Reputation: 4591

You can do it by yourself without using showUserLocation

You should use CLLocationManager to get current location and add it into mapView

Implement MKAnnotation

@interface MyAnnotation : NSObject<MKAnnotation>{
    CLLocationCoordinate2D _coordinate;
    NSString *_title;
    NSString *_subtitle;
}
- (id)initWithCoordinate:(CLLocationCoordinate2D)coordinate title:(NSString *)title subtitle:(NSString *)subtitle;
@end
@implementation MyAnnotation
- (NSString *)title {return _title;}
- (NSString *)subtitle {return _subtitle;}
- (CLLocationCoordinate2D)coordinate {return _coordinate;}
- (void)setCoordinate:(CLLocationCoordinate2D)newCoordinate {_coordinate = newCoordinate;}
- (id)initWithCoordinate:(CLLocationCoordinate2D)coordinate title:(NSString *)title subtitle:(NSString *)subtitle {
    if (self = [super init]) {
        _title = title.copy;
        _subtitle = subtitle.copy;
        _coordinate = coordinate;
    }
    return self;
}
@end

Create 1 UIViewController

@interface MyViewController () <CLLocationManagerDelegate>{
    CLLocationManager *locationManager;
    MyAnnotation *userAnnotation;
}
@property (strong, nonatomic) MKMapView *mapView;
@end

@implementation MyViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.mapView = [[MKMapView alloc] initWithFrame:self.view.bounds];
    self.mapView.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;
    [self.view addSubview:self.mapView];

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

#pragma mark - DELEGATE
//for iOS 5
- (void)locationManager:(CLLocationManager *)manager
    didUpdateToLocation:(CLLocation *)newLocation
           fromLocation:(CLLocation *)oldLocation {
    //    [manager stopUpdatingLocation];
    if (newLocation) {
        if (!userAnnotation) {
            userAnnotation = [[MyAnnotation alloc]
                              initWithCoordinate:newLocation.coordinate
                              title:@"I'm here"
                              subtitle:nil];
            [self.mapView addAnnotation:userAnnotation];
        } else {
            [self.mapView removeAnnotation:userAnnotation];
            [userAnnotation setCoordinate:newLocation.coordinate];
            [self.mapView addAnnotation:userAnnotation];
        }
    }
}
//for iOS 6
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
    CLLocation *loc = locations.lastObject;
    [self locationManager:manager didUpdateToLocation:loc fromLocation:nil];
}
- (void)locationManager:(CLLocationManager *)manager
       didFailWithError:(NSError *)error {
    //open GPS services please
}
- (void)dealloc {
    [locationManager stopUpdatingLocation];
}
@end

Note:

  • Remember remove old annotation and add it again when receiving new location
  • Please use iOS 5 and ARC

Upvotes: 1

user2223516
user2223516

Reputation:

please try using this method

  1. Don't forget to add CLLocationManagerDelegate and MKMapviewDelegate in your header File.

  2. first implement the below line into your reload map method or viewDidload or viewWillappear method:

     [mapView setShowsUserLocation:YES]
    
  3. Than change the below methods like this:

    (MKAnnotationView *) mapView:(MKMapView *)mapsView viewForAnnotation:(id <MKAnnotation>) annotation
    {
    
          if ([annotation isKindOfClass:[MKUserLocation class]])
    
          return nil;
    }
    

Upvotes: 1

Related Questions