19

ユーザーが触れた場所に基づいて、地図に注釈を付ける方法を見つけようとしています。

私はサブクラス化を試みて、発砲MKMapViewするものを探しましたが、結局のところ、標準のtouchsメソッドを使用していません。touchesBeganMKMapView

また、をサブクラス化し、子としてUIViewを追加しMKMapViewてから、HitTestとをリッスンしてみましtouchesBeganた。これは多少機能します。私の地図がフルサイズの場合は、次のUIViewようなものがあります

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
    return map;
}

そしてそれはうまくいきます、私touchesBeganは使用してポイントを得ることができるでしょう

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
  for (UITouch *touch in touches){
  CGPoint pt = [touch  locationInView:map];
  CLLocationCoordinate2D coord= [map convertPoint:pt toCoordinateFromView:map];
  NSLog([NSString stringWithFormat:@"x=%f y=%f - lat=%f long = %f",pt.x,pt.y,coord.latitude,coord.longitude]);
 }
}

しかし、マップはスクロールしない、ダブルタップしない限りズームインしないなどの奇妙な動作をしますが、ズームアウトすることはできます。マップをビューとして返す場合にのみ機能します。ヒットテストメソッドがない場合、マップは正常に機能しますが、明らかにデータを取得しません。

座標を間違えようとしていますか?もっと良い方法があると教えてください。注釈をうまく追加する方法を知っています。ユーザーが地図に触れた場所とタイミングで注釈を追加する例を見つけることができません。

4

5 に答える 5

46

このコードを試すことができます

- (void)viewDidLoad
{
    UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(foundTap:)];

    tapRecognizer.numberOfTapsRequired = 1;

    tapRecognizer.numberOfTouchesRequired = 1;

    [self.myMapView addGestureRecognizer:tapRecognizer];
}


-(IBAction)foundTap:(UITapGestureRecognizer *)recognizer
{
    CGPoint point = [recognizer locationInView:self.myMapView];  

    CLLocationCoordinate2D tapPoint = [self.myMapView convertPoint:point toCoordinateFromView:self.view];

    MKPointAnnotation *point1 = [[MKPointAnnotation alloc] init];

    point1.coordinate = tapPoint;

    [self.myMapView addAnnotation:point1];
}

ではごきげんよう。

于 2013-04-26T06:38:25.287 に答える
9

だから私はついにそれを行う方法を見つけました。ビューを作成し、同じフレームでマップオブジェクトを追加した場合。次に、そのビューでヒットテストをリッスンします。送信されたタッチポイントでconvertPoint:toCoordinateFromView:を呼び出し、次のようなマップを指定できます。

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event{
    CLLocationCoordinate2D coord= [map convertPoint:point toCoordinateFromView:map];
    NSLog(@"lat  %f",coord.latitude);
    NSLog(@"long %f",coord.longitude);

    ... add annotation ...

    return [super hitTest:point withEvent:event];
}

これは現状ではかなりラフであり、マップをスクロールしても常にヒットテストが呼び出されるため、これを処理する必要がありますが、マップに触れてからgps座標を取得することから始めます。

于 2010-07-14T02:42:03.067 に答える
3

少しデッドスレッドを掘りますが、これはグーグルのトップの結果なので、それは価値があるかもしれません:

タップアンドホールドジェスチャレコグナイザーを使用して、座標を取得し、地図上にピンをドロップできます。すべてがfreshmobで説明されています

于 2012-06-02T19:00:56.773 に答える
2

Swift 4.2

func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {

for touch in touches {
    let touchPoint = touch.location(in: mapView)
    let location = mapView.convert(touchPoint, toCoordinateFrom: mapView)
    print ("\(location.latitude), \(location.longitude)")
}}
于 2020-04-16T00:23:06.153 に答える
1

スウィフト2.2

func gestureRecognizerShouldBegin(gestureRecognizer: UIGestureRecognizer) -> Bool {
    let point = gestureRecognizer.locationInView(mapView)
    let tapPoint = mapView.convertPoint(point, toCoordinateFromView: view)
    coordinateLabel.text = "\(tapPoint.latitude),\(tapPoint.longitude)"

    return true
}
于 2016-06-16T13:19:22.780 に答える