Domanda

Hey ragazzi, ho problemi a ottenere sovrapposizioni nella vista della mia mappa per aggiornare tramite il setNeedsDisplayInMapRect: funzione. Ecco il codice pertinente:

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");
    //...
}
//...

PARCHIOREGIOVERLAY.H:

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

E non sto ottenendo l'output "ParkingMapviewController.m MapView: ViewForOverlay" alla console che mi aspetto. Ho attraversato il debugger e ho assicurato che il loop è stato raggiunto ed eseguito, tuttavia mapView:viewForOverlay: non viene chiamato per un motivo. Qualcuno sa cosa sto facendo di sbagliato? Grazie in anticipo!

EDIT 1:

Credo di aver impostato il delegato, le coordinate e il controllo correttamente, ma per favore dai un'occhiata ...

ParkingMapviewController.H

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

ParkingMapviewController.m:

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

PARCHIOREGIONOVERLAY.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;
}
//...

Grazie.

EDIT 2:

Un metodo alternativo che ho provato, senza alcun risultato:

ParkingMapviewController.M

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

Rimuovere e riagganciare tutte le sovrapposizioni non funziona troppo bene per me. Si blocca semplicemente quando viene eseguita quella terza riga. Qualche idea?

EDIT 3:

Quindi ho modificato il codice precedentemente pubblicato in quanto segue:

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

E ora lo vedo nella console:

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'
È stato utile?

Soluzione 2

Quindi l'ho capito. Non necessariamente il metodo più efficiente, ma funziona per me. Questo è quello che ho fatto:

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

Ed ecco 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

In breve, non ho dovuto creare una nuova funzione, ma solo chiamare la funzione che ho usato per caricare le sovrapposizioni nella vista della mappa e funziona bene! Spero che questo aiuti chiunque altro bloccato in una situazione simile.

MODIFICARE:

Importante notare che sto ricaricando sia annotazioni che sovrapposizioni e, se fatto senza prima rimuovere sia annotazioni che sovrapposizioni, può portare a un arresto anomalo dell'app se la funzione di ricarica viene chiamata troppe volte. Questo è ciò che sto vivendo attualmente. Solo qualcosa di cui essere consapevoli. Per risolvere questo problema, avrò funzioni di carico separate, una per le sovrapposizioni e una per le annotazioni che saranno chiamate in modo appropriato.

Altri suggerimenti

È probabile che tu non abbia impostato la coordinata o boundingMapRect Proprietà sul MKOverlay correttamente. Il MapView Chiederà la vista solo se pensa che esiste la possibilità che sia visibile, se il suo retto visibile non interseca il boundMapRect, non lo farà.

Assicurati anche il tuo delegate per il mapView è impostato correttamente.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top