如何使用ios MapKit保持更新用户位置蓝点

时间:2013-10-09 11:07:13

标签: ios mapkit userlocation

我检查了许多链接并搜索了很多内容。我也尝试了这些代码,但没有成功,所以最后我在这里发布。

任何人都可以帮助我更新用户位置。

  • 我知道如何向用户显示当前位置,这只是一张支票 showUserLocation。

  • 我知道有位置管理和代理位置保持更新。

我尝试了一些代码,其中人们正在更新位置管理器更新委托中的地图。但它对我不起作用。它只是显示用户当前位置的蓝点,但如果我移动它不会继续更新。

那么,任何人都可以从一开始就指导我该怎么做。如何显示用户当前位置,无论用户何时移动或何处移动,都会在地图上保持更新。

2 个答案:

答案 0 :(得分:1)

请尝试使用此方法

  1. 不要忘记在标题文件中添加CLLocationManagerDelegate和MKMapviewDelegate。

  2. 首先将以下行实现到reload map方法或viewDidload或viewWillappear方法中:

     [mapView setShowsUserLocation:YES]
    
  3. 比如下更改以下方法:

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

答案 1 :(得分:1)

您可以自行完成,而无需使用showUserLocation

您应该使用CLLocationManager获取当前位置并将其添加到mapView

实施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

创建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

注意:

  • 请记住删除旧注释并在接收新注释时再次添加 位置
  • 请使用iOS 5和ARC