【问题标题】:Custom infobox when tap event点击事件时的自定义信息框
【发布时间】:2016-08-07 18:12:43
【问题描述】:

我试图让一个与标记标准相同的信息框在我的折线被点击时出现。当点击该行时,我得到了一个NSLog 来输出,但现在我需要显示信息框而不是NSLog。我看过一些 Javascript 示例,但没有客观的 c 示例。

- (void)loadView {

// Create a GMSCameraPosition
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:37.551927
                                                        longitude:-77.456292
                                                             zoom:18];
GMSMapView *mapView = [GMSMapView mapWithFrame:CGRectZero camera:camera];
CLLocationCoordinate2D position = CLLocationCoordinate2DMake(37.551709, -77.456510);
mapView.settings.myLocationButton = YES;
mapView.myLocationEnabled = YES;
self.view = mapView;
mapView.delegate = self;

GMSMutablePath *path = [GMSMutablePath path];
[path addCoordinate:CLLocationCoordinate2DMake(37.552243, -77.457415)];
[path addCoordinate:CLLocationCoordinate2DMake(37.551054, -77.455443)];

GMSPolyline *polyline = [GMSPolyline polylineWithPath:path];

UILabel *myLabel = [[UILabel alloc] init];

UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self      action:@selector(labelTapped)];
tapGestureRecognizer.numberOfTapsRequired = 1;
[myLabel addGestureRecognizer:tapGestureRecognizer];
 myLabel.userInteractionEnabled = YES;

polyline.spans = @[[GMSStyleSpan spanWithColor:[UIColor greenColor]]];
polyline.strokeWidth = 5.f;
polyline.tappable = true;
polyline.map = mapView;

}

- (void)mapView:(GMSMapView *)mapView didTapOverlay:(GMSOverlay *)overlay
{
    NSLog(@"in didTapOverlay");
}

@end

【问题讨论】:

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


    【解决方案1】:

    查看 iOS 地图教程中的 Events 指南。 这是 Objective-C 中的一个 sn-p:

    - (void)loadView {
      GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:1.285
                                                              longitude:103.848
                                                                   zoom:12];
      GMSMapView *mapView = [GMSMapView mapWithFrame:CGRectZero camera:camera];
      mapView.delegate = self;
      self.view = mapView;
    }
    
    #pragma mark - GMSMapViewDelegate
    
    - (void)mapView:(GMSMapView *)mapView
        didTapAtCoordinate:(CLLocationCoordinate2D)coordinate {
      NSLog(@"You tapped at %f,%f", coordinate.latitude, coordinate.longitude);
    }
    

    这是一个SO thread 演示GMSMapViewDelegate 的用法:

    1.符合GMSMapViewDelegate协议。

    @interface YourViewController () <GMSMapViewDelegate>
    // your properties
    @end
    2.set your mapView_ delegate.
    
    mapView_.delegate = self;
    3.implement the GMSMapViewDelegate method
    
    - (void)mapView:(GMSMapView *)mapView didTapInfoWindowOfMarker:(GMSMarker *)marker {
        // your code
    }
    

    补充:marker.userData 很有用。您可以将所需的数据设置到其中并在- mapView:didTapInfoWindowOfMarker:中使用它

    【讨论】:

      猜你喜欢
      • 2017-01-28
      • 1970-01-01
      • 2011-07-22
      • 2012-09-19
      • 1970-01-01
      • 2013-12-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多