2

我试图利用地图工具包使用 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

但现在没有别针了。

4

3 回答 3

3

=> 当您设置 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 ***//
}
于 2016-01-09T13:30:04.313 回答
2

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

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

于 2016-01-09T11:01:27.193 回答
0

是的,如果您不想在 mapview 中显示图钉,那么请远离代码或删除您在 didUpdateUserLocation 中编写的代码。

于 2016-01-09T11:19:53.833 回答