0

I have a button on Map which directs to "MyLocation" its hardcore lat n lng. i want to change the pin color. Tried some but not successful. Heres the code.

- (IBAction)btnMylLocation:(UIButton *)sender {

CLLocationCoordinate2D coord = {.latitude =  18.520430, .longitude =  73.856744};
MKCoordinateSpan span = {.latitudeDelta =  0.2, .longitudeDelta =  0.2};
MKCoordinateRegion region = {coord, span};
[self.mapView setRegion:region];
CLLocationCoordinate2D annotationCoord;
annotationCoord.latitude = 18.520430;
annotationCoord.longitude = 73.856744;
self.lblLongitude.text = [NSString stringWithFormat:@"%f ", annotationCoord.latitude];
self.lblLatitude.text = [NSString stringWithFormat:@" %f", annotationCoord.longitude];
MKPointAnnotation *annotationPoint = [[MKPointAnnotation alloc] init];
annotationPoint.coordinate = annotationCoord;
annotationPoint.title = @"Mindbowser";
annotationPoint.subtitle = @"Pune Headquater's";
[_mapView addAnnotation:annotationPoint];
}
rmaddy
  • 314,917
  • 42
  • 532
  • 579
iDeveloper
  • 940
  • 1
  • 10
  • 45

4 Answers4

4

You need to implement the map's delegate method and set the delegate.

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
    // If it's the user location, just return nil.
  if ([annotation isKindOfClass:[MKUserLocation class]])
  return nil;

  // Handle any custom annotations.
  if ([annotation isKindOfClass:[MKPointAnnotation class]])
  {

  MKPinAnnotationView *pinView = (MKPinAnnotationView*)[mapView dequeueReusableAnnotationViewWithIdentifier:@"CustomPinAnnotationView"];
  if (!pinView)
  {
    pinView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"CustomPinAnnotationView"];
    pinView.canShowCallout = YES;
    pinView.pinColor = MKPinAnnotationColorGreen;
  }
  else {
    pinView.annotation = annotation;
  }

  return pinView;

  }

  return nil;
} 

In your viewDidLoad() , write the following code

_mapView.delegate = self;
Muneeba
  • 1,756
  • 10
  • 11
2

This may help you.

- (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>) annotation
{
MKPinAnnotationView *annotationView = nil;

if(![annotation isKindOfClass:[MKUserLocation class]]){

    annotationView=[[MKPinAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:@"pin"];
    annotationView.pinColor = MKPinAnnotationColorGreen;
}
return annotationView;
 }
technerd
  • 14,144
  • 10
  • 61
  • 92
1

Implement the delegate method below and return MKAnnotationView by assigning the pinTintColor it is available from iOS 9.

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
    static NSString *annotaionIdentifier=@"annotationIdentifier";
    MKPinAnnotationView *aView=(MKPinAnnotationView*)[mapView dequeueReusableAnnotationViewWithIdentifier:annotaionIdentifier ];
    if (aView==nil) {

        aView=[[MKPinAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:annotaionIdentifier];
        aView.pinTintColor = [UIColor yellowColor];//from iOS 9, you can pass in any color you want.
        //aView.pinColor = MKPinAnnotationColorPurple //if iOS is less than 9.0
        aView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
        aView.animatesDrop=TRUE;
        aView.canShowCallout = YES;
        aView.calloutOffset = CGPointMake(-5, 5);
    }

    return aView;
}
Satheesh
  • 10,998
  • 6
  • 50
  • 93
0

To change color while adding annotation, check below method.

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation 
(id<MKAnnotation>)annotation {

    static NSString *identifier = @"PinAnnotationIdentifier";
    MKPinAnnotationView *pinAnnotation = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:identifier];
    if(!pinAnnotation) {
        pinAnnotation = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier];
    }
    pinAnnotation.pinColor = MKPinAnnotationColorGreen;  //Color

    return pinAnnotation;
}

Use below method for changing color on select and deselect.

- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKPinAnnotationView *)view {
    view.pinColor = MKPinAnnotationColorRed;
}

- (void)mapView:(MKMapView *)mapView didDeselectAnnotationView:(MKPinAnnotationView *)view {        
    view.pinColor = MKPinAnnotationColorGreen;
}
nikhil84
  • 3,235
  • 4
  • 22
  • 43