【问题标题】:Google maps iOS sdk get tapped overlay coordinates谷歌地图 iOS sdk 获得点击覆盖坐标
【发布时间】:2014-05-25 16:55:50
【问题描述】:

我正在使用 Google 地图 iOS sdk。 当用户点击覆盖时,我想获取触摸点的坐标。

有这个委托方法:

- (void)mapView:(GMSMapView *)mapView didTapAtCoordinate:(CLLocationCoordinate2D)coordinate

但当您点击覆盖或标记时不会调用它。

我可以通过编程方式调用它吗(但缺少坐标参数——这就是我想要的......)? 或从中获取位置:

- (void) mapView: (GMSMapView *) mapView  didTapOverlay: (GMSOverlay *) overlay

任何建议都很宝贵!

谢谢!

【问题讨论】:

标签: ios objective-c google-maps google-maps-sdk-ios gmsmapview


【解决方案1】:

2015 年 6 月 2 日更新

只需将 UITapGestureRecognizer 钉在地图上,然后从触摸点提取坐标。你的 didTapAtCoordinate 和 didTapAtOverlay 会像以前一样继续触发。

  UITapGestureRecognizer *touchTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapTouchTap:)];
  [self.view addGestureRecognizer:touchTap];

-(void)tapTouchTap:(UITapGestureRecognizer*)touchGesture
{
  CGPoint point = [touchGesture locationInView:self.view];
  CLLocationCoordinate2D coord = [self.mapView.projection coordinateForPoint:point];
  NSLog(@"%f %f", coord.latitude, coord.longitude);
}

原帖

您可能缺少两个 sn-ps 代码。 在头文件中采用 GMSMapViewDelegate:

@interface Map : UIViewController <GMSMapViewDelegate>

您还需要在 viewDidLoad 中设置委托:

self.mapView.delegate = self;

现在应该为你触发:

- (void)mapView:(GMSMapView *)mapView didTapAtCoordinate:(CLLocationCoordinate2D)coordinate
{
    NSLog(@"You tapped at %f,%f", coordinate.latitude, coordinate.longitude);
}

【讨论】:

  • 这是正确的,但此方法不会在覆盖点击时触发。 OP 可能有此设置,但正在寻找一种方法来触发覆盖点击以及获取覆盖点击位置的地理坐标。
【解决方案2】:

如果只想获取标记的位置,有一个位置属性

CLLocationCoordinate2D coord = marker.position;

当这个委托方法被调用时

- (void)mapView:(GMSMapView *)mapView didTapAtCoordinate:(CLLocationCoordinate2D)coordinate

根据我的经验,它适用于没有标记或覆盖的屏幕点击。

GMSOverlay 有点不同,因为它是 GMSMarker 的超类。您只需要为您的自定义叠加层子类化 GMSOverlay 并添加一个位置属性。当您创建叠加层时,例如在 didTapAtCoordinate 中,您可以在其中指定位置(GPS 坐标)。

【讨论】:

  • 是的,我可以获取标记位置,但我有兴趣获取 GMSPolygon 和 GMSPolyline 的位置。当用户点击它时,该形状已经存在。基本上,我想在点击事件中放大该位置。 (是的,我没有那么解释......)谢谢!
【解决方案3】:

您可以将圆圈设置为不可点击(默认行为),并使用 didTapAtCoordinate 代表捕获地图上的所有点击。

然后,当触发此事件时,您可以遍历所有圈子以检查用户是否在其中一个圈子内或外面点击。

【讨论】:

    【解决方案4】:

    self.mapview.settings.consumesGesturesInView = NO;

    在 viewdidload 中或分配后添加此行。

    然后实现这个委托方法。

    - (void)mapView:(GMSMapView *)mapView didTapAtCoordinate:(CLLocationCoordinate2D)coordinate
    {
        NSLog(@"%g",coordinate);
    }
    
    
    - (void)mapView:(GMSMapView *)mapView didTapOverlay:(GMSOverlay *)overlay
    {
    
        GMSCircle *circle=(GMSCircle *)overlay;
        CLLocationCoordinate2D touchCoOrdinate= circle.position;
        NSLog(@"%g",touchCoOrdinate);
    }
    

    【讨论】:

      【解决方案5】:

      所以你不能让 didTapAtCoordinateMethod 触发覆盖点击。但是我确实找到了一个稍微肮脏的解决方法。

      使用叠加层来绘制折线,我们需要一种方法来识别折线被点击的位置。所以在绘制折线时,我们可以像这样构建它们。

          //draw line
          GMSPolyline *polyline = [GMSPolyline polylineWithPath:path];
          polyline.strokeColor = [UIColor purpleColor];
          polyline.tappable = TRUE;
          polyline.map = self.googleMapView;
          polyline.title = routestring;
      

      其中 routestring 是来自的构建字符串

      routestring = [NSString stringWithFormat:@"%@/%@/%@",lat,lng,[annnotationobject objectForKey:@"linkId"]];
      

      而 lat 和 lng 是我们坐标的字符串值。最后一部分是折线的 ID。

      路由字符串存储了坐标和一个用'/'分隔的ID,以便我们以后可以使用字符串的组件路径来查找它们。这分配给折线标题。

      现在点击叠加层时:

      -(void)mapView:(GMSMapView *)mapView didTapOverlay:(GMSOverlay *)overlay{
      
      NSString *path = overlay.title;
      
      //Finding componentpaths of string
      NSArray *pathparts = [path pathComponents];
      NSString *lat = [pathparts objectAtIndex:0];
      NSString *lng = [pathparts objectAtIndex:1];
      NSString *linkID = [pathparts objectAtIndex:2];
      
      //Here we are building a marker to place near the users tap location on the polyline.
      GMSMarker *marker = [GMSMarker markerWithPosition:CLLocationCoordinate2DMake([lat doubleValue],[lng doubleValue])];
      marker.title = overlay.title;
      marker.snippet = @"ROUTE DATA";
      marker.map = self.googleMapView;
      
      //This will popup a marker window
      [self.googleMapView setSelectedMarker:marker];
      

      }

      我们可以使用我们构建的字符串的组件路径(以“/”分隔)从折线中获取经纬度坐标。然后为他们分配一个标记以弹出叠加项上的信息。

      【讨论】:

        【解决方案6】:

        您可以继承 UITapGestureRecognizer 并覆盖其 touchesEnded 来检索触摸点,而不是使用 GMSMapView 的坐标点来检索坐标。

        子类 UITapGestureRecognizer 并将其添加到您的 mapView:

        self.touchTap = [[TouchGestureRecognizer alloc] initWithTarget:self action:@selector(tapTouchTap)];
        self.touchTap.mapView = self.mapView;
        [self.view addGestureRecognizer:self.touchTap];
        

        TouchDownGestureRecognizer.h:

        #import <UIKit/UIKit.h>    
        
        @interface TouchGestureRecognizer : UITapGestureRecognizer
        @property  GMSMapView       *mapView;
        @end
        

        TouchDownGestureRecognizer.m:

        #import "TouchGestureRecognizer.h"
        #import <UIKit/UIGestureRecognizerSubclass.h>
        
        @implementation TouchGestureRecognizer
        
        -(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
        {
          [super touchesEnded:touches withEvent:event];
        
          UITouch *touch = touches.allObjects[0];
          CGPoint point = [touch locationInView:self.mapView];
          CLLocationCoordinate2D coord = [self.mapView.projection coordinateForPoint:point];
          NSLog(@"%f %f", coord.latitude, coord.longitude);
        }
        
        @end
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-09-29
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多