Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/119.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 更新MKMapkitview注释不起作用?_Ios_Annotations_Nsarray_Mkmapview_Mapkit - Fatal编程技术网

Ios 更新MKMapkitview注释不起作用?

Ios 更新MKMapkitview注释不起作用?,ios,annotations,nsarray,mkmapview,mapkit,Ios,Annotations,Nsarray,Mkmapview,Mapkit,我有一个UIViewcontroller,带有一个MKMapKit和一个PickerView。如下图所示。 我有一个plist,它包含与PickerView中的值相对应的所有坐标。因此,一旦我从PickerView中选择任何项目,我将刷新当前地图,并获取新项目的坐标(有时可能是5到6个坐标),并在地图中显示 但对我来说,我可以看到“注释”出现在地图视图中,但当我单击另一个项目时,之前的图钉仍然存在于地图中 我只想显示所选PickerView项目的批注PIN。请帮助我 用例: 这是我的plist文

我有一个UIViewcontroller,带有一个MKMapKit和一个PickerView。如下图所示。 我有一个plist,它包含与PickerView中的值相对应的所有坐标。因此,一旦我从PickerView中选择任何项目,我将刷新当前地图,并获取新项目的坐标(有时可能是5到6个坐标),并在地图中显示

但对我来说,我可以看到“注释”出现在地图视图中,但当我单击另一个项目时,之前的图钉仍然存在于地图中

我只想显示所选PickerView项目的批注PIN。请帮助我

用例: 这是我的plist文件

在我的列表中有一个名为“类别”的字段,Pickerview中列出的项目是总类别列表

因此,当我在PickerView中滚动时,我将检查所选项目是否与Plist category字段中的任何项目匹配。如果其匹配,则在地图视图中显示其坐标

但当我点击PickerView中的“Restarents”类别时,我得到了以下正确的地图

当我点击“cluburi”类别时,它应该在地图上显示2个Pin,而不是3个。实际上它并没有从地图上清除当前的“Restarent”Pin。如下图所示

那么,在显示新管脚之前,如何清除地图中以前的管脚

这是我在viewcontroller.m文件中的代码

-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    return 1;
}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    return [locations count];
}
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    return [locations objectAtIndex: row];
}
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{

[_MAPVIEW setRegion:[_MAPVIEW region] animated:YES];

    for(int j=0;j<[[savedContentsarray valueForKey:@"category"] count];j++)
    {
        if([[[savedContentsarray valueForKey:@"category"] objectAtIndex:j] isEqualToString:[locations objectAtIndex: row]])
         {


              coordinate.latitude=[[latitude objectAtIndex:j] floatValue];
              coordinate.longitude=[[longitude objectAtIndex:j] floatValue];
              annotation = [[myAnnotation alloc] initWithCoordinate:coordinate title:[[savedContentsarray valueForKey:@"name"]objectAtIndex:j]];
              [_MAPVIEW addAnnotation:annotation];


        }

    }

}

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id)annotation {
    //7
    if([annotation isKindOfClass:[MKUserLocation class]])
        return nil;

    //8
    static NSString *identifier = @"myAnnotation";
    MKPinAnnotationView * annotationView = (MKPinAnnotationView*)[_MAPVIEW dequeueReusableAnnotationViewWithIdentifier:identifier];
    if (!annotationView)
    {
        //9
        annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier];
        annotationView.pinColor = MKPinAnnotationColorPurple;
        annotationView.animatesDrop = YES;
        annotationView.canShowCallout = YES;
    }else {
        annotationView.annotation = annotation;
    }
    annotationView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
    return annotationView;
}
-(NSInteger)组件编号pickerView:(UIPickerView*)pickerView{
返回1;
}
-(NSInteger)pickerView:(UIPickerView*)pickerView行数组件:(NSInteger)组件{
返回[位置计数];
}
-(NSString*)pickerView:(UIPickerView*)pickerView标题箭头:(NSInteger)组件行:(NSInteger)组件{
返回[位置对象索引:行];
}
-(void)pickerView:(UIPickerView*)pickerView didSelectRow:(NSInteger)row不完整项:(NSInteger)组件{
[\u地图视图设置区域:[\u地图视图区域]已设置动画:是];

对于(int j=0;j因此,在我的应用程序中,我创建了一个从
MKAnnotationView
继承而来的自定义类,作为
btmapanotation
。您必须在选择picker控件中的任何对象时进行此调用

for (NSObject *a in [mapView annotations]) {
    if ([a isKindOfClass:[btMapAnnotation class]]) {
        btMapAnnotation *annotation = (btMapAnnotation *)a;
        [mapView removeAnnotation:annotation];
    }
}

完成此调用后,只需将新注释添加到地图中。

因此,如果我了解您的要求,您只希望在地图上显示picker视图中选定的项目。如果是这样,那么您需要从Mapview子视图中删除现有的pin。我有一段工作代码,请稍等片刻,让我把它放在地图上eOk那太好了!!,我已经试过了removeAnnotation代码,一旦我添加了,那么地图似乎总是空的..这就是我删除的。你能给出你的示例代码吗?你的意思是在PickerViewies的didselect方法中,一旦点击了picker项,上面的代码将尝试查找注释(如果有)并删除它。之后,你的代码如下若要使用plist文件中选定的坐标添加新批注。@BalramTiwari您能否看一看有关MKOVERAYRENDERER的问题,看看是否有解决方案,谢谢