私は自分のアプリでMAPビューを持っており、注釈ピン(赤いピン)を地図ビューの中央に追加したいと思います。
ユーザーのスクロールマップビューのピンは、それに従ってセンターで調整する必要があります。
どうすればいいですか?マップビューの中心部の上方に位置するだけで通常のビューのではなく、実際のアノテーションを使用したい場合は
をすることができます、iPhoneのマップビューの中心に注釈を追加するには?
答えて
ありがとう:
- が設定可能な座標プロパティと注釈クラスを使用します(前定義されたクラス
MKPointAnnotation
など)。これにより、中心が変更されたときにアノテーションを削除して追加する必要がなくなります。 - 確認マップビューのデリゲートプロパティが設定されていることを確認し(マップビューの
regionDidChangeAnimated
デリゲートメソッドでcenterAnnotation - は、その(など、タイトル)座標を更新すると言う、プロパティにそれへの参照を保持
- のviewDidLoad で を注釈を作成します)
例:
@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
さて、これはスムーズに注釈を移動しなくなります。あなたがよりスムーズに移動する注釈が必要な場合は、マップビューにUIPanGestureRecognizer
とUIPinchGestureRecognizer
を追加しても、ジェスチャーハンドラ内で注釈を更新することができます。
// (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;
}
その完璧な作業に感謝しています。 – dks1725
+1私はまだ解決策の上に「まだ」注釈を与えていませんが、地図ソリューションの上に表示します。 –
誰かが速やかにこのコードを書くことができます:) –
アンナの答えはで中心に注釈を維持する巧妙なアプローチであります標準的な地図ビューの注釈を使用してマップします。しかし、コメント執筆者の1人が指摘するように、スクロールとピンチではずっと優れた状態が続いていますが、推奨されるアプローチは、アノテーションビューをマップビューのサブビューとして追加することです。それは何のように見えます。
@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プロジェクトを作成しました。ここでそれをチェックアウト:https://github.com/scottrhoyt/CenterPinMapViewController
これは素晴らしいことです。ありがとうございました。どのようにあなたはピンにズームをロックすることができますか?ズームするときは、ピンをズームするだけです。 http://stackoverflow.com/questions/25577813/make-mkmapview-only-zoom-in-on-the-centercoordinate/25577949#25577949 –
スウィフトバージョンのクラスでは
:のviewDidLoadで
:viewDidAppearで
if #available(iOS 9.0, *) {
centerAnnotationView.pinTintColor = customColor
} else {
// Fallback on earlier versions
centerAnnotationView.pinColor = MKPinAnnotationColor.Red
}
self.view.addSubview(centerAnnotationView)
:
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)
}
- 1. Morris.jsに注釈/注釈を追加
- 2. 選択したセルに応じて、マップビューに注釈ピンを追加する
- 3. 同じマップビューで異なる画像を注釈に追加する方法
- 4. 注釈をpdfに追加
- 5. iphoneアプリケーションのmkmapviewに複数の注釈を追加するには?
- 6. 注釈を追加する
- 7. データポイントに注釈を追加する
- 8. iPhone MapViewに複数の注釈を追加するのが遅い
- 9. PDFで注釈を追加
- 10. 新しい注釈をMKAnnotationViewに追加するには
- 11. MKMapView - 削除する注釈を次のように私のマップビューから注釈を削除
- 12. アンドロイドのタブにマップビューを追加する
- 13. ハイチャートチャートに注釈テキストを追加する方法は?
- 14. 複数のピンポイント注釈をマップに追加する方法
- 15. マップ内の注釈記号にテキストを追加する方法
- 16. iphoneアプリケーションのMapviewで複数の注釈を追加する方法
- 17. iPhoneマップビューカスタム注釈
- 18. Gsonによるクラス注釈の中止
- 19. ノード(graphicsmagick/imagemagick)の中心テキストをgmで追加するには?
- 20. マップビューでマーカーにテキストを追加する
- 21. 注釈を使用して別の注釈を異なるファセットに追加する
- 22. Frama-C:プラグインで注釈を追加
- 23. プログラムで図形に注釈を追加する
- 24. NSMutableArrayに注釈を追加する手助け
- 25. メソッドにJava注釈を遡及的に追加しますか?
- 26. アンドロイドのマップビューにダイアログボックスを追加するには
- 27. iPhoneのMapView注釈/ピン
- 28. userlocationのカスタム注釈ビューでマップビューを移動しない
- 29. アンドロイドで複数のマーカーをマップビューに追加する方法は?
- 30. iOS用のLibHaruカスタム注釈を追加する
それはあなたが中央を変更することにより、ピンの位置を変えていることを意味します。 –
@Muhammad Zeeshan - はい、ユーザーがスクロールすると、赤いピン(注釈)がユーザーに表示されるマップビューの新しい中心に合わせて変更されます。 – dks1725
注釈をマップの中心に置くようにしてください –