簡體   English   中英

如何在 iPhone 的 map 視圖的中心添加注釋?

[英]How to add annotation on center of map view in iPhone?

我的應用程序中有 MAP 視圖,我想在 map 視圖的中心添加注釋引腳(紅色引腳)。
現在,當用戶滾動 map 時,視圖引腳應根據該中心調整。
怎么做?
感謝

如果您想使用實際注釋而不是僅位於 map 視圖中心上方的常規視圖,您可以:

  • 使用帶有可設置坐標屬性的注釋 class(預定義的MKPointAnnotation class 例如)。 這避免了在中心更改時必須刪除和添加注釋。
  • 在 viewDidLoad 中創建注解
  • 在屬性中保留對它的引用,例如 centerAnnotation
  • 在 map 視圖的regionDidChangeAnimated委托方法中更新其坐標(和標題等)(確保設置了 map 視圖的委托屬性)

例子:

@interface SomeViewController : UIViewController <MKMapViewDelegate> {
    MKPointAnnotation *centerAnnotation;
}
@property (nonatomic, retain) MKPointAnnotation *centerAnnotation;
@end

@implementation SomeViewController

@synthesize centerAnnotation;

- (void)viewDidLoad {
    [super viewDidLoad];

    MKPointAnnotation *pa = [[MKPointAnnotation alloc] init];
    pa.coordinate = mapView.centerCoordinate;
    pa.title = @"Map Center";
    pa.subtitle = [NSString stringWithFormat:@"%f, %f", pa.coordinate.latitude, pa.coordinate.longitude];
    [mapView addAnnotation:pa];
    self.centerAnnotation = pa;
    [pa release];
}

- (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated {
    centerAnnotation.coordinate = mapView.centerCoordinate;
    centerAnnotation.subtitle = [NSString stringWithFormat:@"%f, %f", centerAnnotation.coordinate.latitude, centerAnnotation.coordinate.longitude]; 
}

- (void)dealloc {
    [centerAnnotation release];
    [super dealloc];
}

@end

現在這將移動注釋但不順利。 如果您需要注釋移動得更順暢,您可以在 map 視圖中添加UIPanGestureRecognizerUIPinchGestureRecognizer並更新手勢處理程序中的注釋:

    // (Also add UIGestureRecognizerDelegate to the interface.)

    // In viewDidLoad:
    UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handleGesture:)];
    panGesture.delegate = self;
    [mapView addGestureRecognizer:panGesture];
    [panGesture release];

    UIPinchGestureRecognizer *pinchGesture = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(handleGesture:)];
    pinchGesture.delegate = self;
    [mapView addGestureRecognizer:pinchGesture];
    [pinchGesture release];

- (void)handleGesture:(UIGestureRecognizer *)gestureRecognizer
{
    centerAnnotation.coordinate = mapView.centerCoordinate;
    centerAnnotation.subtitle = [NSString stringWithFormat:@"%f, %f", centerAnnotation.coordinate.latitude, centerAnnotation.coordinate.longitude]; 
}

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
    //let the map view's and our gesture recognizers work at the same time...
    return YES;
}

Anna 的回答是使用標准 mapview 方式的注釋將注釋保持在 map 中心的巧妙方法。 然而,正如一位評論者指出的那樣,滾動和捏合雖然好得多,但仍然顯示出明顯的滯后,推薦的方法是將注釋視圖添加為地圖視圖的子視圖。 這就是它的樣子。

@interface SHCenterPinMapViewController () <MKMapViewDelegate>

@property (weak, nonatomic) IBOutlet MKMapView *mapView;
@property (strong, nonatomic) MKPointAnnotation *centerAnnotaion;
@property (strong, nonatomic) MKPinAnnotationView *centerAnnotationView;

@end

@implementation SHCenterPinMapViewController

- (MKPointAnnotation *)centerAnnotaion
{
    if (!_centerAnnotaion) {
        _centerAnnotaion = [[MKPointAnnotation alloc] init];
    }

    return _centerAnnotaion;
}

- (MKPinAnnotationView *)centerAnnotationView
{
    if (!_centerAnnotationView) {
        _centerAnnotationView = [[MKPinAnnotationView alloc] initWithAnnotation:self.centerAnnotaion
                                                                reuseIdentifier:@"centerAnnotationView"];
    }

    return _centerAnnotationView;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.mapView.delegate = self;
    [self.mapView addSubview:self.centerAnnotationView];
}

-(void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    [self moveMapAnnotationToCoordinate:self.mapView.centerCoordinate];
}

// These are the constants need to offset distance between the lower left corner of
// the annotaion view and the head of the pin
#define PIN_WIDTH_OFFSET 7.75
#define PIN_HEIGHT_OFFSET 5

- (void)moveMapAnnotationToCoordinate:(CLLocationCoordinate2D) coordinate
{
    CGPoint mapViewPoint = [self.mapView convertCoordinate:coordinate toPointToView:self.mapView];

    // Offset the view from to account for distance from the lower left corner to the pin head
    CGFloat xoffset = CGRectGetMidX(self.centerAnnotationView.bounds) - PIN_WIDTH_OFFSET;
    CGFloat yoffset = -CGRectGetMidY(self.centerAnnotationView.bounds) + PIN_HEIGHT_OFFSET;

    self.centerAnnotationView.center = CGPointMake(mapViewPoint.x + xoffset,
                                                   mapViewPoint.y + yoffset);
}

- (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated
{
    self.centerAnnotaion.coordinate = mapView.centerCoordinate;
    [self moveMapAnnotationToCoordinate:mapView.centerCoordinate];
}

@end

真正唯一有趣的是,您必須將 MKPinAnnotationView 偏移一定量以考慮左下角和針頭之間的距離。 我不喜歡在代碼中包含這些資產相關的常量,所以如果有人能找到更好的方法來做到這一點,我會全力以赴。

I created a github project with a map controller that does this as well as some other things related to using a mapview to have a user select a location. 在這里查看: https://github.com/scottrhoyt/CenterPinMapViewController

Swift版

在 class 中:

var centerAnnotation = MKPointAnnotation()
var centerAnnotationView = MKPinAnnotationView()

在 viewDidLoad 中:

if #available(iOS 9.0, *) {
        centerAnnotationView.pinTintColor = customColor
    } else {
        // Fallback on earlier versions
        centerAnnotationView.pinColor = MKPinAnnotationColor.Red
    }
self.view.addSubview(centerAnnotationView)

在 viewDidAppear 中:

self.moveMapAnnotationToCoordinate(self.mapView.centerCoordinate)

然后:

func moveMapAnnotationToCoordinate(locate : CLLocationCoordinate2D ) {
    let mapViewPoint : CGPoint = self.mapView.convertCoordinate(locate, toPointToView: self.mapView)
    let pinWidth : CGFloat = 7.75
    let pinHeight : CGFloat = 7
    let xOffset : CGFloat = CGRectGetMidX(self.centerAnnotationView.bounds) - pinWidth
    let yOffset : CGFloat = CGRectGetMidY(self.centerAnnotationView.bounds) - pinHeight

    self.centerAnnotationView.center = CGPointMake(mapViewPoint.x - xOffset, mapViewPoint.y - yOffset)
}

要使用位置更改,請添加委托CLLocationManagerDelegate ,然后:

func mapView(mapView: MKMapView, regionDidChangeAnimated animated: Bool) {
    self.centerAnnotation.coordinate = self.mapView.centerCoordinate
    self.moveMapAnnotationToCoordinate(self.mapView.centerCoordinate)
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM