简体   繁体   English

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

[英]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 know how to show user current location it's just a check showUserLocation. 我知道如何显示用户当前位置,这只是一个检查showUserLocation。

  • I know there is location Manage and delegate for location keep updates. 我知道有位置管理和代表位置保持更新。

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. 如何显示用户当前位置,无论用户何时何地移动,都将在地图上保持更新。

please try using this method 请尝试使用此方法

  1. Don't forget to add CLLocationManagerDelegate and MKMapviewDelegate in your header File. 不要忘记在头文件中添加CLLocationManagerDelegate和MKMapviewDelegate。

  2. first implement the below line into your reload map method or viewDidload or viewWillappear method: 首先在您的重载地图方法或viewDidload或viewWillappear方法中实现以下行:

      [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; } 

You can do it by yourself without using showUserLocation 您可以自己使用,而无需使用showUserLocation

You should use CLLocationManager to get current location and add it into mapView 您应该使用CLLocationManager获取当前位置并将其添加到mapView中

Implement MKAnnotation 实施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 创建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 请使用iOS 5和ARC

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM