MKMapview将引脚放置在位置(长/纬)

3sl*_*3sl 18 objective-c mkmapview mkannotation mkannotationview

我有纬度和长值,我需要能够在这个位置放一个引脚.

任何人都可以就如何解决这个问题提供一些建议吗?

Jha*_*iya 22

找到以下非常简单的解决方案,将引脚放在CLLocationCoordinate2D定义的给定位置

放下MKMapView上的Pin

编辑:

CLLocationCoordinate2D  ctrpoint;
ctrpoint.latitude = 53.58448;
ctrpoint.longitude =-8.93772;
AddressAnnotation *addAnnotation = [[AddressAnnotation alloc] initWithCoordinate:ctrpoint]; 
[mapview addAnnotation:addAnnotation];
[addAnnotation release];
Run Code Online (Sandbox Code Playgroud)

  • 无需为了放置引脚而创建/使用自定义类.您可以使用苹果提供的`MKPointAnnotation`类. (7认同)

Inf*_*ies 16

您应该:
1.将MapKit框架添加到您的项目中.2.创建一个实现MKAnnotation协议的类.
样品:

Annotation.h

@interface Annotation : NSObject <MKAnnotation> {
    NSString *_title;
    NSString *_subtitle;

    CLLocationCoordinate2D _coordinate;
}

// Getters and setters
- (void)setTitle:(NSString *)title;
- (void)setSubtitle:(NSString *)subtitle;

@end
Run Code Online (Sandbox Code Playgroud)

Annotation.m

@implementation Annotation

#pragma mark -
#pragma mark Memory management

- (void)dealloc {
    [self setTitle:nil];
    [self setSubtitle:nil];

    [super dealloc];
}

#pragma mark -
#pragma mark Getters and setters

- (NSString *)title {
    return _title;
}

- (NSString *)subtitle {
    return _subtitle;
}

- (void)setTitle:(NSString *)title {    
    if (_title != title) {
        [_title release];
        _title = [title retain];
    }
}

- (void)setSubtitle:(NSString *)subtitle {
    if (_subtitle != subtitle) {
        [_subtitle release];
        _subtitle = [subtitle retain];
    }
}

- (CLLocationCoordinate2D)coordinate {
    return _coordinate;
}

- (void)setCoordinate:(CLLocationCoordinate2D)newCoordinate {
    _coordinate = newCoordinate;
}

@end
Run Code Online (Sandbox Code Playgroud)

2.创建此类的实例并设置lat/lon属性
3.使用以下方法将实例添加到MKMapView对象:

- (void)addAnnotation:(id<MKAnnotation>)annotation
Run Code Online (Sandbox Code Playgroud)

4.您应该设置地图的委托并实现以下方法:

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation {
    if ([annotation isKindOfClass:[MKUserLocation class]]) {
        return nil;
    }

    static NSString* ShopAnnotationIdentifier = @"shopAnnotationIdentifier";
    MKPinAnnotationView *pinView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:ShopAnnotationIdentifier];
    if (!pinView) {
        pinView = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:ShopAnnotationIdentifier] autorelease];
        pinView.pinColor = MKPinAnnotationColorRed;
        pinView.animatesDrop = YES;
    }
    return pinView;
}
Run Code Online (Sandbox Code Playgroud)


fil*_*chp 5

这假设您已启用ARC并且已包含MapKit框架.

首先创建一个实现MKAnnotation协议的类.我们称之为MapPinAnnotation.

MapPinAnnotation.h

@interface MapPinAnnotation : NSObject <MKAnnotation>

@property (nonatomic, readonly) CLLocationCoordinate2D coordinate;
@property (nonatomic, readonly) NSString* title;
@property (nonatomic, readonly) NSString* subtitle;

- (id)initWithCoordinates:(CLLocationCoordinate2D)location
                placeName:(NSString *)placeName
              description:(NSString *)description;

@end
Run Code Online (Sandbox Code Playgroud)

MapPinAnnotation.m

@implementation MapPinAnnotation

@synthesize coordinate;
@synthesize title;
@synthesize subtitle;

- (id)initWithCoordinates:(CLLocationCoordinate2D)location
                placeName:(NSString *)placeName
              description:(NSString *)description;
{
  self = [super init];
  if (self)
  {    
    coordinate = location;
    title = placeName;
    subtitle = description;
  }

  return self;
}

@end
Run Code Online (Sandbox Code Playgroud)

然后使用以下内容将注释添加到地图:

CLLocationCoordinate2D coordinate = CLLocationCoordinate2DMake(34.421496,
                                                              -119.70182);

MapPinAnnotation* pinAnnotation = 
  [[MapPinAnnotation alloc] initWithCoordinates:coordinate
                                      placeName:nil
                                    description:nil];
[mMapView addAnnotation:pinAnnotation];
Run Code Online (Sandbox Code Playgroud)

包含类必须实现MKMapViewDelegate协议.特别是你必须定义以下函数来绘制引脚:

- (MKAnnotationView *)mapView:(MKMapView *)mapView 
            viewForAnnotation:(id <MKAnnotation>)annotation
{
  if ([annotation isKindOfClass:[MKUserLocation class]])
  {
    return nil;
  }

  static NSString* myIdentifier = @"myIndentifier";
  MKPinAnnotationView* pinView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:myIdentifier];

  if (!pinView)
  {
    pinView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:myIdentifier];
    pinView.pinColor = MKPinAnnotationColorRed;
    pinView.animatesDrop = NO;
  }
  return pinView;
}
Run Code Online (Sandbox Code Playgroud)

在此示例中,不使用MKAnnotation标题和副标题成员变量,但它们可以显示在委托函数中.