0

我检查了很多链接并用谷歌搜索了很多。我也尝试了这些代码,但没有成功,所以最后我把它贴在这里。

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

  • 我知道如何显示用户当前位置,这只是一个检查 showUserLocation。

  • 我知道有位置管理和委托进行位置保持更新。

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

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

4

2 回答 2

1

请尝试使用此方法

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

  2. 首先在您的重新加载地图方法或 viewDidload 或 viewWillappear 方法中实现以下行:

     [mapView setShowsUserLocation:YES]
    
  3. 比像这样更改以下方法:

    (MKAnnotationView *) mapView:(MKMapView *)mapsView viewForAnnotation:(id <MKAnnotation>) annotation
    {
    
          if ([annotation isKindOfClass:[MKUserLocation class]])
    
          return nil;
    }
    
于 2013-10-09T11:30:25.247 回答
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

创建 1UIViewController

@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
于 2013-10-09T11:43:29.583 回答