8

我的 iOS 应用程序有以下代码:

NSString *location = [[NSString alloc] initWithFormat:@"%@, %@", [self.campus campusStreetAddress], [self.campus campusCityStateZip]];
CLGeocoder *geocoder = [[CLGeocoder alloc] init];
[geocoder geocodeAddressString:location
             completionHandler:^(NSArray* placemarks, NSError* error){
                 if (placemarks && placemarks.count > 0) {
                     CLPlacemark *topResult = [placemarks objectAtIndex:0];
                     MKPlacemark *placemark = [[MKPlacemark alloc] initWithPlacemark:topResult];
                     MKCoordinateRegion region = self.campusMap.region;
                     region.center = placemark.region.center; //DEPRECATED iOS 7
                     region.span.longitudeDelta /= 1500;
                     region.span.latitudeDelta /= 1500;
                     [self.campusMap setRegion:region animated:NO];
                     [self.campusMap addAnnotation:placemark];
                 }
             }
 ];

但是,当我将我的应用程序升级到 iOS 7 时,placemark.region.center 已被弃用。我应该使用替代品吗?这甚至是在视图中创建地图的正确方法吗?

谢谢!!

4

3 回答 3

22

尝试这个:

region.center = [(CLCircularRegion *)placemark.region center];
于 2013-10-05T18:03:33.607 回答
8

如果您只想要该区域的中心,您可以使用:

region.center = placemark.location.coordinate

于 2014-04-25T23:00:53.403 回答
0

结合 Heesien 和其他答案以及一些实验。

- (void)centerMapAroundPlacemark:(MKPlacemark *)placemark
{
    CLRegion *region = placemark.region;

    if ([region isKindOfClass:[CLCircularRegion class]])
    {
        [self centerMapAroundCircularRegion:(CLCircularRegion *)region
                            centerCoodinate:placemark.location.coordinate];
    }
    else
    {
        [self centerMapAroundCoorinate:placemark.location.coordinate];
    }
}

- (void)centerMapAroundCircularRegion:(CLCircularRegion *)circularRegion
{
    MKCoordinateRegion coordinateRegion =
    MKCoordinateRegionMakeWithDistance(circularRegion.center,
                                       circularRegion.radius,
                                       circularRegion.radius);

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

- (void)centerMapAroundCircularRegion:(CLCircularRegion *)circularRegion
                      centerCoodinate:(CLLocationCoordinate2D)centerCoodinate
{
    // Only user the radius of region for an appropriate zoom level.
    // The center of the region is not accurate.
    // To see this search for 'Bath, UK'

    MKCoordinateRegion coordinateRegion =
    MKCoordinateRegionMakeWithDistance(centerCoodinate,
                                       circularRegion.radius,
                                       circularRegion.radius);

    [self.mapView setRegion:coordinateRegion animated:YES];
}
于 2015-03-24T13:14:55.420 回答