{"id":4815,"date":"2014-03-30T15:34:48","date_gmt":"2014-03-30T15:34:48","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/subclasses-of-mkannotationview-collection-of-common-programming-errors\/"},"modified":"2014-03-30T15:34:48","modified_gmt":"2014-03-30T15:34:48","slug":"subclasses-of-mkannotationview-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/subclasses-of-mkannotationview-collection-of-common-programming-errors\/","title":{"rendered":"Subclasses of MKAnnotationView-Collection of common programming errors"},"content":{"rendered":"<p>I don&#8217;t know of any other templates, but that does not mean that they don&#8217;t exist. \ud83d\ude42<\/p>\n<p>Anyway, here&#8217;s how to create custom ones: Create a new class conforming to the <code>MKAnnotation<\/code> protocol. You will need to have two instance variables of type <code>NSString*<\/code> named <code>title<\/code> and <code>subtitle<\/code> and one of type <code>CLLocationCoordinate2D<\/code> named coordinate and an appropriate setter method (e.g. property). Those strings are going to be displayed in the callout. In the delegate of your mapView, implement the method <code>-mapView:viewForAnnotation:<\/code> in a similar way as you would implement the datasource for a UITableView. That is, dequeueing an annotationView by an identifier, setting the new properties (e.g. a button of type UIButtonTypeDetailDisclosure for the right accessory view). You will want to add an image to display underneath the offset. Just use the <code>image<\/code> property of your MKAnnotationView. The center of your custom image will be placed at the coordinate specified, so you may want to give an offset: <code>aView.centerOffset = CGPointMake(0, -20)<\/code><\/p>\n<p>Here is some sample code:<\/p>\n<pre><code>- (MKAnnotationView *) mapView: (MKMapView *) mapView viewForAnnotation: (id) annotation {\n    \/\/ reuse a view, if one exists\n    MKAnnotationView *aView = [mapView dequeueReusableAnnotationViewWithIdentifier:@\"pinView\"];\n\n    \/\/ create a new view else\n    if (!aView) {\n        aView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@\"pinView\"];\n    }\n\n    \/\/ now configure the view\n    aView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];\n    [(UIButton*)aView.rightCalloutAccessoryView addTarget:self action:@selector(showDetails:) forControlEvents:UIControlEventTouchUpInside];\n    aView.canShowCallout = YES;\n    aView.enabled = YES;\n    aView.image = [UIImage imageNamed:@\"green_pin.png\"];\n    aView.centerOffset = CGPointMake(0, -20);\n\n    return aView;\n}\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>I don&#8217;t know of any other templates, but that does not mean that they don&#8217;t exist. \ud83d\ude42 Anyway, here&#8217;s how to create custom ones: Create a new class conforming to the MKAnnotation protocol. You will need to have two instance variables of type NSString* named title and subtitle and one of type CLLocationCoordinate2D named coordinate [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-4815","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/4815","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=4815"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/4815\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=4815"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=4815"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=4815"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}