谷歌地图iOS SDK移动"我的位置"按钮到左下角

时间:2013-06-02 04:10:05

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

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

enter image description here

我想将它放在左下角(是的,我意识到我需要注意不要在那里隐藏“Google”徽标)。我不相信SDK有“官方”方式这样做,但基于this answer我已经弄清楚如何抓住我的位置按钮子视图,以便我可以定位它。

让我感到困惑的部分是我应该给“我的位置”视图的框架和/或界限赋予它我想要的值。对于初学者,当前的“我的位置”按钮有frame.origin.x = -74frame.origin.y = -54。这是我第一次看到frame.origin.xframe.origin.y的负坐标,我甚至不确定iOS如何处理负坐标。我的第一个念头就是frame.origin.x = -74相当于[view superview].frame.size.width - 74,即从超视图的宽度或高度中减去负值。但后来我查看了超视图的宽度和高度,它们都是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.0更改为-66.0并且可以看到位置上的差异,所以至少我对自己有信心m修改右视图的位置。我仍然不明白i)负坐标如何工作以及ii)如何在这个特定场景中正确定位视图。在阅读this question的答案后,我认为我对视图框架和边界有一个合理的把握,但鉴于我还没有让它工作,显然不是。

6 个答案:

答案 0 :(得分: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的未来版本中使用。

您还应创建功能请求here

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

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

答案 1 :(得分:9)

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

enter image description here

答案 2 :(得分:5)

在swift 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 
                    }
                }
            }
        }
    }

答案 3 :(得分: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;
            }
        }

    }
};

答案 4 :(得分: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;
            }
        }

    }
};

答案 5 :(得分:0)

def collectUsingGenerics[L <: Product : Encoder : TypeTag, R <: Product : Encoder : TypeTag](
    left: Dataset[L],
    right: Dataset[R],
    joinCol: Column,
    groupCol: Column): Dataset[(L, List[R])] = {

    import left.sparkSession.implicits._
    import org.apache.spark.sql.functions._

    val result = left
      .join(right, joinCol)
      .select(
        groupCol.as("groupCol"),
        struct(left("*")).as("_1"),
        struct(right("*")).as("_2"))
      .groupBy($"groupCol")
      .agg(
        first($"_1").as("_1"),
        collect_list($"_2").as("_2")
      )
      .drop($"groupCol")

    //This does not Work!!!
    //result.as[Result[L]]
    result.as[(L,List[R])]
  }
相关问题