Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
831 views
in Technique[技术] by (71.8m points)

iphone - Annotation on the map problem

I am trying to create a map app where the user can tag a map with a photo, comment or video, but I am having a problem putting an annotation on the map.

My scenario is like this:

On the 1st page, the user can see three button with the map (1.photo, 2.comment and 3.video). When he wants to tag the map by clicking on the photo button. I use cammerView class, which gives three more buttons (1.take photo, 2.choose photo and 3.use photo); after taking a photo he has a choice of using photo or not. If he wants use this photo the screen must move to the map page, and the annotation must drop.

I am getting problem. I cannot figure out how to drop the annotation on user's map at the current location. This annotation must drop after the clicking the use button which on the photo class.

I also tried this sample application, but in my case I need to have the annotation drop on the map from a button that is on the same page. How can I make this work?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Code from Class1.m (Where Your button is touched):

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

And here last Supportive Class:

.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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...