【问题标题】:Annotation Pin for current location not appearing当前位置的注释图钉未出现
【发布时间】:2016-01-09 10:50:39
【问题描述】:

我试图通过 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

但现在没有别针了。

【问题讨论】:

    标签: ios mkmapview mapkit mkannotation mkpointannotation


    【解决方案1】:

    => 当您设置 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】:

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

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

      【讨论】:

      • 我根据您的问题更新了我的代码,但现在没有 pin。请检查上面的更新代码。
      【解决方案3】:

      是的,如果您不想在 mapview 中显示图钉,那么请不要在代码之外或将其删除,因为您在 didUpdateUserLocation 中编写了代码。

      【讨论】:

      • 这不是我的问题。我想要一个位置(当前)的 pin 但不是多个。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多