Reputation: 8629
I am trying to change my pin colour to purple, when I do it I lose the title though. Code is:
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationController.navigationBarHidden=YES;
//init the location manager
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[self.locationManager requestWhenInUseAuthorization];
self.mapView.showsUserLocation=YES;
self.userGeoPoint=self.message[@"userLocation"];
self.pinView = [[MKPointAnnotation alloc] init];
self.pinView.title=self.message[@"fromUser"];
self.coord = CLLocationCoordinate2DMake(self.userGeoPoint.latitude, self.userGeoPoint.longitude);
self.pinView.coordinate=self.coord;
//use a slight delay for more dramtic zooming
[self performSelector:@selector(addPin) withObject:nil afterDelay:0.5];
}
-(void)addPin{
[self.mapView addAnnotation:self.pinView];
[self.mapView selectAnnotation:self.pinView animated:YES];
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(self.coord, 800, 800);
[self.mapView setRegion:[self.mapView regionThatFits:region] animated:YES];
}
-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotationPoint
{
if ([annotationPoint isKindOfClass:[MKUserLocation class]])//keep the user as default
return nil;
static NSString *annotationIdentifier = @"annotationIdentifier";
MKPinAnnotationView *pinView = [[MKPinAnnotationView alloc]initWithAnnotation:annotationPoint reuseIdentifier:annotationIdentifier];
pinView.pinColor = MKPinAnnotationColorPurple;
//now we can throw an image in there
return pinView;
}
I tried setting the title property for MKPinAnnotation but there isn't one. Is there anyway I can get around this?
Upvotes: 3
Views: 5026
Reputation: 5535
I just upgrade to ios 10, apple changed the api.
pinColor no longer in use, instead, use tintColor
New:
MKPinAnnotationView *view = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:nil];
view.tintColor = [UIColor purpleColor];
Upvotes: 1
Reputation: 1242
Note that the pin title will be displayed when the user taps it:
If the value of this property is true, a standard callout bubble is shown when the user taps a selected annotation view. The callout uses the title and subtitle text from the associated annotation object. ( https://developer.apple.com/documentation/mapkit/mkannotationview/1452451-canshowcallout )
Upvotes: 0
Reputation: 3730
The pinColor
property was deprecated in iOS 9. From iOS 9 onwards you should use pinTintColor
which takes a UIColor
rather than a MKPinAnnotationColor
.
Old:
MKPinAnnotationView *view = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:nil];
view.pinColor = MKPinAnnotationColorPurple;
New:
MKPinAnnotationView *view = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:nil];
view.pinTintColor = [UI Color purpleColor];
There is more information available in the Apple docs.
Upvotes: 1
Reputation:
In viewForAnnotation
, you need to set canShowCallout
to YES
(it's NO
by default):
pinView.pinColor = MKPinAnnotationColorPurple;
pinView.canShowCallout = YES;
pinView
of type MKPointAnnotation
that you are using to create the annotation object in viewDidLoad
. Then in viewForAnnotation
, you have a local variable also named pinView
of type MKPinAnnotationView
. Although there is no naming conflict here, it causes and implies some conceptual confusion. MKPointAnnotation
is an annotation model class while MKPinAnnotationView
is an annotation view class -- they are completely different things. It would be better to name the annotation property pin
or userAnnotation
for example.This comment in viewForAnnotation
:
//now we can throw an image in there
seems to imply that you could set a custom image in the view at this point. Setting a custom image in a MKPinAnnotationView
is not recommended since that class is designed to display default pin images in one of three colors. To use a custom image, create a plain MKAnnotationView
instead.
Upvotes: 2