MKAnnotation没有响应 [英] MKAnnotation not responding on tap
问题描述
我正在地图上显示几个图钉:
I am displaying several pins on map just like this:
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];
那么我选择每个引脚的外观如下:
THEN I choose "look" of each pin like this:
- (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];
但是,当我点击每个引脚时,我的屏幕上没有显示带有细节的气泡!有没有人经历过同样的事情?
BUT, when I click on each pin, I get no bubble displayed on screen with details! Anybody experiencing the same?
推荐答案
只是实现标题
属性不是足够。
你必须确保 title
没有返回 nil
或空白。 br>
如果是,即使 canShowCallout
设置为 YES
。
Simply implementing the title
property is not enough.
You have to make sure that title
is not returning nil
or blank.
If it is, the annotation view will not display a callout even if canShowCallout
is set to YES
.
与您的问题无关,但关于此行:
Unrelated to your issue but regarding this line:
region.span.latitudeDelta = meters / 111319.5;
不建议这样做,至少有三个原因是不必要的:
This is not recommended and unnecessary for at least three reasons:
-
计算两个角之间的仪表距离(您的经度和经度为度,然后将这些米转换回 degrees )是不必要的,因为
latitudeDelta
和longitudeDelta
只是顶级/之间的度数差异底部或左/右。所以你要做的就是:
Calculating the meter distance between the two corners (for which you have the latitude and longitude in degrees and then converting those meters back to degrees) is unecessary because
latitudeDelta
andlongitudeDelta
is simply the difference in degrees between the top/bottom or left/right. So all you have to do is:
region.span.latitudeDelta = fabs(southWest.latitude - northEast.latitude);
这是我建议的更改。
除以将米转换为度数( 111319.5
)的值只能在赤道上准确。
The value you are dividing by to convert meters to degrees (111319.5
) will only be accurate at the equator.
如果你想根据米来指定一个区域,而不是手动计算度数的跨度,那么使用内置的 MKCoordinateRegionMakeWithDistance
功能:
If you want to specify a region based on meters, instead of calculating the span in degrees manually, it's much better and easier to use the built-in MKCoordinateRegionMakeWithDistance
function:
CLLocationCoordinate2D center = CLLocationCoordinate2DMake (lat, long);
CLLocationDistance latMeters = 5000; //5 km
CLLocationDistance lonMeters = 5000; //5 km
MKCoordinateRegion region
= MKCoordinateRegionMakeWithDistance (center, latMeters, lonMeters);
此外,在你的情况下调用 regionThatFits
是不必要的,因为 setRegion
已经在你传递它的任何区域自己做了。当你想知道(没有实际改变区域)地图视图将区域调整到给定某个区域时,使用 regionThatFits
方法。
Also, calling regionThatFits
is unnecessary in your case because the setRegion
will already do this itself with whatever region you pass it. The regionThatFits
method is used when you want to know (without actually changing the region) what the map view would adjust the region to given a certain region.
这篇关于MKAnnotation没有响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!