【问题标题】:MapView annotations not clickable and not showing detailsMapView 注释不可点击且不显示详细信息
【发布时间】:2012-12-20 08:10:37
【问题描述】:

1- 我正在使用以下代码,但是我的自定义图钉不会像默认我的位置图钉那样显示气泡。 2- 有没有办法自定义我的位置图钉以将颜色从默认的蓝色更改为红色,或者增加其动画圆半径?

-(void) showMarkers
{
    [self.mapView removeAnnotations:[self.mapView annotations]];
    for (int i = 0 ; i < 1;  i ++)
    {
        MKPointAnnotation *annotationPoint = [[MKPointAnnotation alloc] init];

        Players *player = [self.playersArray objectAtIndex:i];

        annotationPoint.coordinate = CLLocationCoordinate2DMake([player.latitude doubleValue], [player.longitude doubleValue]);
        annotationPoint.title = player.name;
        annotationPoint.subtitle = [NSString stringWithFormat:@"Level %@", player.level ];
        [self.mapView addAnnotation:annotationPoint];
        MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(annotationPoint.coordinate, 1500, 1500);
        MKCoordinateRegion adjustedRegion = [self.mapView regionThatFits:viewRegion];
        [self.mapView setRegion:adjustedRegion animated:YES];

    }
}

- (void)map:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
    self.mapView.centerCoordinate = userLocation.location.coordinate;
    myLocation = userLocation;
}

-(MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
{
    if ([[annotation title] isEqualToString:@"Current Location"] )
    {
        return nil;
    }

    MKAnnotationView *annView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"players"];
    annView.image = [UIImage imageNamed:@"marker.png"];
    annView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
    annView.canShowCallout = YES;
    annView.enabled = YES;
    [annView setUserInteractionEnabled:YES];

    return  annView;
}

【问题讨论】:

  • 你能用你看到的图片代替气泡吗?
  • 什么都没有!!我点击我的marker.png,没有任何反应
  • 尝试下断点,打印player的值,不就是nil吗?
  • 按照 Ramy 的建议,检查 player.name 是否不为零。是否设置了地图视图的委托,否则它不会调用您的 viewForAnnotation。另外,didUpdateUserLocation 方法应该命名为mapView:didUpdateUserLocation:(而不是map:didUpdateUserLocation:)。
  • 得到了修复,我认为字幕可能是空的。其他的呢,我们可以自定义默认的蓝色标记以在更大的屏幕上进行动画处理吗?

标签: iphone objective-c cocoa-touch


【解决方案1】:

最近在一个应用程序中,我使用自定义图像来显示气泡而不是默认图钉。使用的代码发布在下面。请看一看。也许会有所帮助。

(MKAnnotationView *) mapView:(MKMapView *)mapView1 viewForAnnotation:(id <MKAnnotation>) annotation{

Annotation *annotationInst = (Annotation*)annotation;


MKAnnotationView *pinView = nil;
if(annotation != mapView.userLocation)
{
    static NSString *defaultPinID = @"mypin";
    pinView = (MKAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:defaultPinID];

    if ( pinView == nil ){
        pinView = [[[MKAnnotationView alloc]
                   initWithAnnotation:annotation reuseIdentifier:defaultPinID]autorelease];

    }        

    pinView.canShowCallout = YES;
    pinView.image = [UIImage imageNamed:@"pin.png"];

    UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
    pinView.rightCalloutAccessoryView = rightButton; 
}
else {
    [mapView.userLocation setTitle:@"You are here"];
}
return pinView;
}

获取标注附件按钮点击:

- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control{
Annotation *tappedAnnotation =  view.annotation;
CLLocationCoordinate2D tappedLocation = [tappedAnnotation coordinate];  

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-11-23
    • 1970-01-01
    • 2013-09-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-08
    相关资源
    最近更新 更多