Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>If you let the map view show the default annotation view for the user location (blue dot), this is simpler to implement (and you get a nice blue dot with cool animated zooming circle).</p> <p>If you must show the user location using a pin image instead of a blue dot, then some more work is needed.</p> <p>First, the simple way using the blue dot:</p> <pre><code>- (MKAnnotationView *)mapView:(MKMapView *)map viewForAnnotation:(id &lt;MKAnnotation&gt;)annotation{ if ([annotation isKindOfClass:[MKUserLocation class]]) { ((MKUserLocation *)annotation).title = @"My Current Location"; return nil; //return nil to use default blue dot view } //Your existing code for viewForAnnotation here (with some corrections)... static NSString *AnnotationViewID = @"annotationViewID"; SolarAnnotationView* annotationView = (SolarAnnotationView*)[map dequeueReusableAnnotationViewWithIdentifier:AnnotationViewID]; if(annotationView == nil) { { annotationView = [[[SolarAnnotationView alloc] initWithAnnotation:annotation] autorelease]; //added autorelease above to avoid memory leak annotationView.delegate = self; } } //update annotation in view in case we are re-using a view annotationView.annotation = annotation; return annotationView; } </code></pre> <p><br /> If you want to use your custom annotation view for the user location instead, you should put the pin color changing code in the custom view. One way to periodically change the color is using performSelector:withObject:afterDelay:. In the SolarAnnotationView.m, add these two methods:</p> <pre><code>-(void)startChangingPinColor { switch (self.pinColor) { case MKPinAnnotationColorRed: self.pinColor = MKPinAnnotationColorGreen; break; case MKPinAnnotationColorGreen: self.pinColor = MKPinAnnotationColorPurple; break; default: self.pinColor = MKPinAnnotationColorRed; break; } [self performSelector:@selector(startChangingPinColor) withObject:nil afterDelay:1.0]; } -(void)stopChangingPinColor { [NSObject cancelPreviousPerformRequestsWithTarget:self]; } </code></pre> <p>Also add the method headers to the SolarAnnotationView.h file.</p> <p>Then change the viewForAnnotation method like this:</p> <pre><code>- (MKAnnotationView *)mapView:(MKMapView *)map viewForAnnotation:(id &lt;MKAnnotation&gt;)annotation{ static NSString *AnnotationViewID = @"annotationViewID"; SolarAnnotationView* annotationView = (SolarAnnotationView*)[map dequeueReusableAnnotationViewWithIdentifier:AnnotationViewID]; if(annotationView == nil) { { annotationView = [[[SolarAnnotationView alloc] initWithAnnotation:annotation] autorelease]; annotationView.delegate = self; } } //Update annotation in view in case we are re-using a view... annotationView.annotation = annotation; //Stop pin color changing in case we are re-using a view that has it on //and this annotation is not user location... [annotationView stopChangingPinColor]; if([self CLLocationCoordinate2DEquals:mapView.userLocation.location.coordinate withSecondCoordinate:[annotation coordinate]]) //Show current location with green pin { [annotationView setPinColor:MKPinAnnotationColorGreen]; annotationView.canShowCallout = YES; ((MKPointAnnotation *)annotation).title = @"My Current Location"; [annotationView startChangingPinColor]; } return annotationView; } </code></pre>
 

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