gpt4 book ai didi

ios - MKAnnotation 在点击时没有响应

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:20:13 25 4
gpt4 key购买 nike

我在 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 已经对您传递给它的任何区域进行了此操作。 regionThatFits 方法用于当您想知道(无需实际更改区域) map View 会将区域调整为给定特定区域时的内容。

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com