2011-06-28 64 views
6

我在我的應用程序中有MAP視圖,我想在地圖視圖中心添加註釋針(紅色針)。
現在當用戶滾動地圖視圖引腳應該按照中心來調整。
如何做到這一點?
感謝如何在iPhone中添加地圖視圖中心的註釋?

+0

這意味着你被改變中心改變銷的位置。 –

+0

@Muhammad Zeeshan - 是的,當用戶滾動地圖時,紅色的別針(註解)應該改變以適應用戶可見的地圖視圖的新中心。 – dks1725

+0

嘗試將註釋放到地圖中心 –

回答

10

如果你想用一個實際的註解,而不是定位在地圖視圖的中心,只是一個普通視圖,您可以:

  • 使用註解類與設定的座標屬性(預-defined MKPointAnnotation class eg)。這避免了在中心改變時必須移除和添加註釋。
  • 在viewDidLoad中創建註釋
  • 保持對它的引用它的屬性,說centerAnnotation
  • 更新其座標(和標題等),在地圖視圖中的regionDidChangeAnimated的委託方法(確保地圖視圖的委託屬性設置)

例子:

@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 

現在,這將移動註釋,但並不順利。如果你需要的註釋更平滑地移動,你可以添加一個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; 
} 
+0

感謝它的工作完美.. – dks1725

+0

從我的+1,仍然高於解決方案並沒有給出「仍然」的註釋,但也是一個完美的基礎以及提到在地圖解決方案上方查看。 –

+0

有人可以用swift寫這段代碼:)? –

4

安娜的回答是聰明的做法,以保持在中心的註解使用標準mapview方式中的註釋來映射。然而,正如一位評論者指出的那樣,滾動和捏合雖然更好,但仍然顯示出明顯的滯後,推薦的方法是將註釋視圖添加爲mapview的子視圖。這是看起來像。

@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佔左下角和銷頭之間的距離。我不喜歡在代碼中使用這些與資產相關的常量,因此如果任何人都可以找到更好的方法來實現這一點,那麼我完全可以接受。

我創建了一個帶有地圖控制器的github項目,它可以做到這一點,以及其他一些與使用mapview來讓用戶選擇一個位置相關的東西。看看這裏:https://github.com/scottrhoyt/CenterPinMapViewController

+0

這太好了。謝謝。有關如何將縮放鎖定在引腳上的任何想法?因此,縮放時只能放大引腳,反之亦然。 http://stackoverflow.com/questions/25577813/make-mkmapview-only-zoom-in-on-the-centercoordinate/25577949#25577949 –

0

斯威夫特版本

在類:

​​

在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) 
} 
相關問題