9

使用 Google Maps for iOS SDK,“我的位置”按钮默认位于右下角:

在此处输入图像描述

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

让我感到困惑的部分是我应该为“我的位置”视图的框架和/或边界赋予什么值,以便将它放在我想要的位置。对于初学者来说,我的位置按钮目前有frame.origin.x=-74frame.origin.y= -54。这是我第一次看到 aframe.origin.x或 a 的负坐标,frame.origin.y我什至不确定 iOS 如何处理负坐标。我的第一个想法是 egframe.origin.x = -74相当于[view superview].frame.size.width - 74,即从 superview 的宽度或高度中减去负值。但后来我查看了超级视图的宽度和高度,它们都是0.0. 这是我的代码,它输出有关地图和我的位置按钮框架和边界的一些信息:

- (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

作为一个简单的测试,我尝试将“我的位置”按钮放置在左上角

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

但后来我的位置按钮根本没有显示。

我还尝试了对现有值的小修改(例如frame.origin.x-76.0to更改-66.0并可以看到位置的差异,所以至少我有信心我正在修改正确视图的位置。我仍然不明白我)如何负坐标有效,ii)如何在这种特定情况下正确定位视图。在阅读了这个问题的答案后,我认为我对视图框架和边界有一个合理的掌握,但鉴于我还没有让它工作,显然没有。

4

6 回答 6

20

解决此问题的最简单方法是在创建地图视图后立即更改按钮的框架和自动调整大小掩码。

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

编辑:对不起,我把按钮放在右上角。更新了我的代码以将其放置在左下角。

注意:目前没有来自 Google 的 API 来获取位置按钮。第一行可能在 SDK 的未来版本中不起作用。

您还应该在此处创建功能请求。

另一种选择是自己创建按钮并将其作为子视图添加到地图中。按钮处理程序代码相当简单:

  CLLocation *location = mapView_.myLocation;
  if (location) {
    [mapView_ animateToLocation:location.coordinate];
  }
于 2013-06-02T08:37:43.000 回答
10

您可以使用 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;

斯威夫特 3

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

在此处输入图像描述

于 2015-09-30T11:48:43.030 回答
6

在迅速 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 
                    }
                }
            }
        }
    }
于 2016-10-05T14:33:57.513 回答
2
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-02-10T11:38:04.583 回答
0

截至 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;
            }
        }

    }
};
于 2015-07-17T15:05:57.730 回答
0
for (UIView *object in _mapView.subviews) {
    if([[[object class] description] isEqualToString:@"GMSUISettingsPaddingView"] )
    {
        for (UIView *settingView in object.subviews) {
            if([[[settingView class] description] isEqualToString:@"GMSUISettingsView"] )
            {
                for(UIView *view in settingView.subviews) {
                    if([[[view class] description] isEqualToString:@"GMSx_QTMButton"] ) {
                        CGRect frame = view.frame;
                        frame.origin.y -= 60;
                        view.frame = frame;
                    }
                }
            }
        };

    }
}
于 2018-03-09T09:12:20.180 回答