ios MKMapView上的当前位置按钮
我正在iOS应用程序中使用MKMapView。我的视图中有搜索栏。若我在地图上搜索位置,我会在搜索到的位置上添加注释。现在的问题是,我想回到我现在的位置。我看过谷歌地图应用程序,他们在地图上有一个按钮,可以将用户发送到当前位置ios MKMapView上的当前位置按钮,ios,iphone,mkmapview,mapkit,Ios,Iphone,Mkmapview,Mapkit,我正在iOS应用程序中使用MKMapView。我的视图中有搜索栏。若我在地图上搜索位置,我会在搜索到的位置上添加注释。现在的问题是,我想回到我现在的位置。我看过谷歌地图应用程序,他们在地图上有一个按钮,可以将用户发送到当前位置 怎么显示那个按钮?。如何获取该按钮的单击事件 解决方案1: 将UIButton拖动到情节提要中的UIViewController,并将其连接到ViewController.m中的iAction 解决方案2: 或者,您可以通过编程方式创建按钮,如下所示: UIButton
怎么显示那个按钮?。如何获取该按钮的单击事件 解决方案1: 将UIButton拖动到情节提要中的UIViewController,并将其连接到ViewController.m中的iAction 解决方案2: 或者,您可以通过编程方式创建按钮,如下所示:
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button addTarget:self
action:@selector(zoomToUserLocation)
forControlEvents:UIControlEventTouchUpInside];
[button setTitle:@"My Location" forState:UIControlStateNormal];
button.frame = CGRectMake(80.0, 210.0, 160.0, 40.0);
[view addSubview:button];
使用以下方法:
-(void)zoomToUserLocation{
MKCoordinateRegion mapRegion;
mapRegion.center = mapView.userLocation.coordinate;
mapRegion.span.latitudeDelta = 0.2;
mapRegion.span.longitudeDelta = 0.2;
[mapView setRegion:mapRegion animated: YES];
}
您需要创建一个
MKUserTrackingBarButtonItem
,并在构造函数中将MKMapview
传递给它,然后将该按钮项添加到导航菜单中(或者在按钮应该位于的位置)
斯威夫特3+
let buttonItem = MKUserTrackingBarButtonItem(mapView: mapView)
self.navigationItem.rightBarButtonItem = buttonItem
这会将映射发送到用户位置,但如何添加按钮?@selector(zoomToUserLocation)remove:“因为您不需要将按钮作为参数发送到方法
- (void)viewDidLoad
{
[super viewDidLoad];
MKUserTrackingBarButtonItem *buttonItem = [[MKUserTrackingBarButtonItem alloc] initWithMapView:self.map];
self.navigationItem.rightBarButtonItem = buttonItem;
}
(void) viewDidLoad
{
[super viewDidLoad];
MKUserTrackingBarButtonItem *buttonItem = [[MKUserTrackingBarButtonItem alloc] initWithMapView:self.map];
self.navigationItem.rightBarButtonItem = buttonItem;
}
let buttonItem = MKUserTrackingBarButtonItem(mapView: mapView)
self.navigationItem.rightBarButtonItem = buttonItem