OGeek|极客世界-中国程序员成长平台

标题: ios - 当前位置的注释图钉未出现 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-13 12:38
标题: ios - 当前位置的注释图钉未出现

我试图利用 map 工具包通过 MKMapView 及其委托(delegate)方法 didUpdateUserLocation 显示我当前的位置,但是当我放大并非常接近时,我会看到多个图钉。然后经过一些建议,我意识到,每次更新位置,都会重新创建引脚。所以,我从委托(delegate)方法中取出了我的添加注释代码。

这是我的代码-

#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 respondsToSelectorselector(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)mapViewMKMapView *)mapView didUpdateUserLocationMKUserLocation *)userLocation{

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

}

@end

但现在没有别针了。



Best Answer-推荐答案


=> 当您设置 mapView.showsUserLocation = YES 时,MKMapView 会自动放置 MKUserLocation 类的注释:

您可以通过在 mapView:viewForAnnotation 中执行此操作来将此注释的默认 View 替换为您想要的任何默认注释 View :

- (MKAnnotationView *) mapViewMKMapView *)theMapView viewForAnnotationid <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 ***//
}

关于ios - 当前位置的注释图钉未出现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34692534/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://jike.in/) Powered by Discuz! X3.4