我试图利用地图工具包使用 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
但现在没有别针了。