简体   繁体   English

将MapKit区域按钮动作转换为方法

[英]Translate MapKit Region Button Action to Method

Apologies in advance for what must surely be so simple. 事先为必定如此简单的事情道歉。

I want my map to zoom to a programmatically defined region surrounding the user's location. 我希望地图缩放到围绕用户位置的以编程方式定义的区域。 I've found tons of code where this is done via a button action, but I need it done automatically when the map loads. 我已经找到了很多通过按钮操作完成的代码,但是我需要在地图加载时自动完成。 I'll post what I've tried here. 我将在这里发布我尝试过的内容。 Probably missing something very obvious. 可能缺少一些非常明显的东西。 The meat of these both work as button actions, so I assume my issue is with my method code (first line of each). 这两者的实质都是按钮动作,因此我假设我的问题出在我的方法代码上(每个方法的第一行)。

Method 1: Using MKCoordinateSpanMake 方法1:使用MKCoordinateSpanMake

- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
    float spanX = 0.00725;
    float spanY = 0.00725;
    MKCoordinateRegion region;
    region.center.latitude = self.mapView.userLocation.coordinate.latitude;
    region.center.longitude = self.mapView.userLocation.coordinate.longitude;
    region.span = MKCoordinateSpanMake(spanX, spanY);
    [self.mapView setRegion:region animated:YES];

}

Method 2: Using MKCoordinateRegionMakeWithDistance 方法2:使用MKCoordinateRegionMakeWithDistance

- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
MKUserLocation *userLocation = _mapView.userLocation;
    MKCoordinateRegion region =
    MKCoordinateRegionMakeWithDistance (
                                        userLocation.location.coordinate, 3000, 3000);
    [_mapView setRegion:region animated:YES];
}

As always, thanks so much for your time and advice. 与往常一样,非常感谢您的时间和建议。

Move to viewDidLoad: 移至viewDidLoad:

- (void)viewDidLoad
{
    MKUserLocation *userLocation = _mapView.userLocation;
    MKCoordinateRegion region =[_mapView regionThatFits:MKCoordinateRegionMakeWithDistance (userLocation.location.coordinate, 3000, 3000)];
    [_mapView setRegion:region animated:YES];
}

Hope that will help. 希望对您有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM