在当前位置蓝点上叠加透明PNG

时间:2011-09-09 15:57:06

标签: ios

我正在玩Apple的CurrentAddress示例代码,我试图使用trueHeading属性来确定用户面向的方向。虽然事实证明这很简单,但我想在当前位置点上方显示一个透明的PNG,我想旋转它以模拟指南针。

这是我目前得到的最基本的代码:

@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

如何将PNG叠加到蓝点的确切位置并保持在那里(如果用户移动,则跟在点后面),是这样的?

1 个答案:

答案 0 :(得分:1)

重点是你需要实现mapView:viewForAnnotation:委托方法,并在其中查找不属于你的注释对象。看看剪下的代码:

- (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;
    }
}