iOS - 如何使用mkmapview作为位置选择器

Nir*_*att 2 iphone mkmapview mkannotation ios

在我的应用程序中,我有两个地址UITextfields使用地址文本填充.我想要这种行为:

  • 用户输入地址文本字段后,会立即显示地图.(已经完成了)
  • 用户使用地图.当他这样做时,会出现一个浮动的注释对象,该对象即将丢弃.
  • 按下按钮(mkmapview的子视图),将此浮动注释放到地图上.
  • 用户按下"完成"按钮后,地图上的注释位置将进行反向地理编码.
  • 地图视图消失(已完成).
  • UITextfield填充有与地图视图中心对应的地址文本.
  • 对于另一个UITextfield重复相同的操作.

我相信这是mkmapview的一个非常直接的用例.但是找不到任何已经完成的例子.我在UIKit中遗漏了一些非常明显的东西吗?

非常感谢您的帮助....

编辑:

我读了Apple的文档,我找到了许多指向这个问题的解决方案.但是,所有这些似乎都假设MKPinAnnotationView(或MKAnnotationView)被覆盖.

有必要吗?如果是的话,除了拖动之外,我需要在子类中提供多少?

Rob*_*Rob 7

不需要子类MKPinAnnotationView.只是使用它.如果您正在寻找一些自定义行为,您应该只是它的子类.但是编写一个是有用的,viewForAnnotation这样你就可以正确配置它.但通常我发现标准的配置MKPinAnnotationView足够简单,不需要子类化:

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

    MKPinAnnotationView *annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"DroppedPin"];

    annotationView.draggable = YES;
    annotationView.canShowCallout = YES;
    annotationView.animatesDrop = YES;

    return annotationView;
}
Run Code Online (Sandbox Code Playgroud)

话虽如此,拥有自己的注释类并不罕见.我可能会这样做至少有两个原因:

  1. 您可以将反向地理编码保留MKPlacemark为注释的属性.从逻辑上讲,地理编码信息似乎是注释的属性,而不是视图的属性.然后,您可以查询placemark已删除引脚的此属性,以获取传递回其他视图所需的任何信息.

  2. 如果需要,您可以将注释配置为执行反向地理编码查找,该反向地理编码查找placemark属性,但也可以在更改时将标题更改为反向地理编码地址coordinate.通过这种方式,用户可以获得有关反向地理编码的主动反馈,因为他们将地图上的引脚拖放,但代码仍然非常简单:

因此,您可能有一个注释类,如:

@interface DroppedAnnotation : NSObject <MKAnnotation>

@property (nonatomic, strong) MKPlacemark *placemark;
@property (nonatomic) CLLocationCoordinate2D coordinate;
@property (nonatomic, strong) NSString *title;
@property (nonatomic, strong) NSString *subtitle;

@end

@implementation DroppedAnnotation

- (void)setCoordinate:(CLLocationCoordinate2D)coordinate
{
    CLLocation *location = [[CLLocation alloc] initWithLatitude:coordinate.latitude
                                                      longitude:coordinate.longitude];
    CLGeocoder *geocoder = [[CLGeocoder alloc] init];
    [geocoder reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks, NSError *error) {
        // do whatever you want here ... I'm just grabbing the first placemark

        if ([placemarks count] > 0 && error == nil)
        {
            self.placemark = placemarks[0];

            NSArray *formattedAddressLines = self.placemark.addressDictionary[@"FormattedAddressLines"];
            self.title = [formattedAddressLines componentsJoinedByString:@", "];
        }
    }];

    _coordinate = coordinate;
}

@end
Run Code Online (Sandbox Code Playgroud)

您的视图控制器可以使用这个新类:

@property (nonatomic, weak) id<MKAnnotation> droppedAnnotation;

- (void)dropPin
{
    // if we've already dropped a pin, remove it

    if (self.droppedAnnotation)
        [self.mapView removeAnnotation:self.droppedAnnotation];

    // create new dropped pin

    DroppedAnnotation *annotation = [[DroppedAnnotation alloc] init];
    annotation.coordinate = self.mapView.centerCoordinate;
    annotation.title = @"Dropped pin"; // initialize the title
    [self.mapView addAnnotation:annotation];

    self.droppedAnnotation = annotation;
}
Run Code Online (Sandbox Code Playgroud)

要清楚,您不需要自己的注释类.MKPointAnnotation例如,您可以使用该标准.但是,您的视图控制器必须保持呼叫并跟踪反向地理编码信息本身.我只是认为使用自定义注释类时代码更清晰,更符合逻辑.