Val*_*imb 8 iphone mapkit mkmapview mkannotationview ios
我正在我的iPhone应用程序中的MapView中开发自定义引脚.这是代码:
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
static NSString *AnnotationViewID = @"annotationViewID";
MKAnnotationView *annotationView = (MKAnnotationView *)[myMapView dequeueReusableAnnotationViewWithIdentifier:AnnotationViewID];
if (annotationView == nil)
{
annotationView = [[[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationViewID] autorelease];
}
if ([[annotation title] isEqualToString:NSLocalizedString(@"Current Location",@"")]) {
MKPinAnnotationView *pin = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationViewID] autorelease];
//pin.pinColor = MKPinAnnotationColorRed;
annotationView = pin;
}
else
{
NSString *pin = [NSString stringWithFormat:@"pin%i.png",[[annotation imgNumber] intValue]];
annotationView.image = [UIImage imageNamed:pin];//Canviar la chincheta per numero
UIButton *button = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[annotationView setRightCalloutAccessoryView:button];
annotationView.annotation = annotation;
annotationView.canShowCallout = YES;
/********* Imagen a la izquierda en la burbuja *********/
annotationView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
// Set up the Left callout
UIButton *myDetailButton = [UIButton buttonWithType:UIButtonTypeCustom];
myDetailButton.frame = CGRectMake(0, 0, 23, 23);
myDetailButton.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
myDetailButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
// Set the image for the button
//[myDetailButton setImage:[UIImage imageNamed:@"botocorreu.png"] forState:UIControlStateNormal];
NSString *detailButton = [NSString stringWithFormat:@"%i",[[annotation imgNumber] intValue]];
[myDetailButton setImage:[UIImage imageNamed:detailButton] forState:UIControlStateNormal];
// Set the button as the callout view
annotationView.leftCalloutAccessoryView = myDetailButton;
annotationView.canShowCallout = YES;
}
return annotationView;
}
Run Code Online (Sandbox Code Playgroud)
这很好但是用红色针脚向我显示用户位置.我检查IB中的"显示用户位置".我想使用默认的蓝点,当用户移动时会自动移动它,是不是正确的?我怎样才能做到这一点?
非常感谢.
小智 22
这增加了顶部的的viewForAnnotation方法:
if ([annotation isKindOfClass:[MKUserLocation class]])
return nil;
Run Code Online (Sandbox Code Playgroud)
特殊用户位置注释是类型,MKUserLocation并返回nil,在这种情况下告诉地图视图绘制它的默认视图,即蓝点.
您也可以删除这些行,因为它们将不再需要:
if ([[annotation title] isEqualToString:NSLocalizedString(@"Current Location",@"")]) {
MKPinAnnotationView *pin = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationViewID] autorelease];
//pin.pinColor = MKPinAnnotationColorRed;
annotationView = pin;
}
else
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
16350 次 |
| 最近记录: |