No soy capaz de dar una MKAnnotation una imagen!
-
25-09-2019 - |
Pregunta
Hola chicos! Estoy teniendo algunos problemas con dar una MKAnnotationView una imagen en lugar de una vista alfiler. En otras palabras, estoy teniendo problemas para mostrar una imagen de destino (target.png) en lugar de la vista normal alfiler. Aquí está mi código ---
// .h file
#import //Here it says to import mapkit & UIKit. The code blockquote doesn't let me
#import //show you that
@interface AddressAnnotation : NSObject {
CLLocationCoordinate2D coordinate;
NSString *mTitle;
NSString *mSubTitle;
}
@end
@interface ChosenLocationMap : UIViewController {
IBOutlet MKMapView *mapView;
AddressAnnotation *addAnnotation;
}
-(CLLocationCoordinate2D) addressLocation;
// .m file
@implementation AddressAnnotation
@synthesize coordinate;
- (NSString *)subtitle{
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
NSString *stitle = [prefs objectForKey:@"addressKey"];
return @"%@",stitle;
}
- (NSString *)title{
return @"TARGET";
}
-(id)initWithCoordinate:(CLLocationCoordinate2D) c{
coordinate=c;
NSLog(@"%f,%f",c.latitude,c.longitude);
return self;
}
@end
@implementation ChosenLocationMap
@synthesize destinationLabel, startbutton, accelloop, aimview, bombblowupview, bombleftview1, bombleftview2, bombleftview3, firebutton;
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}
- (void)viewDidLoad {
mapView.mapType = MKMapTypeSatellite;
MKCoordinateSpan span;
span.latitudeDelta=0.2;
span.longitudeDelta=0.2;
CLLocationCoordinate2D location = [self addressLocation];
region.span=span;
region.center=location;
if(addAnnotation != nil) {
[mapView removeAnnotation:addAnnotation];
[addAnnotation release];
addAnnotation = nil;
}
addAnnotation = [[AddressAnnotation alloc] initWithCoordinate:location];
[mapView addAnnotation:addAnnotation];
[super viewDidLoad];
}
-(CLLocationCoordinate2D) addressLocation {
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
NSString *destinationstring = [prefs objectForKey:@"addressKey"];
NSString *urlString = [NSString stringWithFormat:@"http://maps.google.com/maps/geo?q=%@&output=csv",
[destinationstring stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSString *locationString = [NSString stringWithContentsOfURL:[NSURL URLWithString:urlString]];
NSArray *listItems = [locationString componentsSeparatedByString:@","];
double latitude = 0.0;
double longitude = 0.0;
if([listItems count] >= 4 && [[listItems objectAtIndex:0] isEqualToString:@"200"]) {
latitude = [[listItems objectAtIndex:2] doubleValue];
longitude = [[listItems objectAtIndex:3] doubleValue];
}
else {
//Show error
}
CLLocationCoordinate2D location;
location.latitude = latitude;
location.longitude = longitude;
return location;
}
- (MKAnnotationView *)map:(MKMapView *)map viewForAnnotation:(id )annotation{
MKAnnotationView *annView;
annView = (MKAnnotationView *) [mapView dequeueReusableAnnotationViewWithIdentifier:annotation.title];
if(annView == nil)
annView = [[[MKAnnotationView alloc]
initWithAnnotation:annotation reuseIdentifier:annotation.title] autorelease];
else
annView.annotation = annotation;
[annView setImage:[UIImage imageNamed:@"target.png"]];
annView.canShowCallout = TRUE;
return annView;
}
Tenga en cuenta que sólo incluía el código que realmente implica el MapView. Gracias de antemano!
EDIT:. He cambiado el código en mi documento de Xcode para los cambios en la respuesta 1. Soy demasiado vago para la transferencia de todo el bloque de código anterior, y aún así, la imagen sigue sin funcionar
Shoop DA EDIT: Gracias por responder! Mi solución fue que se me olvidó decir mapView.delegate = self
. Bye!
Solución
Wow tanto mal con este código: -)
En primer lugar se echa en falta una declaración @property
en su AddressAnnotation
@property (nonatomic,assign) CLLocationCoordinate2D coordinate;
En el método de subtítulos que hace esto:
return @"%@",stitle;
Pero esto es de Objective-C y no Python, por lo que es posible que desee cambiar esto a:
return stitle;
A continuación, su initWithCoordinate es totalmente erróneo. No inicializa súper. Esto es mejor:
-(id) initWithCoordinate: (CLLocationCoordinate2D) c
{
if ((self = [super init]) != nil) {
coordinate=c;
NSLog(@"%f,%f",c.latitude,c.longitude);
}
return self;
}
Trate de fijar esas cosas primero para ver si ayuda: -)
Otros consejos
Soy nuevo en Objective C, pero creo que el prototipo de la función de su delegado debe ser:
- (MKAnnotationView *)mapView:(MKMapView *)map viewForAnnotation:(id )annotation{
MKAnnotationView *annView;
El nombre del delegado es MAPview : viewForAnnotation :, no mapa : viewForAnnotation:
También AddressAnnotation no implementa el protocolo MKAnnotation