0

我试图显示一个简单地捕获用户纬度和经度坐标的地图,然后在地图上放大到用户。我能够捕获用户的坐标,但我无法显示放大到用户位置的地图,也无法将地图以用户为中心。不幸的是,我只能查看从远处显示用户的地图。我的相关代码如下:

userLoc在 .h 文件中声明了 CLLocationCoordinate2D 类型。然后我的 .m 文件中有以下内容:

    - (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{

    NSLog(@"didUpdateToLocation: %@", newLocation);
    CLLocation *currentLocation = newLocation;
    userLoc = newLocation.coordinate;
    [locationManager stopUpdatingLocation];
    [self showCurrentLocation];



    if (currentLocation != nil) {
        userLongitude = [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.longitude];
        userLatitude = [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.latitude];
    }


    NSLog(@"User Latitude is %@", userLatitude);
    NSLog(@"User Longitude is %@", userLongitude);
}

然后我有以下显示实际地图的方法:

-(void) showCurrentLocation {

    mapView = [[MKMapView alloc] initWithFrame:CGRectMake(0, 55, 320, 425)];
    MKCoordinateRegion region = mapView.region;
    MKCoordinateSpan span;

    region.center = userLoc;
    span.latitudeDelta = 0.02;
    span.longitudeDelta = 0.02;
    region.span=span;

    [mapView setMapType:MKMapTypeStandard];
    [mapView setZoomEnabled:YES];
    [mapView setScrollEnabled:YES];
    [mapView setShowsUserLocation:YES];

    [self.view addSubview:mapView];


}

正如我所说,我可以查看显示用户位置的地图,但地图既不会放大,也不会以用户为中心。谁能明白为什么?

4

1 回答 1

0
-(void) showCurrentLocation {

    mapView = [[MKMapView alloc] initWithFrame:CGRectMake(0, 55, 320, 425)];
    MKCoordinateRegion region = mapView.region;
    MKCoordinateSpan span;

    region.center = userLoc;
    span.latitudeDelta = 0.02;
    span.longitudeDelta = 0.02;
    region.span=span;

    [mapView setMapType:MKMapTypeStandard];
    [mapView setZoomEnabled:YES];
    [mapView setScrollEnabled:YES];
    [mapView setShowsUserLocation:YES];
    [mapView setRegion:region animated:YES];
    [self.view addSubview:mapView];


}
于 2013-01-03T20:37:19.617 回答