谷歌地图iOS sdk获取挖掘重叠坐标

我正在使用谷歌地图iOS sdk。 当用户点击叠加层时,我想要获取触摸点的坐标。

有这个委托方法:

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

但是当你点击覆盖或标记时,它不会被调用。

我可以以编程方式调用它(但坐标参数是缺less的,这就是我想要的..)? 或从这里获得位置:

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

任何build议的宝贵!

谢谢!

更新6/2/15

只需将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); } 

原来的post

您可能会丢失两个代码片段。 在头文件中采用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); } 

如果你只是想获得标记的位置,那么有一个位置属性

 CLLocationCoordinate2D coord = marker.position; 

当这个委托方法被调用

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

它的屏幕水龙头,没有标记或覆盖从我的expierience。

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

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); } 

所以你不能让didTapAtCoordinateMethod触发叠加式水龙头。 不过,我发现一个稍微肮脏的解决方法。

使用覆盖来绘制多段线,我们需要一种方法来识别多段线被点击的位置。 所以当绘制多段线时,我们可以像这样构build它们。

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

其中routestring是来自内置的string

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

lat和lng是我们坐标的string值。 最后一部分是折线的ID。

routestring存储的坐标和由'/'分隔的ID,以便我们可以使用string的组件path来查找它们。 这被分配给多段线标题。

现在,当覆盖层被挖掘时:

 -(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]; 

}

我们可以使用我们构build的string(用“/”分隔)的组件path从多段线中获取纬度和经度坐标。 然后为它们分配一个标记,以在覆盖项目上popup信息。

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

然后,当这个事件被触发时,你可以遍历你的所有圈子,检查用户是否在一个圈内或外侧敲击。

您可以inheritanceUITapGestureRecognizer并覆盖它的touchesEnded以检索触点,而不是使用GMSMapView的coordinateForPoint检索坐标。

子类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