【问题标题】:Annotation on the map problem地图标注问题
【发布时间】:2011-06-20 12:02:32
【问题描述】:

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

我的场景是这样的:

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

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

我也尝试过this sample application,但在我的情况下,我需要通过同一页面上的按钮将注释拖放到地图上。我怎样才能做到这一点?

【问题讨论】:

  • 你试过这个代表了吗:- (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>) annotation {}
  • 是的,试试这个,但我需要在点击下一页上的使用照片后删除注释。谢谢
  • 那你需要用trick来编码,没有别的,虽然我自己实现了,但是解释起来很棘手和复杂。 :(
  • 只是谷歌一下......很多例子可以学习;例如:mithin.in/2009/06/22/…

标签: iphone objective-c mkmapview


【解决方案1】:

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

【讨论】:

  • 您知道,您可以使用@synthesize title=mTitle, subtitle=mSubTitle;,然后您不必显式定义titlesubtitle 访问器。
  • @Abizern:谢谢你让我知道..(我没有):)
  • 我可以用示例代码以不同的方式再次问同样的问题,以便更好地了解我在做什么以及我想要做什么?
  • 请查看此问题链接stackoverflow.com/questions/6421613/…
  • @rptwsthi 这里的 moveToMapView 是什么
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-06-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多