ios - Google Maps iOS SDK 将 "My Location"按钮移至左下角

标签 ios objective-c google-maps-sdk-ios

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

enter image description here

我想将它放在左下角(是的,我意识到我需要小心不要遮挡那里的“Google” Logo )。我不相信 SDK 有执行此操作的“官方”方式,但基于 this answer我已经弄清楚如何获取“我的位置”按钮 subview 以便定位它。

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

- (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.0 更改为 -66.0 并且可以看到位置上的差异,所以至少我有信心我正在修改正确 View 的位置。我仍然不明白 i) 负坐标如何工作以及 ii) 如何在这个特定场景中正确定位 View .阅读 this question 的答案后我以为我对 View 框架和边界有一个合理的把握,但考虑到我还没有让它工作,显然不是。

最佳答案

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

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

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

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

您还应该创建一个功能请求 here .

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

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

关于ios - Google Maps iOS SDK 将 "My Location"按钮移至左下角,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16879694/

相关文章:

iphone - 如何在 iOS 中插入和覆盖音频文件

iphone - NSMutableString appendString 生成一个 SIGABRT

ios - 在 ios 的 google maps sdk 中使用名称搜索位置

iphone - 输入文本时在 UIAlertView 中启用按钮 - 多个警报 View

ios - Ios 谷歌地图完成滚动的委托(delegate)方法是什么?

ios - 更改谷歌地图iOS sdk上的默认位置

javascript - 三个 js 场景的 UIWebView Javascript 垃圾收集

ios - CBCentralManager setNotifyValue : not working

ios - 如何在 segue 之后设置 UILabel 的值?

iphone - if 语句后的方向