Annotation on the map problem

后端 未结 1 657
遥遥无期
遥遥无期 2020-12-20 07:30

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

相关标签:
1条回答
  • 2020-12-20 08:05

    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
    
    0 讨论(0)
提交回复
热议问题