关于地图问题的注释

时间:2011-06-20 12:02:33

标签: iphone objective-c mkmapview

我正在尝试创建一个地图应用,用户可以在其中使用照片,评论或视频标记地图,但我在向地图上添加注释时遇到问题。

我的情况是这样的:

在第一页上,用户可以看到带有地图的三个按钮(1.photo,2.comment和3.video)。当他想通过点击照片按钮标记地图时。我使用cammerView类,它提供了三个按钮(1.take photo,2.choose photo和3.use photo);拍照后他可以选择是否使用照片。如果他想使用这张照片,屏幕必须移动到地图页面,并且注释必须下降。

我遇到了问题。我无法弄清楚如何在当前位置删除用户地图上的注释。单击照片类上的使用按钮后,此注释必须删除。

我也试过了this sample application,但在我的情况下,我需要从同一页面上的按钮中删除地图上的注释。我怎样才能做到这一点?

1 个答案:

答案 0 :(得分:2)

Class1.m中的代码(触摸按钮的位置):

#Class1.m

- (void) trackImageOnMapButtonTouched
{
    MapView *tempView =[[MapView alloc] initWithNibName:@"MapView" bundle:[NSBundle mainBundle]];
    self.moveToMapView=tempView;
    [tempView release];
    int iId=[mainSlideShowImageView tag];
    self.moveToMapView.fromFlag_imageId=[NSString stringWithFormat:@"%d",iId];
    self.moveToMapView.slideShowView_imageOnSlide=[NSString stringWithFormat:@"%d",[mainSlideShowImageView tag]];
    NSLog(@"self.moveToMapView.slideShowView_imageOnSlide=%@",self.moveToMapView.slideShowView_imageOnSlide);
    [self.view addSubview:moveToMapView.view];
}

#Class2.m

- (void) viewDidLoad
{
    self.lattitudeArray=[[NSMutableArray alloc] init];
    self.longitudeArray=[[NSMutableArray alloc] init];
    MKCoordinateRegion region;
    MKCoordinateSpan span;

    CLLocationCoordinate2D location;

    span.latitudeDelta=2.0;
    span.longitudeDelta=2.0;
    location.latitude=43.25f;
    location.longitude=11.00f;
    region.span=span;
    region.center=location;


    addAnnotation = [[MapViewAnnotation alloc] initWithLocation:location withTitle:[NSString stringWithFormat:@"Tuscany"] withSubTitle:[NSString stringWithFormat:@"Italy"] withImage:[UIImage imageNamed:@"1.jpg"]];
    addAnnotation.mTitle=[NSString stringWithFormat:@"Tuscany"];
    addAnnotation.mSubTitle=[NSString stringWithFormat:@"Italy"];
    [mapView addAnnotation:addAnnotation];
    [mapView setRegion:region animated:TRUE];
    [mapView regionThatFits:region];
    mapView.mapType = MKMapTypeHybrid;   // also MKMapTypeSatellite or MKMapTypeHybrid
}


- (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>) annotation
{

    MKPinAnnotationView *annView=[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"currentloc"];
    annView.pinColor = MKPinAnnotationColorPurple;
    annView.animatesDrop=TRUE;
    annView.canShowCallout = YES;
    annView.calloutOffset = CGPointMake(-5, 5);


    NSString *imageName=[NSString stringWithFormat:@"%@.jpg",self.fromFlag_imageId];
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *fullImgNm=[documentsDirectory stringByAppendingPathComponent:[NSString stringWithString:imageName]];
    UIImage *actualImage=[UIImage imageWithContentsOfFile:fullImgNm];

    CGSize annImgSize;
    annImgSize.width=60;
    annImgSize.height=30;
    UIImage *locationImage=[self resizeImage:actualImage withSize:annImgSize];
    [rightButton setImage:locationImage forState:UIControlStateNormal];
    [rightButton addTarget:self
                    action:@selector(annotationPinClicked:)
          forControlEvents:UIControlEventTouchUpInside];
    annView.leftCalloutAccessoryView=rightButton;
    return annView;
}

这里是最后一个支持类:

·H

#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>
@interface MapViewAnnotation : NSObject <MKAnnotation>
{
    CLLocationCoordinate2D coordinate;
    NSString *mTitle;
    NSString *mSubTitle;
}

@property (nonatomic, retain) NSString *mTitle;
@property (nonatomic, retain) NSString *mSubTitle;
-(id)initWithLocation:(CLLocationCoordinate2D)location withTitle:(NSString *)title withSubTitle:(NSString *)subTitle withImage:(UIImage *)locationImage;
//- (CLLocationCoordinate2D)initWithLocation:(CLLocationCoordinate2D) location;


@end

的.m:

#import "MapViewAnnotation.h"

@implementation MapViewAnnotation

@synthesize coordinate;
@synthesize mTitle,mSubTitle;

-(id)initWithLocation:(CLLocationCoordinate2D)location withTitle:(NSString *)title withSubTitle:(NSString *)subTitle withImage:(UIImage *)locationImage
{
    coordinate.latitude = location.latitude;
    coordinate.longitude = location.longitude;
    return self;
}

-(NSString *)title
{
    return mTitle;
}

-(NSString *)subtitle
{
    return mSubTitle;
}

- (void) dealloc{
    [mTitle release];
    [mSubTitle release];
    [super dealloc];
}

@end