Skip to content

Instantly share code, notes, and snippets.

@rmarinho
Created November 13, 2014 22:05
Show Gist options
  • Save rmarinho/f81aefe1982c91e3671b to your computer and use it in GitHub Desktop.
Save rmarinho/f81aefe1982c91e3671b to your computer and use it in GitHub Desktop.
Custom Annotation
public class CustomAnnotationView : MKAnnotationView
{
public Item CurrentData {get;set;}
public TipCategory CurrentCategory {get;set;}
UIImageView imgMarker;
UILabel lblTitle;
UIButton btnGoTodetail;
MKMapView mapView;
public CustomAnnotationView (MKMapView map, Item it, TipCategory cat)
{
mapView = map;
CurrentData = it;
CurrentCategory = cat;
lblTitle = new UILabel ();
lblTitle.Text = it.Title;
this.lblTitle.Font = UIFont.FromName("Roboto-Regular", 15);
this.lblTitle.TextColor = UIColor.Clear.FromHexString ("#444a52", 1);
imgMarker = new UIImageView ();
btnGoTodetail = new UIButton ();
btnGoTodetail.TouchUpInside += (object sender, EventArgs e) => {
mapView.Delegate.CalloutAccessoryControlTapped(mapView,this,btnGoTodetail);
};
this.AddSubview (imgMarker);
this.AddSubview (lblTitle);
this.AddSubview (btnGoTodetail);
this.UpdateUi (Selected);
}
void UpdateUi (bool value)
{
if (value) {
var catSelectedImage = UIImage.FromFile ("markerLocation/" + CurrentCategory.IconName.Replace (".png", "") + "Selected.png");
imgMarker.Image = catSelectedImage;
imgMarker.Frame = new System.Drawing.RectangleF (0, 0, 151, 59);
lblTitle.Frame = new System.Drawing.RectangleF (50, -3, 80, 49);
this.btnGoTodetail.Frame = new System.Drawing.RectangleF (80, 0, 70, 45);
var s = this.Frame;
s.Width = 151;
s.X = 53;
this.CenterOffset = new System.Drawing.PointF (s.X,-30);
this.Frame =s;
}
else {
var catImage = UIImage.FromFile ("markerLocation/" + CurrentCategory.IconName);
imgMarker.Image = catImage;
this.CenterOffset = new System.Drawing.PointF (0,-30);
this.Frame = imgMarker.Frame = new System.Drawing.RectangleF (0, 0, 45, 59);
lblTitle.Frame = new System.Drawing.RectangleF (40, 0, 0, 0);
this.btnGoTodetail.Frame = new System.Drawing.RectangleF (80, 0, 0, 0);
}
this.SetNeedsDisplay ();
}
public override bool Selected {
get {
return base.Selected;
}
set {
UpdateUi (value);
Console.WriteLine ("selected " + value);
base.Selected = value;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment