Iphone MapView: Annotation Pin - Different Color
I ask for your advice in solving this issue:
I am fetching a XML File to display Pins on my MapView. In another method I am showing my current position on the map:
-(void)setPOIs {
NSLog(@"insert POIs");
int x = 0;
while (x < [res count]) {
CLLocationCoordinate2D c;
NSString *latitude = [[res objectAtIndex:x] objectForKey:@"latitude"];
NSString *longitude = [[res objectAtIndex:x] objectForKey:@"longitude"];
c.latitude = [latitude doubleValue];
c.longitude = [longitude doubleValue];
GPSAnnotation *annotation = [[GPSAnnotation alloc] initWithCoordinate:c];
annotation.mTitle=[[res objectAtIndex:x] objectForKey:@"ordiname"];
annotation.currentPoint = [NSNumber numberWithInt:[[[res objectAtIndex:x] objectForKey:@"tierarztid"] intValue]];
annotation.currentRow = [res objectAtIndex:x];
[mapViewOutlet addAnnotation:annotation];
[annotation release];
NSLog(@"latitude setPOIs: %@", latitude);
x++;
}
[self showOwn];
}
-(void)showOwn {
CLLocationCoordinate2D c;
c.latitude = location.latitude;
c.longitude = location.longitude;
GPSAnnotation *annotation1 = [[GPSAnno开发者_如何学JAVAtation alloc] initWithCoordinate:c];
annotation1.mTitle= @"Ihr Standort";
[mapViewOutlet addAnnotation:annotation1];
[annotation1 release];
}
Here I show the pins:
- (MKAnnotationView *)mapView:(MKMapView *)mapViewOutlet viewForAnnotation:(GPSAnnotation *)annotation {
int postag = 0;
//int row = 0;
MKPinAnnotationView *annView=[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"currentloc"];
annView.pinColor = MKPinAnnotationColorGreen;
UIButton *myDetailButton = [UIButton buttonWithType:UIButtonTypeCustom];
myDetailButton.frame = CGRectMake(0, 0, 23, 23);
myDetailButton.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
myDetailButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
// Set the image for the button
[myDetailButton setImage:[UIImage imageNamed:@"button_right.png"] forState:UIControlStateNormal];
[myDetailButton addTarget:self action:@selector(showLinks:) forControlEvents:UIControlEventTouchUpInside];
if ([[annotation title] isEqualToString:@"Current Location"]) {
postag = 99999;
} else {
postag = [annotation.currentPoint intValue];
}
myDetailButton.tag = postag;
// Set the button as the callout view
annView.rightCalloutAccessoryView = myDetailButton;
annView.animatesDrop=NO;
annView.canShowCallout = YES;
annView.calloutOffset = CGPointMake(-5, 5);
return annView;
[annView release];
}
What can I do to display a custom image for my current position - and/or not to display a disclosure button?
thanks for your feedback! I solved by simply adapting an if... else statement
- (MKAnnotationView *)mapView:(MKMapView *)mapViewOutlet viewForAnnotation:(GPSAnnotation *)annotation {
int postag = 0;
//int row = 0;
MKPinAnnotationView *annView=[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"currentloc"];
annView.pinColor = MKPinAnnotationColorGreen;
UIButton *myDetailButton = [UIButton buttonWithType:UIButtonTypeCustom];
myDetailButton.frame = CGRectMake(0, 0, 23, 23);
myDetailButton.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
myDetailButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
if ([[annotation title] isEqualToString:@"Ihr Standort"]) {
postag = 99999;
UIImage *annotationImage = [UIImage imageNamed:@"07-map-marker.png"];
annView.image = annotationImage;
} else {
postag = [annotation.currentPoint intValue];
[myDetailButton setImage:[UIImage imageNamed:@"button_right.png"] forState:UIControlStateNormal];
[myDetailButton addTarget:self action:@selector(showLinks:) forControlEvents:UIControlEventTouchUpInside];
}
myDetailButton.tag = postag;
// Set the button as the callout view
annView.rightCalloutAccessoryView = myDetailButton;
annView.animatesDrop=NO;
annView.canShowCallout = YES;
annView.calloutOffset = CGPointMake(-5, 5);
return annView;
[annView release];
}
BR,
Stefan
Instead of using MKPinAnnotationView
use an MKAnnotationView
. MKPinAnnotationView
is a subclass of MKAnnotationView
. The MKPinAnnotationView will ONLY show pins. MKAnnotationView has an image property that you can set to show a different image instead of a pin.
The line of code that is showing the disclosure button is your rightCalloutAccessoryView
instruction. Take that out and the detail disclosure in the callout will go away.
- (MKAnnotationView *)mapView:(MKMapView *)theMapView viewForAnnotation:(id <MKAnnotation>)annotation
{
if ([annotation isKindOfClass:[ParkingSpot class]])
{
static NSString* AnnotationIdentifier = @"ParkAnnotationIdentifier";
MKAnnotationView* annotationView = (MKAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:ParkAnnotationIdentifier];
if (!annotationView)
{
MKAnnotationView *annotationView = [[[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationIdentifier] autorelease];
annotationView.draggable = NO;
UIImage *annotationImage = ...your image here
annotationView.image = annotationImage;
}
return annotationView;
}
}
精彩评论