Ios 在地图视图上重新添加注释以更新其标题
正如你在下面看到的,我用一种基本的方式模拟了我的问题。我有一个计时器,它定期调用一个方法。在该方法中,我创建了一个switch case条件来模拟我的想法 一旦pin添加到地图上,然后再次读取(pin不断下降) 我想添加我的pin,然后只更改表示天气值的标题Ios 在地图视图上重新添加注释以更新其标题,ios,mkmapview,mkannotation,Ios,Mkmapview,Mkannotation,正如你在下面看到的,我用一种基本的方式模拟了我的问题。我有一个计时器,它定期调用一个方法。在该方法中,我创建了一个switch case条件来模拟我的想法 一旦pin添加到地图上,然后再次读取(pin不断下降) 我想添加我的pin,然后只更改表示天气值的标题 - (IBAction)playBtn:(id)sender { timer = [NSTimer scheduledTimerWithTimeInterval:(4.0) target:self selector:@selec
- (IBAction)playBtn:(id)sender {
timer = [NSTimer scheduledTimerWithTimeInterval:(4.0) target:self selector:@selector(control) userInfo:nil repeats:YES];
}
-(void)control{
NSMutableArray *annotationArray = [[[NSMutableArray alloc] init] autorelease];
switch (value%2) {
case 0:
{
// Create some annotations
Annotation *annotation = nil;
annotation = [[Annotation alloc] init];
annotation.coordinate = CLLocationCoordinate2DMake(29.7161,-95.3906);
annotation.color = RGB(13, 0, 182);
annotation.title = @"17";
[annotationArray addObject:annotation];
[annotation release];
annotation = [[Annotation alloc] init];
annotation.coordinate = CLLocationCoordinate2DMake(30.168456,-95.504480);
annotation.color = RGB(0, 182, 146);
annotation.title = @"16";
[annotationArray addObject:annotation];
[annotation release];
// Center map
//self.mapView.visibleMapRect = [self makeMapRectWithAnnotations:annotationArray];
// Add to map
//[self.mapView addAnnotations:annotationArray];
}
break;
case 1:
{
// Create some annotations
Annotation *annotation = nil;
annotation = [[Annotation alloc] init];
annotation.coordinate = CLLocationCoordinate2DMake(29.7161,-95.3906);
annotation.color = RGB(13, 0, 182);
annotation.title = @"27";
[annotationArray addObject:annotation];
[annotation release];
annotation = [[Annotation alloc] init];
annotation.coordinate = CLLocationCoordinate2DMake(30.168456,-95.504480);
annotation.color = RGB(0, 182, 146);
annotation.title = @"25";
[annotationArray addObject:annotation];
[annotation release];
}
break;
}
[self.mapView addAnnotations:annotationArray];
[mapView setNeedsDisplay];
value++;
我认为您需要删除旧的注释,因为注释数组是只读的 NSArray*oldAnnotations=[地图注释];
[地图视图删除注释:旧注释] 如果要更新已添加到地图的注释的属性,请使用新属性再次添加该注释(无需先删除旧属性),只需在同一位置创建并添加另一个注释(使用新属性) 如您所见,调用
removeAnnotation
,然后调用addAnnotation
会导致闪烁和另一个拖放动画
相反,您必须获取对现有注释的引用,然后更新其属性
如果它只是一个注释,那么可以保留对它的类级ivar引用,并在值更改时使用该引用更新属性
如果需要在不同的时间更新不同的注释,一种简单的方法是在地图视图的注释
数组中搜索要更新的注释
这要求在注释类中有一个属性,该属性对于每个注释都是唯一的,并且(理想情况下)对于每个注释都保持不变。换句话说:如果要更新批注的标题
,请不要将标题
用作“唯一”批注标识符
相反,添加另一个属性(例如int或string),在创建注释时为每个注释指定该属性,并且该属性不会更改,以便以后可以使用该值查找注释
例如,假设将名为annotationId
的int属性添加到注释类中,并希望使用id#42更新注释:
BOOL annFound=NO;
//在地图视图的注释数组中循环查找注释id#42。。。
用于(mapView.annotations中的id)
{
if([ann Iskindof类:[MyAnnotationClass]])
{
MyAnnotationClass*myAnn=(MyAnnotationClass*)ann;
if(myAnn.annotationId==42)
{
annFound=是;
myAnn.title=@“一些新标题”;
打破
}
}
}
如果(!annFound)
{
//注释id#42尚未出现在地图上。
//创建它并添加到地图。。。
}
首先,感谢您的快速回复。但是,我已经删除了旧的注释。但是,在再次添加它们的同时,它仍然会在地图上留下别针。我不想让别针掉下来,我只想让别针留在地图上,但标题和副标题会发生变化。你好,安娜,首先,我要非常感谢你。pin标题现在可以更改而不闪烁。现在,我也想更改我的pin颜色。但是,即使我正在使用annotationId更改pin颜色,但它不会更改。。别针颜色不变!!再次感谢。还有一个问题,您上面编写的代码应该在(MKAnnotationView*)mapView:(MKMapView*)mV viewForAnnotation:(id)annotation中?不,此代码不应该在viewForAnnotation中。它应该位于注释属性更改的任何位置。我忘记提到的一件事是,如果任何注释属性影响注释视图,那么您需要做更多的事情来强制更新它。标题之所以有效,是因为MKAnnotationView已经监控了标注对该属性的更改。有关更新注释属性后如何强制更改注释视图的示例,请参见以下问题:,。非常感谢Anna提供了如此深入的信息。现在我可以更改pin和标题的颜色了。
BOOL annFound = NO;
//loop through the map view's annotations array to find annotation id# 42...
for (id<MKAnnotation> ann in mapView.annotations)
{
if ([ann isKindOfClass:[MyAnnotationClass class]])
{
MyAnnotationClass *myAnn = (MyAnnotationClass *)ann;
if (myAnn.annotationId == 42)
{
annFound = YES;
myAnn.title = @"some new title";
break;
}
}
}
if (!annFound)
{
//annotation id# 42 is not yet on the map.
//create it and add to map...
}