【问题标题】:Having trouble zooming and centering on users location in MapView in iOS在 iOS 的 MapView 中缩放和集中用户位置时遇到问题
【发布时间】:2012-12-18 06:46:09
【问题描述】:

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

我在 .h 文件中声明了 CLLocationCoordinate2D 类型的 userLoc。然后我的 .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];


}

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

【问题讨论】:

    标签: ios mkmapview core-location cllocationmanager


    【解决方案1】:
    -(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];
    
    
    }
    

    【讨论】:

    • 非常感谢您的解决方案。我没有意识到我是如此接近:-)
    • 没问题!有时最愚蠢的事情很难看到,而另一双眼睛会有所帮助
    猜你喜欢
    • 2015-09-28
    • 1970-01-01
    • 2017-02-13
    • 2010-09-20
    • 1970-01-01
    • 2020-02-19
    • 1970-01-01
    • 2012-03-15
    • 1970-01-01
    相关资源
    最近更新 更多