Custom annotation view in Google Maps SDK(自定义注释在谷歌地图SDK)

I created an iOS application based on maps in which I thought to use Google Maps SDK for iOS instead of Mapkit, I found the documentation but it I didn’t find methods related to custom annotation view, Can anyone provide me the solution for how to create custom annotation view(info window) and how to add content(title, snippet) for it.

翻译:我创建了一个iOS应用程序基于地图,我想使用谷歌地图为iOS SDK,相反地,我发现文档,但我当时不知道€™t找到自定义注释相关的方法看来,谁能提供我的解决方案如何创建自定义注释视图(信息窗口)和如何添加内容(标题、片段)

1.

If you check GMSMapView.h within GoogleMaps.Framework, you can see the below method which should let you add a custom infowindow for a marker, instead of using standard title and snippet alone:

NOTE you (op) say annotationView=infoWindow
BUT NORMAL: annotationView = marker itself and calloutView = infoWindow

翻译:

如果你检查GMSMapView。 在GoogleMaps h。 框架中,您可以看到以下方法应该让您添加一个自定义infowindow标记,而不是使用标准的标题和片段:

注意你(op)annotationView = infoWindow说
正常:annotationView =本身和calloutView = infoWindow标志

2.

/**
 * Called when a marker is about to become selected, and provides an optional
 * custom info window to use for that marker if this method returns a UIView.
 * If you change this view after this method is called, those changes will not
 * necessarily be reflected in the rendered version.
 *
 * The returned UIView must not have bounds greater than 500 points on either
 * dimension.  As there is only one info window shown at any time, the returned
 * view may be reused between other info windows.
 *
 * @return The custom info window for the specified marker, or nil for default
 */
- (UIView *)mapView:(GMSMapView *)mapView
    markerInfoWindow:(id<GMSMarker>)marker;
使用 SMCalloutView


首先,做一些设置:

#import <SMCalloutView/SMCalloutView.h>

static const CGFloat CalloutYOffset = 10.0f;

@interface ViewController ()
@property (strong, nonatomic) SMCalloutView *calloutView;
@property (strong, nonatomic) UIView *emptyCalloutView;
@end

初始化SMCalloutView添加一个按钮,然后创建一个空的UIView:

- (void)viewDidLoad
{
    /* all your other view init, settings, etc... */

    self.calloutView = [[SMCalloutView alloc] init];
    self.calloutView.hidden = YES;

    UIButton *button = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
    [button addTarget:self
               action:@selector(calloutAccessoryButtonTapped:)
     forControlEvents:UIControlEventTouchUpInside];
    self.calloutView.rightAccessoryView = button;

    self.emptyCalloutView = [[UIView alloc] initWithFrame:CGRectZero];
}

我们必须画,空的UIView为了满足地图SDK,但我们将显示视图SMCalloutView。 我也设置一个可重用的垂直偏移callout视图。

添加委托方法来处理信息窗口调用:

#pragma mark - GMSMapViewDelegate

- (UIView *)mapView:(GMSMapView *)mapView markerInfoWindow:(GMSMarker *)marker {
    CLLocationCoordinate2D anchor = marker.position;

    CGPoint point = [mapView.projection pointForCoordinate:anchor];

    self.calloutView.title = marker.title;

    self.calloutView.calloutOffset = CGPointMake(0, -CalloutYOffset);

    self.calloutView.hidden = NO;

    CGRect calloutRect = CGRectZero;
    calloutRect.origin = point;
    calloutRect.size = CGSizeZero;

    [self.calloutView presentCalloutFromRect:calloutRect
                                      inView:mapView
                           constrainedToView:mapView
                                    animated:YES];

    return self.emptyCalloutView;
}

- (void)mapView:(GMSMapView *)pMapView didChangeCameraPosition:(GMSCameraPosition *)position {
    /* move callout with map drag */
    if (pMapView.selectedMarker != nil && !self.calloutView.hidden) {
        CLLocationCoordinate2D anchor = [pMapView.selectedMarker position];

        CGPoint arrowPt = self.calloutView.backgroundView.arrowPoint;

        CGPoint pt = [pMapView.projection pointForCoordinate:anchor];
        pt.x -= arrowPt.x;
        pt.y -= arrowPt.y + CalloutYOffset;

        self.calloutView.frame = (CGRect) {.origin = pt, .size = self.calloutView.frame.size };
    } else {
        self.calloutView.hidden = YES;
    }
}

- (void)mapView:(GMSMapView *)mapView didTapAtCoordinate:(CLLocationCoordinate2D)coordinate {
    self.calloutView.hidden = YES;
}

- (BOOL)mapView:(GMSMapView *)mapView didTapMarker:(GMSMarker *)marker {
    /* don't move map camera to center marker on tap */
    mapView.selectedMarker = marker;
    return YES;
}

处理触摸调出按钮,这里使用一个警报视图标题标记和代码片段:

- (void)calloutAccessoryButtonTapped:(id)sender {
    if (mapView_.selectedMarker) {
        GMSMarker *marker = mapView_.selectedMarker;
        //NSDictionary *userData = marker.userData;

        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:marker.title
                                                            message:marker.snippet
                                                           delegate:nil
                                                  cancelButtonTitle:@"OK"
                                                  otherButtonTitles:nil];
        [alertView show];
    }
}

显然,确保你ViewController听GMSMapViewDelegate(. h):

@interface ViewController : UIViewController <GMSMapViewDelegate>

基本上,应该工作。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

朝着阳光飞去

鼓励鼓励我吧 ,让我继续加油

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值