Google地图的自定义标记图标无法点击(使用委托)

时间:2015-12-26 08:02:05

标签: ios objective-c iphone google-maps

我已经在我的iOS应用中实现了Google Maps并显示了用户位置,我正在使用自定义标记图标。一切都正常。我已经实现了GMSMapViewDelegate

问题是,当我点击标记图标时,事件didTapAtCoordinate:不会被触发,但是当我点击地图上的任何地方时它就会被触发。我尝试将其设置为marker.tappable = YES;

时,该标记无法点击

我在互联网上搜索过但无法得到我错误或遗失的内容。

以下是代码:

/** SETUP MAP & MARKERS **/
-(void) setupMapMarkers {

    self.mapView.delegate = self;

    /** SET CAMERA POSITION ON MAP **/
    GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:[self.userObject.latitude doubleValue]
                                                            longitude:[self.userObject.longitude doubleValue]
                                                                 zoom:10];
    self.mapView.camera = camera;

    /** ADDING USER'S LOCATION MARKER **/
    CLLocationCoordinate2D position = CLLocationCoordinate2DMake([self.userObject.latitude doubleValue], [self.userObject.longitude doubleValue]);
    GMSMarker *marker = [GMSMarker markerWithPosition:position];
    marker.tappable = YES;

    NSURL *url = [[NSBundle mainBundle] URLForResource:@"pin_user_active" withExtension:@"gif"];
    marker.icon = [UIImage animatedImageWithAnimatedGIFData:[NSData dataWithContentsOfURL:url]];
    marker.map = self.mapView;
}

#pragma mark - GMSMapViewDelegate

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

    NSLog(@"You tapped at %f,%f", coordinate.latitude, coordinate.longitude);
}

// Added it just to check, if it works on tap
- (void)mapView:(GMSMapView *)mapView didTapOverlay:(GMSOverlay *)overlay {
    NSLog(@"tapped");
}

2 个答案:

答案 0 :(得分:1)

给marker.title = @"一些文字"并调用此委托方法:

- (void)mapView:(GMSMapView *)mapView didTapInfoWindowOfMarker:(GMSMarker *)marker;

当您点击标题时,将调用此方法。

答案 1 :(得分:0)

经过GOOGLE MAPS COCOA DOCS后,我们知道还有另一种专门用于标记点击的委托方法。

代表方法

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

它声明:

  

在特定坐标上点按手势后调用,但仅在未点按标记时调用。在取消选择任何当前选定的标记(用于点击地图的隐式操作)之前调用此方法。

因此,对于我们需要的标记点击事件:

- (BOOL)mapView:(GMSMapView *)mapView didTapMarker:(GMSMarker *)marker

我希望它也会帮助别人。