19

アプリのユーザーがマップ内の場所を選択できるようにしたいと考えています。ネイティブ マップには、ピンをドロップして何かを見つけることができる「ドロップ ピン」機能があります。MapKit でこれを行うにはどうすればよいですか?

4

4 に答える 4

27

MKAnnotationプロトコルを実装するオブジェクトを作成し、そのオブジェクトをMKMapViewに追加する必要があります。

@interface AnnotationDelegate : NSObject <MKAnnotation> {
    CLLocationCoordinate2D coordinate;
    NSString * title;
    NSString * subtitle;
} 

デリゲート オブジェクトをインスタンス化し、マップに追加します。

AnnotationDelegate * annotationDelegate = [[[AnnotationDelegate alloc] initWithCoordinate:coordinate andTitle:title andSubtitle:subt] autorelease];
[self._mapView addAnnotation:annotationDelegate];

マップは AnnotationDelegate の座標プロパティにアクセスして、マップ上のピンを配置する場所を見つけます。

注釈ビューをカスタマイズする場合は、Map View Controller にMKMapViewDelegate viewForAnnotationメソッドを実装する必要があります。

- (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>) annotation

ピン ドラッグ機能を実装したい場合は、Apple OS Reference Libraryで注釈タッチ イベントの処理について読むことができます。

GitHubの実用的なサンプル ライブラリを参照する mapkit を使用したドラッグ ドロップに関するこの記事も確認できます。DDAnnotationオブジェクトの_coordinatesメンバーをチェックすることで、ドラッグされた注釈の座標を取得できます。

于 2009-12-16T23:25:19.527 に答える
23

ピンを落とす方法は複数ありますが、質問でどちらの方法を指定するかは指定しません。最初の方法はプログラムで行うことです。そのため、カスタムクラスは実際には必要ないことを除いて、RedBlueThingが書いたものを使用できます(対象とするiOSのバージョンによって異なります)。iOS 4.0以降では、このスニペットを使用してプログラムでピンをドロップできます。

// Create your coordinate
CLLocationCoordinate2D myCoordinate = {2, 2};
//Create your annotation
MKPointAnnotation *point = [[MKPointAnnotation alloc] init];
// Set your annotation to point at your coordinate
point.coordinate = myCoordinate;
//If you want to clear other pins/annotations this is how to do it
for (id annotation in self.mapView.annotations) {
    [self.mapView removeAnnotation:annotation];
}
//Drop pin on map
[self.mapView addAnnotation:point];

たとえば、実際のmapViewを長押ししてピンをドロップできるようにする場合は、次のように実行できます。

// Create a gesture recognizer for long presses (for example in viewDidLoad)
UILongPressGestureRecognizer *lpgr = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleLongPress:)];
lpgr.minimumPressDuration = 0.5; //user needs to press for half a second.
[self.mapView addGestureRecognizer:lpgr]


- (void)handleLongPress:(UIGestureRecognizer *)gestureRecognizer {
    if (gestureRecognizer.state != UIGestureRecognizerStateBegan) {
        return;
    }
    CGPoint touchPoint = [gestureRecognizer locationInView:self.mapView];
    CLLocationCoordinate2D touchMapCoordinate = [self.mapView convertPoint:touchPoint toCoordinateFromView:self.mapView];
    MKPointAnnotation *point = [[MKPointAnnotation alloc] init];
    point.coordinate = touchMapCoordinate;
    for (id annotation in self.mapView.annotations) {
        [self.mapView removeAnnotation:annotation];
    }
    [self.mapView addAnnotation:point];
}

すべてのアノテーションを列挙する場合は、両方のスニペットのコードを使用してください。これは、すべての注釈の位置をログに記録する方法です。

for (id annotation in self.mapView.annotations) {
    NSLog(@"lon: %f, lat %f", ((MKPointAnnotation*)annotation).coordinate.longitude,((MKPointAnnotation*)annotation).coordinate.latitude);
}
于 2012-12-11T09:03:48.910 に答える
9

jcesarmobileでタッチした場所を取得できますiphone mapkitでタップされた座標を取得すると、次のようにどこにでもピンをドロップできます

// Define pin location
CLLocationCoordinate2D pinlocation;
pinlocation.latitude = 51.3883454 ;//set latitude of selected coordinate ;
pinlocation.longitude = 1.4368011 ;//set longitude of selected coordinate;

// Create Annotation point 
MKPointAnnotation *Pin = [[MKPointAnnotation alloc]init];
Pin.coordinate = pinlocation;
Pin.title = @"Annotation Title";
Pin.subtitle = @"Annotation Subtitle";

// add annotation to mapview
[mapView addAnnotation:Pin];
于 2015-04-07T06:26:27.473 に答える
3

MapView Delegate も設定する必要がある場合があります。

[mkMapView setDelegate:self];

次に、そのデリゲートを呼び出しますviewForAnnotation

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation{
    MKPinAnnotationView *pinAnnotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation
                                                                    reuseIdentifier:@"current"];
    pinAnnotationView.animatesDrop = YES;
    pinAnnotationView.pinColor = MKPinAnnotationColorRed;
    return pinAnnotationView;
}
于 2013-01-28T07:58:50.503 に答える