Overlaying a transparent PNG on top of the current location blue dot
i'm just playing around with Apple's CurrentAddress sample code and I was trying to use the trueHeading property to determine the direction the user is facing. While that proved simple enough, I wanted to display a transparent PNG on top of the current location dot and I wanted to rotate it in order to simulate a compass.
Here's the very basic code I've currently got:
@implementation MapViewController
@synthesize mapView, reverseGeocoder, getAddressButton;
- (void)viewDidLoad
{
[super viewDidLoad];
mapView.showsUserLocation = YES;
}
- (IBAction)reverseGeocodeCurrentLocation
{
self.reverseGeocoder =
[[[MKReverseGeocoder alloc] initWithCoordinate:mapView.userLocation.location.coordinate] autorelease];
reverseGeocoder.delegate = self;
[reverseGeocoder start];
}
- (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFailWithError:(NSError *)error
{
NSString *errorMessage = [error localizedDescription];
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Cannot obtain address."
开发者_如何学编程 message:errorMessage
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alertView show];
[alertView release];
}
- (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFindPlacemark:(MKPlacemark *)placemark
{
PlacemarkViewController *placemarkViewController =
[[PlacemarkViewController alloc] initWithNibName:@"PlacemarkViewController" bundle:nil];
placemarkViewController.placemark = placemark;
[self presentModalViewController:placemarkViewController animated:YES];
}
- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
// we have received our current location, so enable the "Get Current Address" button
[getAddressButton setEnabled:YES];
NSString *north = [NSString stringWithFormat:@"%f", self.mapView.userLocation.heading.trueHeading];
NSLog(@"here: %@", north);
}
@end
How can I overlay the PNG to the exact position of the blue dot and keep it there (following the dot if the user moves, that is)?
The point is that you need to implement mapView:viewForAnnotation:
delegate method and look inside it for annotation objects which are not yours. Take a look at the code snipped:
- (MKAnnotationView *)mapView:(MKMapView *)map viewForAnnotation:(id <MKAnnotation>)annotation {
NSString static *defaultID = @"defaultID";
NSString static *userLocationID = @"userLocationID";
if ([annotation isKindOfClass:[MyAnnotation class]]) {
// your code here
} else {
MKAnnotationView *annotationView = [map dequeueReusableAnnotationViewWithIdentifier:userLocationID];
if (!annotationView) {
annotationView = [[[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:userLocationID] autorelease];
annotationView.image = [UIImage imageNamed:@"UserLocationIcon.png"];
}
return annotationView;
}
}
精彩评论