谷歌地图iOS SDK移动“我的位置”按钮到左下角 [英] Google Maps iOS SDK move "My Location" button to bottom left hand corner

查看:849
本文介绍了谷歌地图iOS SDK移动“我的位置”按钮到左下角的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Using the Google Maps for iOS SDK, the "My Location" button is by default placed in the bottom right hand corner:

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

I'd like to place it in the bottom left hand corner (yes, I realize I need to be careful not to obscure the "Google" logo there). I don't believe the SDK has an "official" way of doing this, but based on this answer I have figured out how to get a hold of the My Location button subview so that I can position it.

令我困惑的部分是我应该给我的位置的值视图的框架和/或边界,以获得它我想要的地方。对于初学者来说,目前的My Location按钮有 frame.origin.x = -74 frame.origin.y = -54 。这是我第一次看到 frame.origin.x frame.origin.y 我甚至不确定iOS如何处理负坐标。我的第一个念头就是 frame.origin.x = -74 相当于 [查看超级视图] .frame.size.width - 74 ,即从超视图的宽度或高度中减去负值。但后来我查看了superview的宽度和高度,它们都是 0.0 。这是我的代码,它输出有关地图和我的位置按钮框架和边界的一些信息:

The part that has me confused is what values I should be giving to the My Location view's frame and/or bounds to get it where I want it. For starters, the My Location button as it currently stands has frame.origin.x = -74 and frame.origin.y = -54. This is the first time I've seen negative coordinates for a frame.origin.x or a frame.origin.y and I'm not even sure how iOS handles negative coordinates. My first thought was that e.g. frame.origin.x = -74 is equivalent to [view superview].frame.size.width - 74, i.e. negative value are subtracted from the width or height of the superview. But then I looked at the width and height of the superview and they're both 0.0. Here's my code which outputs some information about both the map and the my location button frames and bounds:

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

我尝试了一个简单的测试,将我的位置按钮放在顶部左上角,并显示:

I tried as a simple test to position the "My Location" button in the top left hand corner with:

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

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

but then the My Location button didn't show at all.

我还尝试对现有值进行小修改(例如从<$ c更改 frame.origin.x $ c> -76.0 -66.0 并且可以看到位置上的差异,所以至少我有信心我正在修改位置正确的观点。我仍然不明白i)负坐标如何工作以及ii)如何在这个特定场景中正确定位视图。在阅读这个问题的答案后,我认为我对视图有了合理的把握框架和边界,但鉴于我还没有使用它,显然不是。

I have also tried small modifications to the existing values (e.g. changing frame.origin.x from -76.0 to -66.0 and can see the difference in position, so at least I'm confident I'm modifying the position of the right view. I still don't understand i) how negative coordinates work and ii) how to properly position the view in this specific scenario though. After reading the answers to this question I thought I had a reasonable grasp on view frames and bounds, but given that I haven't gotten this to work yet, apparently not.

推荐答案

最简单的解决方法这是在创建地图视图后立即更改按钮的框架和自动调整遮罩。

The easiest way to solve this is to change the frame and autoresizing mask of the button right after the map view is created.

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

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

EDIT: Sorry, I placed the button in the top right corner. Updated my code to place it in the bottom left hand corner.

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

您还应该创建一个功能请求此处

You also should create a feature request here.

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

Another option is to create the button yourself and add it as a subview to the map. The button handler code is fairly easy:

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

这篇关于谷歌地图iOS SDK移动“我的位置”按钮到左下角的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆