Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I have been using cutom map pin created by <strong>Mayur Birari,</strong> which i tweaked a little bit, to support custom map pin images and id's.</p> <p><strong>CustomMapPin.h</strong></p> <pre><code>#import &lt;Foundation/Foundation.h&gt; #import &lt;MapKit/MapKit.h&gt; @interface CustomMapPin : NSObject&lt;MKAnnotation&gt; { CLLocationCoordinate2D coordinate; NSString* title; NSString* subtitle; NSString* pic; NSInteger tag_number; } @property (nonatomic, assign) CLLocationCoordinate2D coordinate; @property (nonatomic, copy) NSString* title; @property (nonatomic, copy) NSString* subtitle; @property (nonatomic, copy) NSString* pic; @property (nonatomic) NSInteger tag_number; @end </code></pre> <p><strong>CustomMapPin.m</strong></p> <pre><code>#import "CustomMapPin.h" @implementation CustomMapPin @synthesize title; @synthesize subtitle; @synthesize coordinate; @synthesize pic; @synthesize tag_number; - (void)dealloc { self.title = nil; self.pic = nil; self.subtitle = nil; [super dealloc]; } @end </code></pre> <p>and using it in class like this:</p> <pre><code>CLLocationCoordinate2D pinlocation; </code></pre> <p><em>in a loop I set up required values and create a map pin:</em></p> <pre><code>pinlocation.latitude = ...; pinlocation.longitude = ...; NSInteger pinID = ....; CustomMapPin* customMapPin=[[CustomMapPin alloc] init]; customMapPin.coordinate=(CLLocationCoordinate2D {pinlocation.latitude,pinlocation.longitude}; customMapPin.title=@"title"; customMapPin.subtitle=@"subtitle"; customMapPin.pic = @"customImageName"; customMapPin.tag_number = pinId; [mapView addAnnotation:customMapPin]; </code></pre> <p>Setting up custom image:</p> <pre><code>- (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id &lt;MKAnnotation&gt;) annotation { if ([annotation isKindOfClass: [CustomMapPin class]]) { CustomMapPin * a = annotation; [annView setImage:[UIImage imageNamed:a.pic]]; } } </code></pre> <p>Getting pin id on callout:</p> <pre><code>- (void)mapView:(MKMapView *)mp annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control { CustomMapPin * v = (CustomMapPin *) view.annotation; int tagNumber = v.tag_number; .... } </code></pre> <p>and finally - in my project It was required to have filter buttons - so I needed to remove all pins, and add required. By default calling mapview to remove all annotations created memory leaks. So when I need to clear mapview from annotations, I call this function:</p> <pre><code>- (void)removeAnnotations { NSMutableArray *toRemove = [NSMutableArray arrayWithCapacity:[mapView.annotations count]]; for (id annotation in mapView.annotations) { if (annotation != mapView.userLocation) { [toRemove addObject:annotation]; } } [mapView removeAnnotations:toRemove]; for(int i = 0; i &lt; [toRemove count]; i++) { CustomMapPin * a = [toRemove objectAtIndex:i]; [a release]; a = nil; } } </code></pre> <p>Hope this helps Happy coding! :)</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload