繁体   English   中英

放大地图上的当前位置

[英]Zoom in to current location on map

我想在地图上显示用户的当前位置并将其放大。

我尝试如下:

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        NSLog(@"Init MapViewController");
        [self initMap];
    }
    return self;
}


-(void) initMap
{
    _mapView = [[MKMapView alloc] initWithFrame:self.view.frame];

    _mapView.delegate = self;
    _mapView.showsUserLocation = YES;

    [self.view addSubview:_mapView];

    CLLocationCoordinate2D currentLocation;
    currentLocation.latitude = _mapView.userLocation.coordinate.latitude;
    currentLocation.longitude= _mapView.userLocation.coordinate.longitude;

    MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(currentLocation, 0.5*METERS_PER_MILE, 0.5*METERS_PER_MILE);

    [_mapView setRegion:viewRegion animated:YES];
}

然后在模拟器中显示海洋中的位置。 我在模拟器中将位置设置为伦敦。

只要这样做:

@property  (strong, nonatomic)   CLLocationManager *locationManager;

在viewdidload中:

self.locationManager = [[CLLocationManager alloc] init] ;
self.locationManager.delegate = (id)self;
self.locationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation ;
[self.locationManager setDistanceFilter:10.0f] ;
[self.locationManager startUpdatingLocation];

在您的位置经理中

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation
       fromLocation:(CLLocation *)oldLocation
{
    [self.locationManager stopUpdatingLocation];
    CLLocationCoordinate2D zoomLocation;
    zoomLocation.latitude = YourMapView.userLocation.location.coordinate.latitude;
    zoomLocation.longitude= YourMapView.userLocation.location.coordinate.longitude;
    // 2
    MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(zoomLocation, 0.5*1609.344, 0.5*1609.344);
    // 3
    [YourMapView setRegion:viewRegion animated:YES];
}

我需要听用户位置的更改。 因为在视图中未加载位置,所以位置未知。

-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
    MKCoordinateRegion region;
    region.center = self.mapView.userLocation.coordinate;

    MKCoordinateSpan span;
    span.latitudeDelta  = 1; // Change these values to change the zoom
    span.longitudeDelta = 1;
    region.span = span;

    [self.mapView setRegion:region animated:YES];
}

而这在viewdidload中:

[self.mapView.userLocation addObserver:self
                            forKeyPath:@"location"
                               options:(NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld)
                               context:nil];

暂无
暂无

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

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