嘿傢伙,我無法在我的地圖視圖中通過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'
viewForOverlay最初是否被調用?覆蓋層顯示嗎?你需要刷新的覆蓋層有什麼變化?有趣的是,ParkingRegionOverlay既是MKOverlayView的子類,也實現了MKOverlay協議。不知道這可能與它有什麼關係。 – Anna 2011-05-05 21:23:53
是的,viewForOverlay被初始調用並且所有的疊加層都正確顯示。如果邊界矩形沒有正確設置,那麼疊加層是不會出現的?這就是爲什麼我懷疑這是問題所在。我想在用戶輸入一些信息後改變疊加層的顏色(不是所有的都會改變)。是的,我曾經有ParkingRegionOverlay是NSObject的子類,但然後setNeedsDisplayInMapRect:不會工作,所以我把它作爲一個子類,現在它不抱怨,但沒有任何反應。我不確定是否這樣做是標準的。請參閱上文。 – Stunner 2011-05-05 21:30:11