Google Maps iOS SDK 将“我的位置”按钮移动到左下角
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16879694/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Google Maps iOS SDK move "My Location" button to bottom left hand corner
提问by Bryce Thomas
Using the Google Maps for iOS SDK, the "My Location" button is by default placed in the bottom right hand corner:
使用 Google Maps for iOS 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 answerI have figured out how to get a hold of the My Location button subview so that I can position it.
我想把它放在左下角(是的,我意识到我需要小心不要掩盖那里的“谷歌”标志)。我不相信 SDK 有一种“官方”的方式来做到这一点,但基于这个答案,我想出了如何获取“我的位置”按钮子视图以便我可以定位它。
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:
让我感到困惑的部分是我应该为“我的位置”视图的框架和/或边界赋予什么值以将其放在我想要的位置。对于初学者来说,目前的“我的位置”按钮有frame.origin.x
=-74
和frame.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);
}
}
}
}
And here is the output:
这是输出:
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 topleft 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.
但是我的位置按钮根本没有显示。
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 questionI thought I had a reasonable grasp on view frames and bounds, but given that I haven't gotten this to work yet, apparently not.
我还尝试了对现有值的小修改(例如,frame.origin.x
从-76.0
to更改-66.0
并可以看到位置的差异,所以至少我有信心我正在修改正确视图的位置。我仍然不明白我)如何负坐标工作和 ii) 如何在这个特定场景中正确定位视图。在阅读了这个问题的答案后,我认为我对视图框架和边界有一个合理的掌握,但鉴于我还没有让它起作用,显然没有。
回答by Felix
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.
编辑:对不起,我把按钮放在右上角。更新了我的代码以将其放在左下角。
Note: there is currently no API from Google to get the location button. The first line may not work in future releases of the SDK.
注意:目前没有来自 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];
}
回答by 3lvis
You can use the padding of the GMSMapView.
您可以使用 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
斯威夫特 3
self._mapView.padding = UIEdgeInsets(top: 0, left: 0, bottom: 10, right: 0)
回答by Franklin Samboni Castillo
in swift 3 xcode 8
在 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
}
}
}
}
}
回答by Spydy
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;
}
}
}
};
回答by Etienne No?l
As of July 2015, this is a way to do it:
截至 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;
}
}
}
};
回答by Vikash Rajput
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;
}
}
}
};
}
}