我正在地图上显示几个图钉:
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;
}
我还设置了委托
[self.mapView setDelegate:self];
但是,当我点击每个引脚时,我的屏幕上没有显示带有细节的气泡!有没有人经历过同样的事情?
答案 0 :(得分:2)
仅仅实施title
财产是不够的
您必须确保title
未返回nil
或空白
如果是,即使canShowCallout
设置为YES
,注释视图也不会显示标注。
与您的问题无关,但关于这一行:
region.span.latitudeDelta = meters / 111319.5;
至少有三个原因不建议这样做,不需要这样做:
计算两个角之间的仪表距离(您有度的纬度和经度,然后将这些米转换回度)是不必要的因为latitudeDelta
和longitudeDelta
只是顶部/底部或左/右之间的度数差异。所以你要做的就是:
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
方法。