ios - MKAnnotation 点击​​时没有响应

标签 ios ipad ios5 mkmapview mkannotationview

我在 map 上显示几个图钉,如下所示:

for (int i = 0; i < points.count; i++) {
    if([[[points objectAtIndex:i] objectForKey:@"lat"] class] != [NSNull class]) {
        southWest.latitude = MAX(southWest.latitude , [[[points objectAtIndex:i] objectForKey:@"lat"] doubleValue]);
        southWest.longitude = MIN(southWest.longitude, [[[points objectAtIndex:i] objectForKey:@"lon"] doubleValue]);
        northEast.latitude = MIN(northEast.latitude, [[[points objectAtIndex:i] objectForKey:@"lat"] doubleValue]);
        northEast.longitude = MAX(northEast.longitude, [[[points objectAtIndex:i] objectForKey:@"lon"] doubleValue]);

        pin.latitude = [[[points objectAtIndex:i] objectForKey:@"lat"] doubleValue];
        pin.longitude = [[[points objectAtIndex:i] objectForKey:@"lon"] doubleValue];
        CarPin *cPin = [[CarPin alloc] initWithName:[[self.brain.cars objectAtIndex:i] objectForKey:@"name"] state:[self getStateStringFor:[[[points objectAtIndex:i] objectForKey:@"state"] intValue]] coordinate:pin];
        cPin.state = [[[points objectAtIndex:i] objectForKey:@"state"] intValue];
        cPin.carID = [[points objectAtIndex:i] objectForKey:@"objekt_id"];
        [self.mapView addAnnotation:cPin];
    }
}

CLLocation *locSouthWest = [[CLLocation alloc] initWithLatitude:southWest.latitude longitude:southWest.longitude];
CLLocation *locNorthEast = [[CLLocation alloc] initWithLatitude:northEast.latitude longitude:northEast.longitude];
CLLocationDistance meters = [locSouthWest distanceFromLocation:locNorthEast];

MKCoordinateRegion region;
region.center.latitude = (southWest.latitude + northEast.latitude) / 2.0;
region.center.longitude = (southWest.longitude + northEast.longitude) / 2.0;
region.span.latitudeDelta = meters / 111319.5;
region.span.longitudeDelta = 0.0;

[self.mapView setRegion:[self.mapView regionThatFits:region] animated:YES];

然后我选择每个引脚的“外观”,如下所示:

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation {
static NSString *identifier = @"CarPin";
if ([annotation isKindOfClass:[CarPin class]]) {
    MKPinAnnotationView *annotationView = (MKPinAnnotationView *) [self.mapView dequeueReusableAnnotationViewWithIdentifier:identifier];
    if (annotationView == nil) {
        annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier];
    } else {
        annotationView.annotation = annotation;
    }
    annotationView.enabled = YES;
    annotationView.canShowCallout = YES;
    annotationView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
    if (((CarPin *)annotation).state == 1) {
        annotationView.pinColor = MKPinAnnotationColorGreen;
    } else {
        annotationView.pinColor = MKPinAnnotationColorRed;
    }
    return annotationView;
}
return nil;
}

我还设置了委托(delegate)

[self.mapView setDelegate:self];

但是,当我单击每个图钉时,屏幕上没有显示带有详细信息的气泡!有人有同样经历吗?

最佳答案

仅仅实现 title 属性是不够的。
您必须确保 title 不会返回 nil 或空白。
如果是,即使将 canShowCallout 设置为 YES,注释 View 也不会显示标注。


与您的问题无关,但关于此行:

region.span.latitudeDelta = meters / 111319.5;

不建议这样做,也没有必要这样做,至少有以下三个原因:

  • 无需计算两个角之间的米距(您拥有以为单位的纬度和经度,然后将这些米转换回)因为 latitudeDeltalongitudeDelta 只是顶部/底部或左侧/右侧之间的度数差异。所以你所要做的就是:

    region.span.latitudeDelta = fabs(southWest.latitude - northEast.latitude);
    

    这是我针对您的情况建议的更改。

  • 将米转换为度时所除的值 (111319.5) 仅在赤道处准确。

  • 如果您想指定基于米的区域,而不是手动计算以度为单位的跨度,使用内置的 MKCoordinateRegionMakeWithDistance 会更好更容易。功能:

    CLLocationCoordinate2D center = CLLocationCoordinate2DMake (lat, long);
    CLLocationDistance latMeters = 5000;  //5 km
    CLLocationDistance lonMeters = 5000;  //5 km
    MKCoordinateRegion region 
        = MKCoordinateRegionMakeWithDistance (center, latMeters, lonMeters);
    


另外,在您的情况下,调用 regionThatFits 是不必要的,因为 setRegion 本身已经对您传递的任何区域执行此操作。当您想知道(而不实际更改区域) map View 将区域调整为给定的特定区域时,可以使用 regionThatFits 方法。

关于ios - MKAnnotation 点击​​时没有响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12173496/

相关文章:

iOS cordova 应用程序性能分析

iphone - 以编程方式关闭 UIAlertView

ios - 尝试删除 NSUserdefault 数组中的 TableView 行

iphone - NSURLConnection 没有响应

iOS全时后台服务-基于位置追踪

ios - 如何使用PromiseKit/Photos?

ios - 如何摆脱 Xcode 中的 "Unknown type name ' __declspec'"错误

iphone - 检测 URL 并在 UITextField 中格式化它

ipad - 如何在 ipad safari 浏览器中选择文本框的整个文本?

objective-c - 排序 JSON 对象