在Google Maps SDK中的InfoWindow / Marker上添加Click事件,以实现本机iOS / Objective C

简单的问题

我正在开发一个适用于iOS的应用程序,其中embedded了用于本机iOS的新Google Map。 一切工作正常,除了一个问题,我不能find一个本地IOS / Objective-C的propper解决scheme既不在这里,也不在谷歌(如果有一个请给我看,对不起,烦你)

我想要什么:我希望用户点击打开信息窗口的标记。 之后如果他点击或点击信息窗口,它应该打开一个新的UIView进一步的信息。

我怎样才能做到这一点?

感谢您的build议

1.符合GMSMapViewDelegate协议。

 @interface YourViewController () <GMSMapViewDelegate> // your properties @end 

2.设置你的mapView_委托。

 mapView_.delegate = self; 

3.实现GMSMapViewDelegate方法

 - (void)mapView:(GMSMapView *)mapView didTapInfoWindowOfMarker:(GMSMarker *)marker { // your code } 

顺便说一句, marker.userData是有用的。 你可以设置你需要的数据并在- mapView:didTapInfoWindowOfMarker:使用它- mapView:didTapInfoWindowOfMarker:

添加地图的地方添加

  mapView_.delegate=self; 

然后使用这个

 -(void)mapView:(GMSMapView *)mapView didTapInfoWindowOfMarker:(id<GMSMarker>)marker{ //info window tapped } 

我正在使用适用于iOS的Google Maps SDK。

我已经subclassed uiview为infowindow创build自定义视图“InfoWindow”。

添加@property(nonatomic,保留)UIView * actionOverlayCalloutView; 在你的viewcontroller的头文件中。

 - (UIView *)mapView:(GMSMapView *)mapView markerInfoWindow:(GMSMarker *)marker { if(self.objSelectedParking != nil) { float anchorSize = 0.5f; float infoWindowWidth = 250.0f; float infoWindowHeight = 250.0f; [self.actionOverlayCalloutView removeFromSuperview]; InfoWindow *infoWindow = [[InfoWindow alloc] initWithFrame:CGRectMake(0, 0, infoWindowWidth, infoWindowHeight)]; infoWindow.lblTitle.text = self.objSelectedParking.strParkingName; infoWindow.lblDescription.text = self.objSelectedParking.strParkingDescription; infoWindow.lblAddress.text = self.objSelectedParking.strParkingAddress; infoWindow.lblPhone.text = self.objSelectedParking.strParkingPhone; infoWindow.imageViewParking.image = [UIImage imageNamed:@"parking_image_sample.jpg"]; float offset = anchorSize * M_SQRT2; self.actionOverlayCalloutView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, infoWindowWidth, infoWindowHeight - offset/2)]; [self.actionOverlayCalloutView setBackgroundColor:[UIColor clearColor]]; self.actionOverlayCalloutView.layer.cornerRadius = 5; self.actionOverlayCalloutView.layer.masksToBounds = YES; UIButton *hiddenCloseButton = [[UIButton alloc] initWithFrame:CGRectMake((infoWindow.viewContainer.frame.origin.x + infoWindow.viewContainer.frame.size.width - 30), 10, 20, 20)]; [hiddenCloseButton addTarget:self action:@selector(hiddenCloseButtonClickedInInfowindow:) forControlEvents:UIControlEventTouchUpInside]; [self.actionOverlayCalloutView addSubview:hiddenCloseButton]; UIButton *hiddenDirectionButton = [[UIButton alloc] initWithFrame:CGRectMake((infoWindow.lblAddress.frame.origin.x + infoWindow.lblAddress.frame.size.width + 5), (infoWindow.lblAddress.frame.origin.y - 15), 25, 25)]; [hiddenDirectionButton addTarget:self action:@selector(hiddenDirectionButtonClickedInInfowindow:) forControlEvents:UIControlEventTouchUpInside]; [self.actionOverlayCalloutView addSubview:hiddenDirectionButton]; UIButton *hiddenInfoButton = [[UIButton alloc] initWithFrame:CGRectMake((infoWindow.innerContainerView.frame.origin.x + infoWindow.imageViewParking.frame.origin.x + infoWindow.imageViewParking.frame.size.width + 20), (infoWindow.innerContainerView.frame.origin.y + 25), 25, 25)]; [hiddenInfoButton addTarget:self action:@selector(hiddenInfoButtonClickedInInfowindow:) forControlEvents:UIControlEventTouchUpInside]; [self.actionOverlayCalloutView addSubview:hiddenInfoButton]; UIButton *hiddenScheduleButton = [[UIButton alloc] initWithFrame:CGRectMake((infoWindow.innerContainerView.frame.origin.x + infoWindow.verticalLineSeperatorView.frame.origin.x + infoWindow.verticalLineSeperatorView.frame.size.width + 10), (infoWindow.innerContainerView.frame.origin.y + 25), 25, 25)]; [hiddenScheduleButton addTarget:self action:@selector(hiddenScheduleButtonClickedInInfowindow:) forControlEvents:UIControlEventTouchUpInside]; [self.actionOverlayCalloutView addSubview:hiddenScheduleButton]; [infoWindow addSubview:self.actionOverlayCalloutView]; CLLocationCoordinate2D anchor = [_mapView.selectedMarker position]; CGPoint point = [_mapView.projection pointForCoordinate:anchor]; point.y -= _mapView.selectedMarker.icon.size.height + offset/2 + (infoWindowHeight - offset/2)/2; self.actionOverlayCalloutView.center = point; [_mapView addSubview:self.actionOverlayCalloutView]; return infoWindow; } return nil; } -(void)mapView:(GMSMapView *)pMapView didChangeCameraPosition:(GMSCameraPosition *)position { if (pMapView.selectedMarker != nil && self.actionOverlayCalloutView.superview) { float anchorSize = 0.5f; float infoWindowHeight = 250.0f; CLLocationCoordinate2D anchor = [_mapView.selectedMarker position]; CGPoint point = [_mapView.projection pointForCoordinate:anchor]; float offset = anchorSize * M_SQRT2; point.y -= _mapView.selectedMarker.icon.size.height + offset/2 + (infoWindowHeight - offset/2)/2; point.y = point.y - 10; //PRATIK GUJARATI CODE TO ADJUST HEIGHT AND Y VALUE OF TRANSPARENT OVERLAY VIEW 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 (!_mapView.selectedMarker) { [self.actionOverlayCalloutView removeFromSuperview]; } } } 
  1. 创build一个你想在infoWindow中显示的子视图。

  2. 设置子视图的框架等同于infoWindow视图的框架。

     subView = [[[NSBundle mainBundle] loadNibNamed:@"viewName" owner:self options:nil] objectAtIndex:0]; [subView setFrame:infoview.frame]; [self.mapview addSubview:subView]; 

获取谷歌地图视图信息的信息

 func mapView(_ mapView: GMSMapView, didTapInfoWindowOf marker: GMSMarker) { print(marker.title!) print(marker.snippet!) 

}