Google Maps iOS SDK将“我的位置”button移动到左下angular

使用Google Maps for iOS SDK,“我的位置”button默认放置在右下angular:

在这里输入图像说明

我想把它放在左下angular(是的,我意识到我需要小心不要模糊那里的“Google”标志)。 我不相信SDK有这样做的“官方”方式,但基于这个答案,我已经想出了如何获得我的位置button子视图,以便我可以定位它。

有我困惑的部分是我应该给我的位置视图的框架和/或界限,以获得我想要它的值。 对于初学者来说,我现在所处的“我的位置”button有frame.origin.x = -74frame.origin.y = -54 。 这是我第一次看到frame.origin.xframe.origin.y负坐标,我甚至不知道iOS如何处理负坐标。 我的第一个想法是,例如frame.origin.x = -74相当于[view superview].frame.size.width - 74 ,即负值从[view superview].frame.size.width - 74的宽度或高度减去。 但后来我看了超级视图的宽度和高度,他们都是0.0 。 这里是我的代码输出关于地图和我的位置button框架和边界的一些信息:

 - (void)loadView { GMSCameraPosition *cam = [GMSCameraPosition cameraWithLatitude:jcuTownsvilleCenterCampusLat longitude:jcuTownsvilleCenterCampusLon zoom:17]; self.campusMap = [GMSMapView mapWithFrame:CGRectZero camera:cam]; self.campusMap.myLocationEnabled = YES; self.campusMap.settings.myLocationButton = YES; self.view = self.campusMap; for (UIView *view in self.campusMap.subviews) { NSLog(@"view.description: %@",view.description); if ([view isKindOfClass:[UIButton class]]) { // these four values in the conditional below are just what happen to // be the values corresponding to the "my location button" in Google Maps // for iOS SDK version 1.3.0.3430. They might change over time, so this // code is somewhat fragile. if (view.frame.size.width == 76 && view.frame.size.height == 54 && view.frame.origin.x == -76 && view.frame.origin.y == -54) { NSLog(@"we may have found the 'my location' button"); NSLog(@"self.campusMap coord stats:"); NSLog(@"bounds.origin.x: %f", self.campusMap.bounds.origin.x); NSLog(@"bounds.origin.y: %f", self.campusMap.bounds.origin.y); NSLog(@"bounds.size.width: %f", self.campusMap.bounds.size.width); NSLog(@"bounds.size.height: %f", self.campusMap.bounds.size.height); NSLog(@"frame.origin.x: %f", self.campusMap.frame.origin.x); NSLog(@"frame.origin.y: %f", self.campusMap.frame.origin.y); NSLog(@"frame.size.width: %f", self.campusMap.frame.size.width); NSLog(@"frame.size.height: %f", self.campusMap.frame.size.height); NSLog(@"view coord stats:"); NSLog(@"bounds.origin.x: %f", view.bounds.origin.x); NSLog(@"bounds.origin.y: %f", view.bounds.origin.y); NSLog(@"bounds.size.width: %f", view.bounds.size.width); NSLog(@"bounds.size.height: %f", view.bounds.size.height); NSLog(@"frame.origin.x: %f", view.frame.origin.x); NSLog(@"frame.origin.y: %f", view.frame.origin.y); NSLog(@"frame.size.width: %f", view.frame.size.width); NSLog(@"frame.size.height: %f", view.frame.size.height); } } } } 

这里是输出:

 self.campusMap coord stats: bounds.origin.x: 0.000000 bounds.origin.y: 0.000000 bounds.size.width: 0.000000 bounds.size.height: 0.000000 frame.origin.x: 0.000000 frame.origin.y: 0.000000 frame.size.width: 0.000000 frame.size.height: 0.000000 view coord stats: bounds.origin.x: 0.000000 bounds.origin.y: 0.000000 bounds.size.width: 76.000000 bounds.size.height: 54.000000 frame.origin.x: -76.000000 frame.origin.y: -54.000000 frame.size.width: 76.000000 frame.size.height: 54.000000 

我尝试了一个简单的testing,在左上angular的“我的位置”button的位置是:

 CGRect frame = view.frame; frame.origin.x = 0; frame.origin.y = 0; frame.size.width = 76; frame.size.height = 54; [view setFrame:frame]; 

但是我的位置button根本没有显示。

我也尝试了对现有值的小修改(例如,将frame.origin.x-76.0更改为-76.0 ,并且可以看到不同的位置,所以至less我相信我正在修改正确视图的位置。我仍然不明白i)负面坐标的工作原理,以及ii)如何在这个特定场景中正确定位视图。 在阅读了这个问题的答案之后,我认为我对视图框架和界限有了一个合理的把握,但是鉴于我还没有得到这个工作,显然不是。

解决这个问题的最简单的方法是在创build地图视图之后立即更改button的框架和自动追踪掩码。

 UIButton* myLocationButton = (UIButton*)[[mapView_ subviews] lastObject]; myLocationButton.autoresizingMask = UIViewAutoresizingFlexibleRightMargin|UIViewAutoresizingFlexibleTopMargin; CGRect frame = myLocationButton.frame; frame.origin.x = 5; myLocationButton.frame = frame; 

编辑 :对不起,我把button放在右上angular。 更新我的代码,将其放在左下angular。

请注意:Google目前没有获取位置button的API。 第一行可能无法在将来的SDK版本中使用。

你也应该在这里创build一个function请求。

另一种select是自己创buildbutton,并将其作为子视图添加到地图中。 button处理程序代码相当简单:

  CLLocation *location = mapView_.myLocation; if (location) { [mapView_ animateToLocation:location.coordinate]; } 

您可以使用GMSMapView的填充。

 GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:-37.81969 longitude:144.966085 zoom:4]; _mapView = [GMSMapView mapWithFrame:CGRectZero camera:camera]; _mapView.settings.myLocationButton = YES; _mapView.myLocationEnabled = YES; _mapView.padding = UIEdgeInsetsMake(0, 0, kOverlayHeight, 0); self.view = _mapView; 

SWIFT 3

 self._mapView.padding = UIEdgeInsets(top: 0, left: 0, bottom: 10, right: 0) 

在这里输入图像说明

在迅速3 xcode 8

 func moveLocationButton() -> Void{ for object in mapView.subviews{ for obj in object.subviews{ if let button = obj as? UIButton{ let name = button.accessibilityIdentifier if(name == "my_location"){ //config a position button.center = self.view.center } } } } } 
 for (UIView *object in mapView_.subviews) { if([[[object class] description] isEqualToString:@"GMSUISettingsView"] ) { for(UIView *view in object.subviews) { if([[[view class] description] isEqualToString:@"UIButton"] ) { CGRect frame = view.frame; frame.origin.y -= 60; view.frame = frame; } } } }; 

截至2015年7月,这是一个办法:

 for (UIView *object in mapView_.subviews) { if([[[object class] description] isEqualToString:@"GMSUISettingsView"] ) { for(UIView *view in object.subviews) { if([[[view class] description] isEqualToString:@"GMSx_QTMButton"] ) { CGRect frame = view.frame; frame.origin.y -= 60; view.frame = frame; } } } };