【问题标题】:Iphone MapView: Annotation Pin - Different ColorIphone MapView:注释引脚 - 不同颜色
【发布时间】:2011-03-16 19:36:21
【问题描述】:

我在解决这个问题时征求您的意见:

我正在获取一个 XML 文件以在我的 MapView 上显示 Pin 图。在另一种方法中,我在地图上显示我的当前位置:

-(void)setPOIs {

NSLog(@"insert POIs");

int x = 0;

while (x < [res count]) {

    CLLocationCoordinate2D c;
    NSString *latitude = [[res objectAtIndex:x] objectForKey:@"latitude"];
    NSString *longitude = [[res objectAtIndex:x] objectForKey:@"longitude"];
    c.latitude = [latitude doubleValue];
    c.longitude = [longitude doubleValue];
    GPSAnnotation *annotation = [[GPSAnnotation alloc] initWithCoordinate:c];
    annotation.mTitle=[[res objectAtIndex:x] objectForKey:@"ordiname"];
    annotation.currentPoint = [NSNumber numberWithInt:[[[res objectAtIndex:x] objectForKey:@"tierarztid"] intValue]];
    annotation.currentRow = [res objectAtIndex:x];
    [mapViewOutlet addAnnotation:annotation];
    [annotation release];

    NSLog(@"latitude setPOIs: %@", latitude);
    x++;
}   

[self showOwn];

}

-(void)showOwn {

CLLocationCoordinate2D c;
c.latitude = location.latitude;
c.longitude = location.longitude;
GPSAnnotation *annotation1 = [[GPSAnnotation alloc] initWithCoordinate:c];
annotation1.mTitle= @"Ihr Standort";
[mapViewOutlet addAnnotation:annotation1];
[annotation1 release];    

}

这里我展示了引脚:

- (MKAnnotationView *)mapView:(MKMapView *)mapViewOutlet viewForAnnotation:(GPSAnnotation *)annotation {

int postag = 0;
//int row = 0;

MKPinAnnotationView *annView=[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"currentloc"];
annView.pinColor = MKPinAnnotationColorGreen;
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:@"button_right.png"] forState:UIControlStateNormal];
[myDetailButton addTarget:self action:@selector(showLinks:) forControlEvents:UIControlEventTouchUpInside]; 


if ([[annotation title] isEqualToString:@"Current Location"]) {
    postag = 99999;
} else {
    postag = [annotation.currentPoint intValue];

} 



myDetailButton.tag  = postag;

// Set the button as the callout view
annView.rightCalloutAccessoryView = myDetailButton;

annView.animatesDrop=NO;
annView.canShowCallout = YES;
annView.calloutOffset = CGPointMake(-5, 5);


return annView;

[annView release];

}

如何为我的当前位置显示自定义图像 - 和/或不显示披露按钮?

【问题讨论】:

标签: iphone annotations android-mapview


【解决方案1】:

感谢您的反馈!我通过简单地调整 if...else 语句来解决

- (MKAnnotationView *)mapView:(MKMapView *)mapViewOutlet viewForAnnotation:(GPSAnnotation *)annotation {

int postag = 0;
//int row = 0;

MKPinAnnotationView *annView=[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"currentloc"];
annView.pinColor = MKPinAnnotationColorGreen;
UIButton *myDetailButton = [UIButton buttonWithType:UIButtonTypeCustom];
myDetailButton.frame = CGRectMake(0, 0, 23, 23);
myDetailButton.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
myDetailButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;


if ([[annotation title] isEqualToString:@"Ihr Standort"]) {
    postag = 99999;
    UIImage *annotationImage = [UIImage imageNamed:@"07-map-marker.png"];
    annView.image = annotationImage;
} else {
    postag = [annotation.currentPoint intValue];
    [myDetailButton setImage:[UIImage imageNamed:@"button_right.png"] forState:UIControlStateNormal];
    [myDetailButton addTarget:self action:@selector(showLinks:) forControlEvents:UIControlEventTouchUpInside]; 

} 



myDetailButton.tag  = postag;

// Set the button as the callout view
annView.rightCalloutAccessoryView = myDetailButton;

annView.animatesDrop=NO;
annView.canShowCallout = YES;
annView.calloutOffset = CGPointMake(-5, 5);


return annView;

[annView release];

}

BR,

斯蒂芬

【讨论】:

    【解决方案2】:

    不要使用MKPinAnnotationView,而是使用MKAnnotationViewMKPinAnnotationViewMKAnnotationView 的子类。 MKPinAnnotationView 将只显示引脚。 MKAnnotationView 有一个 image 属性,您可以将其设置为显示不同的图像而不是图钉。

    显示披露按钮的代码行是您的rightCalloutAccessoryView 指令。去掉它,标注中的细节披露就会消失。

    【讨论】:

      【解决方案3】:
      - (MKAnnotationView *)mapView:(MKMapView *)theMapView viewForAnnotation:(id <MKAnnotation>)annotation
      {
      if ([annotation isKindOfClass:[ParkingSpot class]])
          {
              static NSString* AnnotationIdentifier = @"ParkAnnotationIdentifier";
              MKAnnotationView* annotationView = (MKAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:ParkAnnotationIdentifier];
              if (!annotationView)
              {
                  MKAnnotationView *annotationView = [[[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationIdentifier] autorelease];
                  annotationView.draggable = NO;
                  UIImage *annotationImage = ...your image here
                  annotationView.image = annotationImage;
              }
          return annotationView;
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多