Google Maps SDK中的自定义注释视图

smi*_*ily 3 google-maps-sdk-ios

我创建了一个基于地图的iOS应用程序,其中我认为使用谷歌地图SDK for iOS而不是Mapkit,我找到了文档,但我没有找到与自定义注释视图相关的方法,任何人都可以为我提供如何解决方案创建自定义注释视图(信息窗口)以及如何为其添加内容(标题,代码段).

fri*_*nny 12

我不知道你们,但我发现谷歌的渲染UIView信息窗口有点限制.使用SMCalloutViewRyan Maxwell的示例项目,可以呈现更多交互式视图.

这适用于Google Maps SDK v1.8.1,截至2014年6月10日.

Google地图上的默认SMCalloutView

首先,做一些设置:

#import <SMCalloutView/SMCalloutView.h>

static const CGFloat CalloutYOffset = 10.0f;

@interface ViewController ()
@property (strong, nonatomic) SMCalloutView *calloutView;
@property (strong, nonatomic) UIView *emptyCalloutView;
@end
Run Code Online (Sandbox Code Playgroud)

初始化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];
}
Run Code Online (Sandbox Code Playgroud)

我们必须将其绘制UIView为满足Maps 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;
}
Run Code Online (Sandbox Code Playgroud)

处理标注按钮,此处使用带标记标题和片段的警报视图:

- (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];
    }
}
Run Code Online (Sandbox Code Playgroud)

显然,请确保您的ViewController(.h)侦听GMSMapViewDelegate:

@interface ViewController : UIViewController <GMSMapViewDelegate>
Run Code Online (Sandbox Code Playgroud)

这基本上应该有效.有关完整的xcode项目,请参阅Ryan Maxwell 的上述示例.


ton*_*y m 10

如果您在GoogleMaps.Framework中检查GMSMapView.h,您可以看到下面的方法,它可以让您为标记添加自定义信息窗口,而不是单独使用标准标题和代码段:

注意你(op)说annotationView = infoWindow
BUT NORMAL:annotationView = marker本身和calloutView = infoWindow

/**
 * 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;
Run Code Online (Sandbox Code Playgroud)