【问题标题】:IPhone SDK MapKIt Multiple Points and AnnotationiPhone SDK MapKIt 多点和注解
【发布时间】:2011-01-27 16:30:09
【问题描述】:

我是 iPhone SDK 开发的新手,我正在尝试使用 MapKit 创建一个应用程序,我已经完成了第一步,我想为应用程序添加多个引脚和注释,但我在这里迷路了。

以下是代码,我如何在此代码中添加更多引脚

-(void)viewDidLoad{
 [super viewDidLoad];

 [mapView setMapType:MKMapTypeStandard];
 [mapView setZoomEnabled:YES];
 [mapView setScrollEnabled:YES];
    MKCoordinateRegion region={{0.0,0.0,},{0.0,0.0}};
 region.center.latitude = 26.438047;
 region.center.longitude = 50.116422;
 region.span.latitudeDelta=0.01f;
 region.span.longitudeDelta=0.01f;
 [mapView setRegion:region animated:YES]; 
 [mapView setDelegate:self]; 

 DisplayMap *ann = [[DisplayMap alloc] init];
 ann.title = @"Corporate Office";
 ann.subtitle =@"King Khalid Street";
 ann.coordinate=region.center;
 [mapView addAnnotation:ann];
 }

-(MKAnnotationView *)mapView:(MKMapView *)mV viewForAnnotation:
(id <MKAnnotation>) annotation{
 MKPinAnnotationView *pinView=nil;
 if (annotation != mapView.userLocation) {
  static NSString *defaultPinID = @"com.invasivecode.pin";
  pinView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:defaultPinID];
  if (pinView ==nil) pinView = [[[MKPinAnnotationView alloc]
            initWithAnnotation:annotation reuseIdentifier:defaultPinID] autorelease];
  pinView.pinColor=MKPinAnnotationColorRed;
  pinView.canShowCallout=YES;
  pinView.animatesDrop=YES;
  pinView.calloutOffset= CGPointMake(-5, 5);
  }
 else {
  [mapView.userLocation setTitle:"I am here"];
 }
 return pinView;
}

【问题讨论】:

  • 添加第一个引脚的方式相同。有什么问题?顺便说一句,你应该在 addAnnotation 之后做[ann release];
  • 嗨,我尝试了几件事 [ann release];并用不同的变量重做代码并尝试for循环没有帮助吗?
  • 显示你尝试过的for循环(将其添加到问题中)。

标签: iphone ios4 mapkit


【解决方案1】:

您已经走在正确的轨道上,只需重复使用您的代码来提出多个观点。 例如:

 DisplayMap *ann = [[DisplayMap alloc] init];   


 for( int i =1;i<=5;i++ ){
     region.center.latitude = 26.438047+i;
     region.center.longitude = 50.116422+i;
     ann.title = [NSString stringWithFormat:@"title %d",i)];
     ann.subtitle =[NSString stringWithFormat:@"subtitle %d",i)];
     ann.image = [NSString stringWithFormat@"image_%d.png",i];

     ann.coordinate=region.center;
     [mapView addAnnotation:ann];
  }
 [ann release];

结果,将显示不同坐标的 5 个点。 (具有相同的名称和副标题)。

已编辑:显示不同的图钉图像。您必须将新字段作为 NSString *image 添加到 DisplayMap。并在 for 循环中添加路径图像。

- (MKAnnotationView *) mapView:(MKMapView *)amapView viewForAnnotation:(id      <MKAnnotation>) annotation
    {
 NSLog(@"pinnview before release %d",[pinView retainCount]);

if (pinView !=nil) {
    pinView =nil;
    [pinView release];
}
NSLog(@"pinnview after release %d",[pinView retainCount]);

// if it's the user location, just return nil.
if ([annotation isKindOfClass:[MKUserLocation class]])
    return nil;

if(annotation != map.userLocation)
{

    static NSString *defaultPinID = @"your-pin";

    pinView = (MKPinAnnotationView *)[map dequeueReusableAnnotationViewWithIdentifier:defaultPinID];

    if ( counting < [map.annotations count])
    {
        counting++;

        pinView = [[[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:defaultPinID] autorelease];


        for(DisplayMap* a in map.annotations)
        {
            if (annotation == a){
                pinView.image =
                [UIImage imageWithContentsOfFile:
                 [[NSBundle mainBundle] pathForResource:a.image ofType:nil]];   
            }
        }
        pinView.centerOffset= CGPointMake(0,-10);
        pinView.canShowCallout = YES;


    }

}

return pinView;

}

【讨论】:

  • 您好,感谢我尝试这种方式的输入,但它停止显示我尝试这种方法更新我的问题的引脚
  • for(int i=1; i
  • @tlikyu:您必须为每个注释创建一个新实例以创建多个引脚(就像 user592514 在他的示例中所做的那样)。
  • @user592514:您评论中的代码总体上没问题,但您可能看不到所有引脚,因为区域跨度设置为 0.01,但您的引脚被放置在 5.0 度(所以增加跨度看到他们)。此外,将所有地图视图设置移到循环之外(对于添加的每个图钉更改地图的区域是不好的或没有必要)。
  • 好的,非常感谢,所以如果我想要你的字符串数组来回纬度和经度,我会使用一个数组使用计数,然后参考经度和纬度,我必须增加跨度对吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-04-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多