我目前正在使用google maps ios SDK创建并返回自定义视图,方法是将委托设置为self并使用以下代码。
#pragma mark - GMSMapViewDelegate
-(UIView*)mapView:(GMSMapView *)mapView markerInfoWindow:(id<GMSMarker>)marker {
int popupWidth = 200;
int contentWidth = 180;
int contentPad = 10;
int popupHeight = 140;
int popupBottomPadding = 16;
int popupContentHeight = popupHeight - popupBottomPadding;
int buttonHeight = 30;
UIView *outerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, popupWidth, popupHeight)];
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, popupWidth, popupContentHeight)];
[view setBackgroundColor:[UIColor whiteColor]];
UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(contentPad, 0, contentWidth, 22)];
[titleLabel setFont:[UIFont systemFontOfSize:17.0]];
titleLabel.text = [marker title];
UILabel *descriptionLabel = [[UILabel alloc] initWithFrame:CGRectMake(contentPad, 24, contentWidth, 20)];
[descriptionLabel setFont:[UIFont systemFontOfSize:11.0]];
descriptionLabel.text = [marker snippet];
[view addSubview:titleLabel];
[view addSubview:descriptionLabel];
UIButton *directionButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
directionButton.frame = CGRectMake(contentPad, 45, contentWidth, buttonHeight);
[directionButton setTitle:@"Directions" forState:UIControlStateNormal];
[directionButton addTarget:self action:@selector(directionsPressed) forControlEvents:UIControlEventTouchDown];
UIButton *viewLocationButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[viewLocationButton addTarget:self action:@selector(viewLocationPressed) forControlEvents:UIControlEventTouchUpInside];
[viewLocationButton setTitle:@"View Location" forState:UIControlStateNormal];
viewLocationButton.frame = CGRectMake(contentPad, 80, contentWidth, buttonHeight);
// handle bottom dealio
UIImage *bottomImage = [UIImage imageNamed:@"map-pointer-bottom"];
UIImageView *bottomView = [[UIImageView alloc] initWithFrame:CGRectMake((popupWidth / 2) - (bottomImage.size.width / 2), (popupContentHeight), bottomImage.size.width, bottomImage.size.height)];
[bottomView setImage:bottomImage];
[outerView addSubview:view];
[outerView addSubview:bottomView];
[outerView addSubview:directionButton];
[outerView addSubview:viewLocationButton];
ListItem *li = (ListItem*)[marker userData];
self.currentItem = li;
NSLog(@"List Item %@ - %@", li.type, li.typeid);
return outerView;
}
-(void)directionsPressed {
NSLog(@"Directions Pressed");
}
-(void)viewLocationPressed {
NSLog(@"Location View Pressed");
}
- (void)mapView:(GMSMapView *)mapView didTapInfoWindowOfMarker:(id<GMSMarker>)marker {
NSLog(@"Tap Captured");
}
当我点击自定义视图时,didTapWindowOfMarker被触发,但是没有触发按钮的目标方法。
为什么会出现这种情况的想法?
答案 0 :(得分:23)
正如Google Maps Android API文档中正式提到的,以下关于 infowindows 的限制也适用于Google Maps iOS SDK:
信息窗口不是实时视图,而是视图在地图上呈现为图像。因此,您在视图上设置的任何侦听器都将被忽略,并且您无法区分视图各个部分上的单击事件。建议您不要在自定义信息窗口中放置交互式组件(如按钮,复选框或文本输入)。
所以基本上点击infowindow的任何部分只会触发“ didTapWindowOfMarker ”
答案 1 :(得分:9)
Swift 3.0解决方案
//empty the default infowindow
func mapView(_ mapView: GMSMapView, markerInfoWindow marker: GMSMarker) -> UIView? {
return UIView()
}
// reset custom infowindow whenever marker is tapped
func mapView(_ mapView: GMSMapView, didTap marker: GMSMarker) -> Bool {
customInfoView.removeFromSuperview()
// customInfoView.button.addTarget(self, action: #selector(buttonTapped(_:)), for: .touchUpInside)
self.view.addSubview(customInfoView)
// Remember to return false
// so marker event is still handled by delegate
return false
}
// let the custom infowindow follows the camera
func mapView(_ mapView: GMSMapView, didChange position: GMSCameraPosition) {
if (locationMarker != nil){
let location = locationMarker.position
customInfoView.center = mapView.projection.point(for: location)
}
}
// take care of the close event
func mapView(_ mapView: GMSMapView, didTapAt coordinate: CLLocationCoordinate2D) {
customInfoView.removeFromSuperview()
}
并将此视图(customInfoWindow)的出口放在具有mapView的同一控制器中。
感谢这位开发人员Custom and interactive googlemaps(IOS SDK) infowindow
,我从这个链接中得到了这个想法答案 2 :(得分:2)
我有一个UView,我正在为视图添加一个委托,以便UIButton调用一个选择器。对于谷歌地图,我打电话
时什么都不做- (void) mapView:(GMSMapView *)mapView didTapInfoWindowOfMarker:(GMSMarker *)marker;
但是,我将我的infowindow(我的自定义视图)委托设置为self,并在按下链接到选择器的按钮时调用我的所有操作。
答案 3 :(得分:1)
<强>更新强>
以下是我使用的代码,用于检测添加到infoWindows的按钮。我创建了一个带有2个假按钮的自定义infoWindow(它们实际上可以替换为图像因为它们不会触发任何动作)并且我在infoWindow上添加了一个完全透明的覆盖图,其中包含2个真正的按钮。这些按钮将触发操作。
我使用一些委托方法或KVC,以便在移动infoWindow时移动叠加层。
- (UIView *)mapView:(GMSMapView *)mapView markerInfoWindow:(GMSMarker *)marker {
[self.actionOverlayCalloutView removeFromSuperview];
UIView *calloutView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, infoWindowWidth, infoWindowHeight)];
float offset = anchorSize * M_SQRT2;
CGAffineTransform rotateBy45Degrees = CGAffineTransformMakeRotation(M_PI_4);
UIView *arrow = [[UIView alloc] initWithFrame:CGRectMake((infoWindowWidth - anchorSize)/2.0, infoWindowHeight - offset, anchorSize, anchorSize)];
arrow.transform = rotateBy45Degrees;
arrow.backgroundColor = [UIColor lightGrayColor];
[calloutView addSubview:arrow];
UIView *contentView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, infoWindowWidth, infoWindowHeight - offset/2)];
[contentView setBackgroundColor:[UIColor whiteColor]];
contentView.layer.cornerRadius = 5;
contentView.layer.masksToBounds = YES;
contentView.layer.borderColor = [UIColor lightGrayColor].CGColor;
contentView.layer.borderWidth = 1.0f;
self.actionOverlayCalloutView =
[NSKeyedUnarchiver unarchiveObjectWithData:[NSKeyedArchiver archivedDataWithRootObject:contentView]]; //hack to copy a view...
self.actionOverlayCalloutView.backgroundColor = [UIColor lightGrayColorWithAlpha:0.5];
self.actionOverlayCalloutView.layer.cornerRadius = 5;
NSMutableArray *falseButtons = [NSMutableArray array];
NSMutableArray *actionButtons = [NSMutableArray array];
PointMapItem *pointAnnotation = marker.userData;
if ([pointAnnotation canPerformSend]) {
UIButton *button = [[UIButton alloc] init];
[button setImage:[UIImage imageNamed:@"imageButton1.png"] forState:UIControlStateNormal];
[falseButtons addObject:button];
UIButton *activableButton = [[UIButton alloc] init];
[activableButton addTarget:self action:@selector(onButton1Clicked) forControlEvents:UIControlEventTouchUpInside];
[actionButtons addObject:activableButton];
}
if ([pointAnnotation canPerformShowDetails]) {
UIButton *button = [[UIButton alloc] init];
[button setImage:[UIImage imageNamed:@"imageButton1.png"] forState:UIControlStateNormal];
[falseButtons addObject:button];
UIButton *activableButton = [[UIButton alloc] init];
[activableButton addTarget:self action:@selector(onButton2Clicked) forControlEvents:UIControlEventTouchUpInside];
[actionButtons addObject:activableButton];
}
int buttonWidth = contentView.frame.size.width / [falseButtons count];
int currentOffset = 0;
for (int i=0; i<falseButtons.count; i++) {
UIButton *falseButton = [falseButtons objectAtIndex:i];
UIButton *activableButton = [actionButtons objectAtIndex:i];
[falseButton setFrame:CGRectMake(currentOffset, 0, buttonWidth, contentView.frame.size.height)];
currentOffset += buttonWidth;
activableButton.frame = falseButton.frame;
[activableButton setTitle:@"" forState:UIControlStateNormal];
[self.actionOverlayCalloutView addSubview:activableButton];
[contentView addSubview:falseButton];
}
[calloutView addSubview:contentView];
CLLocationCoordinate2D anchor = [self.mapView.selectedMarker position];
CGPoint point = [self.mapView.projection pointForCoordinate:anchor];
point.y -= self.mapView.selectedMarker.icon.size.height + offset/2 + (infoWindowHeight - offset/2)/2;
self.actionOverlayCalloutView.center = point;
[self.mapView addSubview:self.actionOverlayCalloutView];
return calloutView;
}
- (void)mapView:(GMSMapView *)pMapView didChangeCameraPosition:(GMSCameraPosition *)position {
if (pMapView.selectedMarker != nil && self.actionOverlayCalloutView.superview) {
CLLocationCoordinate2D anchor = [self.mapView.selectedMarker position];
CGPoint point = [self.mapView.projection pointForCoordinate:anchor];
float offset = anchorSize * M_SQRT2;
point.y -= self.mapView.selectedMarker.icon.size.height + offset/2 + (infoWindowHeight - offset/2)/2;
self.actionOverlayCalloutView.center = point;
} else {
[self.actionOverlayCalloutView removeFromSuperview];
}
}
- (void)mapView:(GMSMapView *)mapView didTapAtCoordinate:(CLLocationCoordinate2D)coordinate {
[self.actionOverlayCalloutView removeFromSuperview];
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
if ([keyPath isEqualToString:@"mapView.selectedMarker"]) {
if (!self.mapView.selectedMarker) {
[self.actionOverlayCalloutView removeFromSuperview];
}
}
}
- (void)onButton2Clicked {
//your code
self.mapView.selectedMarker = nil;
}
- (void)onButton1Clicked {
// your code;
self.mapView.selectedMarker = nil;
}
前诉:
看看这个帖子,特别是#9,应该会有所帮助 https://code.google.com/p/gmaps-api-issues/issues/detail?id=4961
答案 4 :(得分:-2)
1)创建一个要在infoWindow中显示的子视图。
2)设置子视图的帧等于infoWindow视图的帧。
[subView setFrame:infoview.frame];
subView = [[[NSBundle mainBundle] loadNibNamed:@"viewName" owner:self options:nil] objectAtIndex:0];
[self.mapview addSubview:subView];