2013-10-04 29 views
7

我有這樣的代碼爲我的iOS應用:的iOS 7 - region.center棄用

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已被棄用。有我應該使用的替代品嗎?這甚至是在視圖中創建地圖的適當方法嗎?

謝謝!

回答

21

試試這個:

region.center = [(CLCircularRegion *)placemark.region center]; 
+0

工作完美,謝謝! – jordangrogan

+4

這個解決方案令我困擾的唯一問題是,不能保證CLPlacemark的區域屬性必然總是會成爲CLCircularRegion。它現在可能有效,但是...我想我寧願在使用它的中心屬性之前確認該區域是CLCircularRegion,如果沒有,則找到可接受的替代值。 –

+0

我同意@DougKnowles是不是現在有更優雅的解決方案? –

8

如果你只是想在區域的中心,您可以使用:

region.center = placemark.location.coordinate的

+0

事實上,當我使用地標中心和當我使用位置座標時,我會得到不同的經度/緯度答案。 let center =(地標?[0] .region as!CLCircularRegion).center print(placemarks?[0] .location?.coordinate ??「???」) diff的大小足以產生不同的地址。 – mark

0

組合Heesien的和other answers和一些試驗。

- (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]; 
}