我不知道你们怎么想,但我发现 Google 渲染的 UIView 信息窗口有点限制。使用SMCallout视图 https://github.com/nfarina/calloutview and Ryan Maxwell 的示例项目 https://github.com/ryanmaxwell/GoogleMapsCalloutView,可以呈现更多交互式视图。
截至 2014 年 6 月 10 日,这适用于 Google Maps SDK v1.8.1。
首先,做一些设置:
#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
以满足Maps SDK的要求,但我们将显示的视图是SMCalloutView
。我还为标注视图设置了可重用的垂直偏移。
添加委托方法来处理信息窗口调用:
#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(.h) 监听 GMSMapViewDelegate:
@interface ViewController : UIViewController <GMSMapViewDelegate>
这基本上应该有效。完整的xcode项目请参见前述example https://github.com/ryanmaxwell/GoogleMapsCalloutView来自瑞恩·麦克斯韦。