【问题标题】:Drop a pin on MKMapView在 MKMapView 上放置图钉
【发布时间】:2015-09-11 13:25:58
【问题描述】:

iPhone 新手来自 Java。所以我在这个阶段的目标是允许用户在地图上“放置一个图钉”。我的地图初始化如下所示:

- (void)viewDidLoad {
    [super viewDidLoad];
     NSLog(@"your view did load, I'm going to initizlie the map by your location");
     CLLocationCoordinate2D location = theMap.userLocation.coordinate;
     NSLog(@"Location found from Map: %f %f",location.latitude,location.longitude);

     MKCoordinateRegion region;
     MKCoordinateSpan span;

     NSLog(@"coordinates: %f %f",location.latitude,location.longitude);
     if (TARGET_IPHONE_SIMULATOR) {
         NSLog(@"You're using the simulator:");
         location.latitude  =  40.8761620;
         location.longitude = -73.782596;
     } else {
         location.latitude  =  theMap.userLocation.location.coordinate.latitude;
         location.longitude =  theMap.userLocation.location.coordinate.longitude;
     }

     span.latitudeDelta = 0.001;
     span.longitudeDelta = 0.002;

     region.span = span;
     region.center = location;

     [theMap setRegion:region animated:YES];
     [theMap regionThatFits:region];
     [theMap setMapType:MKMapTypeSatellite]; 
     [theMap setZoomEnabled:YES];
     [theMap setScrollEnabled:YES];
     [theMap setShowsUserLocation:YES];
}

对于请求的 pin drop 我有

- (MKAnnotationView *)mapView:(MKMapView *)mV viewForAnnotation:(id <MKAnnotation>)annotation {
    MKPinAnnotationView *pinView = nil;
    if (annotation != theMap.userLocation) {
        static NSString *defaultPinID = @"aPin";
        pinView = (MKPinAnnotationView *)[theMap dequeueReusableAnnotationViewWithIdentifier:defaultPinID];
    if (pinView == nil)
        pinView = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:defaultPinID] autorelease];
    } else {
    }
    pinView.pinColor = MKPinAnnotationColorRed;
    pinView.canShowCallout = YES;
    pinView.animatesDrop = YES;
    return pinView;
}

我不确定我是否完全理解此地图 (theMap) 对viewForAnnotation 中的引脚的作用?我的意思是,用户执行什么操作会激活viewForAnnotation 方法?此代码不起作用,我不确定为什么。

我正在使用模拟器,所以我不确定是否有我应该按下的按钮或Alt click 它?

【问题讨论】:

标签: ios objective-c mkmapview


【解决方案1】:

我不确定我是否完全理解此地图 (theMap) 如何用于 viewForAnnotation 中的图钉?

MKPinAnnotationView 只是另一种注解视图——也就是说,您向地图添加注解(符合MKAnnotation 协议的对象)。当地图想要显示注解时(可能是因为用户滚动地图以使注解在视图中),它会要求您提供用于表示注解的视图。此时,您的 mapView:viewForAnnotation: 方法可以获取或创建一个 pin 注释视图并将其返回。用户不做任何直接触发mapView:viewForAnnotation:的操作,除了滚动或缩放。

如果您希望用户能够删除 pin,那是另一回事。您需要提供他们可以拖动的视图(甚至可能是MKPinAnnotationView)。当他们表示他们想要放下图钉(可能是通过抬起手指)时,您可以移除视图并在该点添加适当的注释。 然后地图视图将通过调用其委托的mapView:viewForAnnotation: 方法来要求您提供一个表示注释的视图。

此代码不起作用,我不知道为什么。

您是否在地图上添加了任何注释?如果是这样,您是否正在查看应该显示它们的地图部分?

我猜您正在查看animatesDrop 属性并期望它完成整个用户的pin-dropping 交互。它不这样做。将该属性设置为 YES 只会使图钉在地图上显示为动画。

【讨论】:

    【解决方案2】:

    好的,过了一会儿,我明白出了什么问题:

    theMap.delegate = (id) self;
    

    在构造函数中丢失了。一旦我这样做了,最终用户的任何操作都会激活地图的其他方法(协议)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-07-08
      • 1970-01-01
      • 2011-06-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多