【发布时间】:2011-05-05 21:35:57
【问题描述】:
请我最近制作了一张地图,它假设显示用户位置(绿色针颜色)和服务站的其他注释(红色针颜色),目前,我能够显示所有具有相同颜色的注释和我仍然无法告诉MKMapView 代表如何区分两种引脚类型,因此为每种类型分配正确的标题以显示。
这是我的MKAnnotationView 方法:
- (MKAnnotationView *)mapView:(MKMapView *)map viewForAnnotation:(id <MKAnnotation>)annotation {
static NSString *identifier = @"MyLocation";
if ([annotation isKindOfClass:[MyLocation class]]) {
MKPinAnnotationView *annotationView = (MKPinAnnotationView *) [map dequeueReusableAnnotationViewWithIdentifier:identifier];
if (annotationView == nil) {
annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier];
} else {
annotationView.annotation = annotation;
}
annotationView.enabled = YES;
[annotationView setAnimatesDrop:YES];
annotationView.canShowCallout = YES;
return annotationView;
}
return nil;
}
对于我的实现MKAnnotation委托的类,我有这个方法:
-(id)initWithName:(NSString *)enseigneDeLaStation distanceVersLaStation:(NSString *) distanceVersLaStation coordinate:(CLLocationCoordinate2D)coordinate
{
if ((self = [super init])) {
_enseigneDeLaStation = [enseigneDeLaStation copy];
_distanceVersLaStation = [distanceVersLaStation copy];
_coordinate = coordinate;
}
return self;
}
提前感谢您的帮助:)
编辑 我尝试像这样制作用户位置注释,如果我错了,请纠正我:
location2D = (CLLocationCoordinate2D){ .latitude = latitudeOfUserLocation, .longitude = longitudeOfUserLocation };
MyLocation *annotation=[[[MyLocation alloc]initWithName:@"You are here" distanceVersLaStation:@"" coordinate:location2D]autorelease];
[mapView addAnnotation:annotation];
编辑 2 再次嗨,我尝试制作一个注释以确保它甚至调用注释方法,所以我制作了这个简单的例子:
latitudeOfUserLocation=43.2923;
longitudeOfUserLocation=5.45427;
location2D = (CLLocationCoordinate2D){ .latitude = latitudeOfUserLocation, .longitude = longitudeOfUserLocation };
MyLocation *annotation=[[[MyLocation alloc]initWithName:@"You are here" distanceVersLaStation:@"coucou" coordinate:location2D]autorelease];
[mapView addAnnotation:annotation];
MKCoordinateSpan span={latitudeDelta:1,longitudeDelta:0.5};
MKCoordinateRegion region={location2D,span};
[mapView setRegion:region];
当我运行应用程序时,我看到与 43.2923/5.45427 一致的区域缩放得很好,但我没有看到注释,为什么它没有调用注释方法,我无法继续,因为它没有显示用户注释,请帮助,提前谢谢:)
编辑 3
嗨,我认为这总是与用户和站点引脚之间的差异有关,所以我声明了一个 var codeColor,在调用注释时将其设置为 1(用户的类型)并将其设置为 2(站点type) 调用车站的注释时:
-(void)viewWillAppear:(BOOL)animated
{
codeColor=1;//it's the first type, the user one
MyLocation *annotation=[[[MyLocation alloc]initWithName:@"You are right here" distanceVersLaStation:@"" coordinate:location2D]autorelease];
annotation.pinColor = MKPinAnnotationColorGreen;
[mapView addAnnotation:annotation];
}
和:
-(void)requestFinished:(ASIHTTPRequest *)request
{ codeColor=2;
[MBProgressHUD hideHUDForView:self.view animated:YES];
MyLocation *annotation=[[[MyLocation alloc]initWithName:ensStation distanceVersLaStation:distance coordinate:location2D]autorelease];
annotation.pinColor = MKPinAnnotationColorPurple;
[mapView addAnnotation:annotation];
}
对于注解方法:
if (codeColor==2) {
annotationView.rightCalloutAccessoryView=[UIButton buttonWithType:UIButtonTypeDetailDisclosure];
}
现在第一次测试时,一切正常,用户注释没有按钮,站一个是按钮,但是当我从视图中移动回来时,所有注释都带有按钮,甚至用户一。你能帮我吗,谢谢提前:)
【问题讨论】:
-
如何添加用户位置注释?您要为其显示绿色图钉还是标准动画蓝点?
-
嗨,实际上我想显示 API 提供的绿色
-
标签: ios annotations mapkit