MKMapView缩放到viewDidLoad上的用户位置?

时间:2011-05-19 19:37:54

标签: iphone objective-c cocoa-touch

我正在尝试在视图加载后将地图缩放到用户的当前位置,但我收到错误“** *因未捕获的异常而终止应用程序'NSInvalidArgumentException',原因:'无效区域视图加载时“”。请有人帮忙吗?

干杯!

- (void)viewDidLoad
{
    [super viewDidLoad];

    MKCoordinateRegion mapRegion;   
    mapRegion.center.latitude = map.userLocation.coordinate.latitude;
    mapRegion.center.longitude = map.userLocation.coordinate.longitude;
    mapRegion.span.latitudeDelta = 0.2;
    mapRegion.span.longitudeDelta = 0.2;
    [map setRegion:mapRegion animated: YES];   
}

11 个答案:

答案 0 :(得分:102)

你设置showsUserLocation = YES了吗? MKMapView如果设置为NO,则不会更新该位置。所以要确保这一点。

MKMapView对象很可能还没有用户位置。要做得对,您应该采用MKMapViewDelegate协议并实施mapView:didUpdateUserLocation:

map.delegate = self;

...
-(void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation 
{
    MKCoordinateRegion mapRegion;   
    mapRegion.center = mapView.userLocation.coordinate;
    mapRegion.span.latitudeDelta = 0.2;
    mapRegion.span.longitudeDelta = 0.2;

    [mapView setRegion:mapRegion animated: YES];
}

答案 1 :(得分:19)

与Deepak的答案一样,除了你可以更优雅地设置范围:

- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation {
    MKCoordinateRegion mapRegion;   
    mapRegion.center = map.userLocation.coordinate;
    mapRegion.span = MKCoordinateSpanMake(0.2, 0.2);
    [map setRegion:mapRegion animated: YES];
}

答案 2 :(得分:7)

如果用户希望滚动地图,您不希望在userDidUpdateLocation内更新此内容。如果您将该代码放在上述方法中,则用户将无法滚动地图,因为将调用该函数并将地图居中回到当前位置。

答案 3 :(得分:6)

到目前为止,最简单的方法是在didUpdateUserLocation中使用mapView.showAnnotations:

func mapView(mapView: MKMapView!, didUpdateUserLocation userLocation: MKUserLocation!) {
    mapView.showAnnotations([userLocation], animated: true)
}

这就是全部!

MKUserLocation符合MKAnnotation协议。将userLocation作为一个数组传递,showAnnotations方法将让mapView放大一个区域加上跨越MKAnnotations数组的填充,在这种情况下它只是userLocation。适合我。

如果您只想放大一次,请使用initialUserLocation属性检查是否已设置初始属性。我根本不喜欢使用dispatch_once来做这类事情

答案 4 :(得分:4)

这是@ Deepak在iOS 10的Swift 3中的答案:

extension ViewController: MKMapViewDelegate
{
    func mapView( _ mapView: MKMapView, didUpdate userLocation: MKUserLocation )
    {
        let regionRadius = 400 // in meters
        let coordinateRegion = MKCoordinateRegionMakeWithDistance( userLocation.coordinate, regionRadius, regionRadius )
        self.mapView.setRegion( coordinateRegion, animated: true)
    }
}

答案 5 :(得分:3)

使用didUpdateUserLocation方法时出现的问题,您将无法滚动或缩放到其他位置。它会一直指向您的位置。 最好的方法(我尝试过)是你需要使用位置管理器。

viewDidLoad中添加以下内容:

self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
self.locationManager.distanceFilter = kCLLocationAccuracyKilometer;;
// showing user location with the blue dot
[self.mapView setShowsUserLocation:YES];

self.mapView.delegate = self;

// getting user coordinates
CLLocation *location = [_locationManager location];
CLLocationCoordinate2D  coordinate = [location coordinate];


// showing them in the mapView
_mapView.region = MKCoordinateRegionMakeWithDistance(coordinate, 250, 250);

[self.locationManager startUpdatingLocation];

这样您就可以使用所需的缩放显示您的位置。 我希望这会有所帮助。

答案 6 :(得分:2)

......

MKCoordinateRegion region =mapView.region;

region.center.latitude = currentLocation.latitude ;
region.center.longitude = currentLocation.longitude;
region.span.longitudeDelta /= 1000.0;
region.span.latitudeDelta /= 1000.0;

[mapView setRegion:region animated:YES];
[mapView regionThatFits:region];

答案 7 :(得分:2)

查看MKMapView的setUserTrackingMode方法。可能是在MKMapView上跟踪用户位置的最简单方法是

[self.mapView setUserTrackingMode:MKUserTrackingModeFollow animated:YES];

这样你也不需要成为MKMapViewDelegate。可能的模式是:

MKUserTrackingModeNone = 0, // the user's location is not followed
MKUserTrackingModeFollow, // the map follows the user's location
MKUserTrackingModeFollowWithHeading, // the map follows the user's location and heading

答案 8 :(得分:0)

检查map.userLocation.coordinate的值,其余的都没问题。

答案 9 :(得分:0)

Swift 3版 使用此方法可放大任何坐标。

extension MKMapView{
    func zoomIn(coordinate: CLLocationCoordinate2D, withLevel level:CLLocationDistance = 10000){
        let camera =
            MKMapCamera(lookingAtCenter: coordinate, fromEyeCoordinate: coordinate, eyeAltitude: level)
        self.setCamera(camera, animated: true)
    }
}

答案 10 :(得分:0)

按照 Deepak 的建议,实现 mapView:didUpdate: 委托方法。 Swift 5 的语法:

func mapView(_ mapView: MKMapView, didUpdate userLocation: MKUserLocation) {
  guard let newLocation = userLocation.location else { return }
  let region = MKCoordinateRegion(center: newLocation.coordinate, span: MKCoordinateSpan(latitudeDelta: 0.2, longitudeDelta: 0.2))
  mapView.setRegion(region, animated: true)
}
相关问题