Shanthanu
Shanthanu

Reputation: 421

Custom Annotation view is not displaying on the map

Hi I'm trying to create a custom Annotation in the mapview. In mapview i have multiple pin maker in on the map when user click on the pin i want to display some text and button so have tried to create a custom Annotation view its not working.

My code

#import <MapKit/MapKit.h>
#import <GoogleMaps/GoogleMaps.h>
      @interface myviewcon : UIViewController<MKMapViewDelegate>
      @property (strong, nonatomic) IBOutlet MKMapView *mpview;

    @end

My MKPointAnnotation code in viewdidload

for (NSDictionary *dic in [jsonArray1 valueForKey:@"houses"]) {


      MKPointAnnotation *annotation = [[MKPointAnnotation alloc]init];
      NSString *name=[dic valueForKey:@"building_name"];

      CLLocationCoordinate2D placeCoord;

      placeCoord.latitude=[[dic objectForKey:@"lat"] doubleValue];
      placeCoord.longitude=[[dic objectForKey:@"lng"] doubleValue];



      [annotation setCoordinate:placeCoord];
      [annotation setTitle:name];
      [mpview addAnnotation:annotation];

      }

And my Custom Annotaionview code

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

    MKAnnotationView *anview = nil;

    UIButton *btnViewVenue = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
    anview.rightCalloutAccessoryView=btnViewVenue;
    anview.enabled = YES;
    anview.canShowCallout = YES;
    anview.multipleTouchEnabled = NO;

    return anview;
  }

I have used the above code to make customview when user click on the pin maker on the map i have make view with text and buttons i have tired with like this but its not working i have checked with break points that delegate is calling but its not working please tell how to resolve this issue.

Thanks in Advance.

Upvotes: 2

Views: 1041

Answers (1)

LoVo
LoVo

Reputation: 2083

To customize your annotation use:

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

here just add subview which you need

- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view {

    UIView *test = [[UIView alloc]initWithFrame:CGRectMake(view.frame.origin.x - 12.5, view.frame.origin.y - 45, 50, 50)];
    test.backgroundColor = [UIColor redColor];
    [_mapView addSubview:test];
}

here just delete your custom callOutView from superView

- (void)mapView:(MKMapView *)mapView didDeselectAnnotationView:(MKAnnotationView *)view

Upvotes: 2

Related Questions