Objective c 使用不正确的自定义pin图像显示地图注释

Objective c 使用不正确的自定义pin图像显示地图注释,objective-c,annotations,mkmapview,mapkit,Objective C,Annotations,Mkmapview,Mapkit,这是我在这个网站上的第一个问题 我的代码使用iOS 6 Mapkit Objective-C在以下函数中实现 - (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation 我认为你的问题很普遍。PinColor套装在哪里?mapview地图要求annotationView随时为任何注释绘制注释。如果通过其他方法将PinColor设置为0,然后ma

这是我在这个网站上的第一个问题

我的代码使用iOS 6 Mapkit Objective-C在以下函数中实现

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation 

我认为你的问题很普遍。PinColor套装在哪里?mapview地图要求annotationView随时为任何注释绘制注释。如果通过其他方法将PinColor设置为0,然后mapview希望绘制注释,则任何注释都将绘制红色接点。您需要做的是检查您正在绘制的注释,然后为其使用正确的颜色。您可以通过读取注释的标题来检查注释,或者如果它是您自己的注释类,则可以使用其他一些属性

旁注:对于两个pin版本,您都重复了几行,您应该将它们放在IF语句之外,并删掉一些行。您应该使用
viewForAnnotation
为您提供的地图视图:

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id < MKAnnotation >)annotation{

MKAnnotationView* pinview = nil;
if (annotation is the red one) {
    pinview = (MKAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:@"identifier"];
    if (nil == pinview) {                 
        pinview = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"identifier"];
        pinview.image = [UIImage imageNamed:@"redpin.png"];
     }        
} else {
   pinview = (MKAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:@"Orangeidentifier"];
    if (nil == pinview) {                
        pinview = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"Orangeidentifier"];
        pinview.image = [UIImage imageNamed:@"pin glabensmall.png"]; //orange pin           
    }
}

[pinview setCanShowCallout:YES];                

pinview.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];

pinview.opaque = YES;
if (self.newPinAdded) {
    [pinview setSelected: YES];                    
}

}
-(MKAnnotationView*)地图视图:(MKMapView*)地图视图用于注释:(id)注释{
MKAnnotationView*pinview=nil;
如果(注释是红色的){
pinview=(MKAnnotationView*)[mapView dequeueReusableAnnotationViewWithIdentifier:@“identifier”];
如果(nil==pinview){
pinview=[[MKAnnotationView alloc]initWithAnnotation:annotation重用标识符:@“标识符”];
pinview.image=[UIImage ImageName:@“redpin.png”];
}        
}否则{
pinview=(MKAnnotationView*)[mapView dequeueReusableAnnotationViewWithIdentifier:@“Orangeidentifier”];
如果(nil==pinview){
pinview=[[MKAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:@“Orangeidentifier”];
pinview.image=[UIImage ImageName:@“pin glabensall.png”];//橙色pin
}
}
[pinview setCanShowCallout:是];
pinview.rightCalloutAccessoryView=[UIButton Button类型为:UIButtonTypedTailDisplay];
pinview.opaque=是;
if(self.newPinAdded){
[选择的pinview设置:是];
}
}

我认为你的问题很常见。PinColor套装在哪里?mapview地图要求annotationView随时为任何注释绘制注释。如果通过其他方法将PinColor设置为0,然后mapview希望绘制注释,则任何注释都将绘制红色接点。您需要做的是检查您正在绘制的注释,然后为其使用正确的颜色。您可以通过读取注释的标题来检查注释,或者如果它是您自己的注释类,则可以使用其他一些属性

旁注:对于两个pin版本,您都重复了几行,您应该将它们放在IF语句之外,并删掉一些行。您应该使用
viewForAnnotation
为您提供的地图视图:

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id < MKAnnotation >)annotation{

MKAnnotationView* pinview = nil;
if (annotation is the red one) {
    pinview = (MKAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:@"identifier"];
    if (nil == pinview) {                 
        pinview = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"identifier"];
        pinview.image = [UIImage imageNamed:@"redpin.png"];
     }        
} else {
   pinview = (MKAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:@"Orangeidentifier"];
    if (nil == pinview) {                
        pinview = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"Orangeidentifier"];
        pinview.image = [UIImage imageNamed:@"pin glabensmall.png"]; //orange pin           
    }
}

[pinview setCanShowCallout:YES];                

pinview.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];

pinview.opaque = YES;
if (self.newPinAdded) {
    [pinview setSelected: YES];                    
}

}
-(MKAnnotationView*)地图视图:(MKMapView*)地图视图用于注释:(id)注释{
MKAnnotationView*pinview=nil;
如果(注释是红色的){
pinview=(MKAnnotationView*)[mapView dequeueReusableAnnotationViewWithIdentifier:@“identifier”];
如果(nil==pinview){
pinview=[[MKAnnotationView alloc]initWithAnnotation:annotation重用标识符:@“标识符”];
pinview.image=[UIImage ImageName:@“redpin.png”];
}        
}否则{
pinview=(MKAnnotationView*)[mapView dequeueReusableAnnotationViewWithIdentifier:@“Orangeidentifier”];
如果(nil==pinview){
pinview=[[MKAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:@“Orangeidentifier”];
pinview.image=[UIImage ImageName:@“pin glabensall.png”];//橙色pin
}
}
[pinview setCanShowCallout:是];
pinview.rightCalloutAccessoryView=[UIButton Button类型为:UIButtonTypedTailDisplay];
pinview.opaque=是;
if(self.newPinAdded){
[选择的pinview设置:是];
}
}

我没有设置任何PinColor。即使显示的红色pin也是我的自定义红色pin,而不是默认情况下在mapkit中出现的标准红色pin。您的代码以
if(PinColor==0)
开头。当-生成红色图像时,否则生成橙色图像。问题是你没有改变PinColor以匹配你正在绘制的注释。完美的答案。您的回答提到,在设置相应图像之前,我应该检查注释属性(即annotation.title或annotation.subtitle),这非常有用。我更新了我的annotation.title以适应可以帮助我做到这一点的文本,现在它运行良好。感谢您的快速帮助。很高兴为您服务。现在,您需要单击答案旁边的勾号/复选标记,以便所有人都知道答案已解决(因此我得到分数:)。我没有设置任何PinColor。即使显示的红色pin也是我的自定义红色pin,而不是默认情况下在mapkit中出现的标准红色pin。您的代码以
if(PinColor==0)
开头。当-生成红色图像时,否则生成橙色图像。问题是你没有改变PinColor以匹配你正在绘制的注释。完美的答案。您的回答提到,在设置相应图像之前,我应该检查注释属性(即annotation.title或annotation.subtitle),这非常有用。我更新了我的annotation.title以适应可以帮助我做到这一点的文本,现在它运行良好。感谢您的快速帮助。很高兴为您服务。现在,您需要单击答案旁边的勾号/复选标记,以便所有人都知道答案已解决(因此我得到分数:)