Domanda

Ho un mkmapview Aggiungere un singolo mkplacemark a rappresentare la posizione di un edificio che l'utente ha appena selezionato.L'utente può solo selezionare un edificio alla volta e voglio semplicemente spostare il segnaposto nel nuovo edificio quando selezionano un nuovo edificio.Sul primo edificio selezionano, funziona bene e posiziona un PIN sulla mappa.Quando provo a chiamare setCoordinate sul segnaposto per aggiornare la posizione del marker quando selezionano un nuovo edificio però, ottengo -[MKPlacemark setCoordinate:]: unrecognized selector sent to instance

in myviewcontroller.h ho:

@property (nonatomic, strong)MKPlacemark *selectedBuildingPlacemark;
.

in myviewcontroller.m

@synthesize selectedBuildingPlacemark;

...

if (self.selectedBuildingPlacemark == nil) {
        self.selectedBuildingPlacemark = [[MKPlacemark alloc] initWithCoordinate:myCoord addressDictionary:nil];
        [mapView addAnnotation:self.selectedBuildingPlacemark];
    }
    else {
        [self.selectedBuildingPlacemark setCoordinate:myCoord];
    }
.

Pensavo che mkplacemark sia conforme a Mkannotation e dovrebbe quindi implementare setCoordinate.Qualcuno può mostrarmi l'errore dei miei modi?

È stato utile?

Soluzione

The documentation of MKAnnotation says:

Annotations that support dragging should implement this method to update the position of the annotation.

So the method setCoordinate: is optional and is only implemented by classes that support dragging. The documentation of MKPlacemark does not reference that method, so it is not implemented.

So you should create a new instance every time you select a new building.

Altri suggerimenti

If you don't specifically need to use an MKPlacemark (it doesn't look like it because you're passing nil for the addressDictionary), you could use the MKPointAnnotation class instead.

MKPointAnnotation implements MKAnnotation but adds a setCoordinate method.

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