当前位置的注释引脚未出现

时间:2016-01-09 10:50:39

标签: ios mkmapview mapkit mkannotation mkpointannotation

我正在尝试使用地图工具包来显示我当前的位置,使用MKMapView及其委托方法didUpdateUserLocation但是当我放大并且非常接近时,我会看到多个引脚。然后在我意识到一些建议之后,每次更新位置时都会重新创建引脚。所以,我从委托方法中提取了我的添加注释代码。

这是我的代码 -

#import "MapViewController.h"
#import <MapKit/MapKit.h>
#import <CoreLocation/CoreLocation.h>


@interface MapViewController ()<MKMapViewDelegate, CLLocationManagerDelegate, UITextFieldDelegate> {
    MKPointAnnotation *myAnnotation;
}
@property (nonatomic, strong) IBOutlet MKMapView *mapView;
@property (nonatomic, strong) IBOutlet UITextField *searchTextField;

@property (strong, nonatomic) CLLocationManager *locationManager;

@end

@implementation MapViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.mapView.delegate=self;
    self.mapView.showsUserLocation=YES;

    self.locationManager = [[CLLocationManager alloc] init];
    self.locationManager.delegate = self;

    if ([self.locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) {
        [self.locationManager requestWhenInUseAuthorization];
    }

    myAnnotation = [[MKPointAnnotation alloc]init];
    myAnnotation.coordinate = self.mapView.userLocation.coordinate;
    myAnnotation.title = @"Current Location";
    [self.mapView addAnnotation:myAnnotation];  
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];

}

#pragma mark-
#pragma mark- MapView Delegate


- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation{

    MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(userLocation.coordinate, 800, 800);
    [self.mapView setRegion:[self.mapView regionThatFits:region] animated:YES];

}

@end

但现在没有针。

3 个答案:

答案 0 :(得分:2)

亲爱的,您需要将myAnnotation代码放在​​委托方法“didUpdateUserLocation”之外

注意:您需要在viewDidLoad中制作注释,然后使用didUpdateUserLocation更新坐标

答案 1 :(得分:2)

=&GT;设置mapView.showsUserLocation = YES时,MKMapView会自动放置类MKUserLocation的注释:

您可以在mapView中将此注释的默认视图替换为您想要的任何默认注释视图:viewForAnnotation:

- (MKAnnotationView *) mapView:(MKMapView *)theMapView viewForAnnotation:(id <MKAnnotation>)annotation {
    if ([annotation isKindOfClass:[MKUserLocation class]]) {
        // replace the following with code to generate and return custom user position annotation view
        return customAnnotationView;
    }

    //*** other code ***//
}

答案 2 :(得分:0)

是的,如果您不想在mapview中显示pin,那么请保留代码的一面,或者删除它在didUpdateUserLocation中写入的内容。