Tuesday, December 15, 2015

Create Custom Annotation View On Map in iOS



//Just add this two methods...

-(void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView*)view{
    
    UIImageView * myView = [[UIImageView allocinit];//(CGRectMake(-110, -72, 260, 80))
    myView.image = [UIImage imageNamed:@"2xLocatioPopUp.png"];//[UIColor blackColor];
    //myView.alpha = 0.4;
    //myView.layer.cornerRadius = 5.0;
    //[myView sizeToFit];
    [view addSubview:myView];

    UITextView * titleLabel = [[UITextView allocinitWithFrame:(CGRectMake(-1050250,0))];//(CGRectMake(-105, -72, 250, 60))
    titleLabel.text = @"You are Here";
    titleLabel.editable = FALSE;
    titleLabel.selectable = FALSE;
    titleLabel.scrollEnabled = FALSE;
    titleLabel.font = [UIFont systemFontOfSize:17.0];
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.backgroundColor = [UIColor clearColor];
    titleLabel.textColor = [UIColor whiteColor];
    titleLabel = [Utility setTextViewFrame:titleLabel fontSize:19.0];
    
    CGFloat height = titleLabel.frame.size.height;
    NSLog(@"%f",height);
    
    if (height < 30.0) {
        height = 30.0;
    }
    titleLabel.frame = CGRectMake(-105, -35 - height, 250, height);
    
    //MKAnnotationView * annotationView = [[MKAnnotationView alloc] init];
    
    myView.frame = CGRectMake(-110, titleLabel.frame.origin.y260, height + 45);

    [view addSubview:titleLabel];
}

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation{
    
    static NSString *reuseId = @"ann";
    MKAnnotationView *av = [mapViewdequeueReusableAnnotationViewWithIdentifier:reuseId];
    if (av == nil)
    {
        av = [[MKAnnotationView allocinitWithAnnotation:annotationreuseIdentifier:reuseId];
    }
    else
    {
        av.annotation = eventPoint;
    }
    //av.frame = CGRectMake(av.frame.origin.x, av.frame.origin.y, 30, 30);
    av.image = [UIImage imageNamed:@"map_pin_OG_512x512.png"];

    return av;

}





No comments:

Post a Comment