在 Google Maps SDK for native iOS/objective C 中的 InfoWindow/Marker 上添加 Click 事件
Posted
技术标签:
【中文标题】在 Google Maps SDK for native iOS/objective C 中的 InfoWindow/Marker 上添加 Click 事件【英文标题】:Adding Click Event on InfoWindow/Marker in Google Maps SDK for native iOS/objective C 【发布时间】:2013-02-27 22:07:51 【问题描述】:简单的问题
我正在开发一款适用于 ios 的应用程序,我在其中嵌入了适用于原生 iOS 的新 Google 地图。 一切都很好,除了一个问题,我在这里和谷歌都找不到原生 IOS/Objective-C 的合适解决方案(如果有,请告诉我,抱歉打扰你)
我想要的: 我希望用户单击打开信息窗口的标记。如果他单击或点击信息窗口后,它应该会打开一个包含更多信息的新 UIView。
我该怎么做?
感谢您的建议
【问题讨论】:
这叫注解披露:***.com/questions/1433067/… 接受的答案与 Apple MapKit 相关,而不是 Google Maps?! 是的,你是对的。无论如何,这是一个旧问题的旧答案:D 对于googlemap,infowindow渲染为UIImage,无法在infowindow内部添加交互,本博客解决问题:kevinxh.github.io/swift/… 【参考方案1】: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:
中使用它
【讨论】:
非常酷,您指出使用 userData。我是 iOS 和 Map SDK 的新手,不知道如何使用 userData。现在我明白了。谢谢! (我的案例研究:我将 userdata url 设置为来自我的 REST 服务的对象的详细视图,这是在进一步视图中处理的标记源。) 这应该是选择的答案。谢谢@Brian 这应该是需要选对的答案。完美! 这是正确的答案 我将此作为选定的答案。【参考方案2】:你添加地图的地方,添加
mapView_.delegate=self;
那就用这个
-(void)mapView:(GMSMapView *)mapView
didTapInfoWindowOfMarker:(id<GMSMarker>)marker
//info window tapped
【讨论】:
【参考方案3】:swift 4 的完整答案
将GMSMapViewDelegate
添加为委托
像这样设置你的地图委托:googlemap.delegate = self
添加这个函数
func mapView(_ mapView: GMSMapView, didTap marker: GMSMarker) -> Bool // do something return true
【讨论】:
【参考方案4】:我正在使用适用于 iOS 的 Google Maps SDK。
我将 uiview 子类化为信息窗口创建自定义视图“InfoWindow”。
添加@property (nonatomic,retain) UIView *actionOverlayCalloutView;在您的视图控制器的头文件中。
- (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];
【讨论】:
【参考方案5】:斯威夫特 5.1
您可以将GMSMapViewDelegate
用于:
mapView.delegate = self
func mapView(_ mapView: GMSMapView, didTapInfoWindowOf marker: GMSMarker)
print("InfoView tapped")
【讨论】:
【参考方案6】:创建一个要在 infoWindow 中显示的子视图。
设置subview的frame等于infoWindow view的frame。
subView = [[[NSBundle mainBundle] loadNibNamed:@"viewName" owner:self options:nil] objectAtIndex:0];
[subView setFrame:infoview.frame];
[self.mapview addSubview:subView];
【讨论】:
【参考方案7】:在谷歌地图查看信息获取信息
func mapView(_ mapView: GMSMapView, didTapInfoWindowOf marker: GMSMarker)
print(marker.title!)
print(marker.snippet!)
【讨论】:
以上是关于在 Google Maps SDK for native iOS/objective C 中的 InfoWindow/Marker 上添加 Click 事件的主要内容,如果未能解决你的问题,请参考以下文章
GMSCoordinateBounds 包括坐标在 Google Maps SDK for iOS 中无法正常工作
例外:Google Maps SDK for iOS 必须在使用前通过 [GMSServices provideAPIKey:...] 进行初始化
在 Google Maps SDK for native iOS/objective C 中的 InfoWindow/Marker 上添加 Click 事件
“Maps SDK for Android v.3.0.0 BETA”能否在没有 Google Play 服务的设备上运行?
Maps SDK for Android v.3.0.0 BETA "是否可以在没有Google Play服务的设备上运行?