2011-05-05 79 views
0

嘿傢伙,我無法在我的地圖視圖中通過setNeedsDisplayInMapRect:函數刷新覆蓋。下面是相關代碼:問題刷新iPhone的地圖查看

ParkingMapViewController.m:

for (ParkingRegionOverlay *overlay in mapView.overlays) { 
    [overlay setNeedsDisplayInMapRect:self.mapView.visibleMapRect]; 
} 

//... 
- (MKOverlayView *)mapView:(MKMapView *)mapView 
      viewForOverlay:(id <MKOverlay>)overlay 
{ 
    NSLog(@"ParkingMapViewController.m mapView:viewForOverlay"); 
    //... 
} 
//... 

ParkingRegionOverlay.h:

@interface ParkingRegionOverlay : MKOverlayView <MKOverlay> { 
    MKPolygon *polygon; 
    MKMapRect boundingRect; 
    CLLocationCoordinate2D centerCoord; 
    //... 
} 
//... 

而且我沒有得到 「ParkingMapViewController.m的MapView:viewForOverlay」 輸出到控制檯我我期待。我已經走過調試器並確保for循環正在到達並執行,但由於某種原因,mapView:viewForOverlay:未被調用。任何人都知道我在做什麼錯了?提前致謝!

編輯1:

我相信我已經設置委託,協調,妥善邊界矩形,但請看看...

ParkingMapViewController.h

@interface ParkingMapViewController : UIViewController <MKMapViewDelegate> { 
    MKMapView *mapView; 
//... 

ParkingMapViewController.m:

//... 
- (void)viewDidLoad { 
    [super viewDidLoad]; 
    mapView.delegate = self; 
//... 

ParkingRegionOverlay.m:

//... 
//initializes polygon and calculates bounding rect as well as its center coordinate 
-(id)initWithPoints:(NSArray *)pointsArray andTitle:(NSString *)overlayTitle{ 
    MKMapPoint points[[pointsArray count]]; 
    double maxX = MIN_COORD_VAL; 
    double minX = MAX_COORD_VAL; 
    double maxY = MIN_COORD_VAL; 
    double minY = MAX_COORD_VAL; 
    double tempX = 0; 
    double tempY = 0; 

    if (self = [super init]) { 
     int i = 0; 
     //determine min/max extrema to help calculate the bounding rect 
     for (id coordDict in pointsArray){ 
      tempX = [[coordDict objectForKey:@"latitude"] doubleValue]; 
      tempY = [[coordDict objectForKey:@"longitude"] doubleValue]; 
      maxX = fmax(tempX, maxX); 
      minX = fmin(tempX, minX); 
      maxY = fmax(tempY, maxY); 
      minY = fmin(tempY, minY); 

      CLLocationCoordinate2D coord = {tempX,tempY}; 
      points[i] = MKMapPointForCoordinate(coord); 
      i++; 
     }//for 

     CLLocationCoordinate2D northWestCorner = CLLocationCoordinate2DMake(maxX, minY); 
     CLLocationCoordinate2D southEastCorner = CLLocationCoordinate2DMake(minX, maxY); 
     MKMapPoint northWestPoint = MKMapPointForCoordinate(northWestCorner); 
     MKMapPoint southEastPoint = MKMapPointForCoordinate(southEastCorner); 
     boundingRect = MKMapRectMake(northWestPoint.x, northWestPoint.y, 
            (southEastPoint.x-northWestPoint.x), 
            (southEastPoint.y-northWestPoint.y)); 

     centerCoord = CLLocationCoordinate2DMake((maxX-minX)/2,(maxY-minY)/2); 
     polygon = [MKPolygon polygonWithPoints:points count:[pointsArray count]]; 
     polygon.title = overlayTitle; 

     [self initAcceptedPermitsBasedOnTitle:overlayTitle]; 
    }//if 

    return self; 
} 
//... 

謝謝。

編輯2:

的另一種方法我曾嘗試,沒有效果:

ParkingMapViewController.m

NSArray *overlayArray = [[NSArray alloc] initWithArray:[mapView overlays]]; 
    [self.mapView removeOverlays:mapView.overlays]; 
    [self.mapView addOverlays:overlayArray]; 

卸下和重新添加所有疊加層不工作太對我來說很好。它只是在第三行執行時崩潰。有任何想法嗎?

編輯3:

所以我改變了以前發佈的代碼如下:

NSArray *overlayArray = [mapView overlays]; 
[self.mapView removeOverlays:overlayArray]; 
[self.mapView addOverlays:overlayArray]; 

,我現在看到這個控制檯:

2011-05-05 14:24:54.145 Parking[68501:207] -[NSCFNumber boundingMapRect]: unrecognized selector sent to instance 0xa9afae0 
2011-05-05 14:24:54.147 Parking[68501:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSCFNumber boundingMapRect]: unrecognized selector sent to instance 0xa9afae0' 
+0

viewForOverlay最初是否被調用?覆蓋層顯示嗎?你需要刷新的覆蓋層有什麼變化?有趣的是,ParkingRegionOverlay既是MKOverlayView的子類,也實現了MKOverlay協議。不知道這可能與它有什麼關係。 – Anna 2011-05-05 21:23:53

+0

是的,viewForOverlay被初始調用並且所有的疊加層都正確顯示。如果邊界矩形沒有正確設置,那麼疊加層是不會出現的?這就是爲什麼我懷疑這是問題所在。我想在用戶輸入一些信息後改變疊加層的顏色(不是所有的都會改變)。是的,我曾經有ParkingRegionOverlay是NSObject的子類,但然後setNeedsDisplayInMapRect:不會工作,所以我把它作爲一個子類,現在它不抱怨,但沒有任何反應。我不確定是否這樣做是標準的。請參閱上文。 – Stunner 2011-05-05 21:30:11

回答

0

所以我想通了。不一定是最有效的方法,但它適用於我。這是我做過什麼:

[self.mapView removeOverlays:[mapView overlays]]; 
[self loadOverlaysAndAnnotations]; 

這裏是loadOverlaysAndAnnotations

- (void)loadOverlaysAndAnnotations { 

    NSError *error; 
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init]; 
    CoreDataSingleton *coreDataSingleton = [CoreDataSingleton sharedManager]; 
    NSEntityDescription *entity = [NSEntityDescription 
            entityForName:@"ParkingLot" inManagedObjectContext:[coreDataSingleton managedObjectContext]]; 
    [fetchRequest setEntity:entity]; 
    NSArray *fetchedObjects = [[coreDataSingleton managedObjectContext] executeFetchRequest:fetchRequest error:&error]; 
    for (NSManagedObject *overlayEntity in fetchedObjects) { 
     NSArray *pointsArray = [NSArray arrayWithArray:[overlayEntity valueForKey:@"overlayCoordPoints"]]; 
     ParkingRegionOverlay *regionPolygon = [[ParkingRegionOverlay alloc] initWithPoints:pointsArray andTitle:[overlayEntity valueForKey:@"lotName"]]; 
     [mapView addOverlay:regionPolygon]; 
     [regionPolygon release]; 


     NSSet *annotationsSet = [NSSet setWithSet:[overlayEntity valueForKey:@"parkingAnnotations"]]; 
     NSArray *allAnnotations = [NSArray arrayWithArray:[annotationsSet allObjects]]; 
     CLLocationCoordinate2D workingCoordinate; 
     for (ParkingAnnotations *annotation in allAnnotations) { 
      ParkingAnnotation *parkingAnnot = [[ParkingAnnotation alloc] init]; 
      workingCoordinate.latitude = [[annotation latitude] doubleValue]; 
      workingCoordinate.longitude = [[annotation longitude] doubleValue]; 
      [parkingAnnot setCoordinate:workingCoordinate]; 
      [parkingAnnot setTitle:[overlayEntity valueForKey:@"lotName"]]; 
      if ([[overlayEntity valueForKey:@"lotName"] isEqualToString:@"VP 1"]) { 
       [parkingAnnot setLot:lot1]; 
      } 

      [mapView addAnnotation:parkingAnnot]; 
      [parkingAnnot release]; 
     } 
    }   
    [fetchRequest release]; 
}//loadOverlaysAndAnnotations 

總之,我沒有創建一個新的功能,但只是叫我用來加載覆蓋到功能地圖視圖和工作正常!希望這有助於其他人陷入類似的情況。

編輯:

需要注意的是,我重裝BOTH註釋和覆蓋,並且,如果沒有先刪除這兩個註釋和覆蓋完成後,可能會導致您的應用程序崩潰,如果重載函數被調用過多次。這是我目前正在經歷的。只是要注意的事情。爲了解決這個問題,我將有單獨的加載函數,一個用於覆蓋,另一個用於註釋,它將被適當地調用。

0

機會是你沒有正確設置MKOverlay上的座標或boundingMapRect屬性。 MapView只會詢問視圖是否認爲有可能是可見的,如果其可見矩形不與boundMapRect相交,則不會。

還要確保您的delegatemapView設置正確。

+0

感謝您的回覆,請看看我上面編輯過的帖子。 – Stunner 2011-05-05 12:40:37